Red and Black
Time Limit: 1000MS |
| Memory Limit: 30000K |
Total Submissions: 22633 |
| Accepted: 12217 |
Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
4559613
题意:
有@出发能够扫到的所有的‘.’(不能通过“#”)
搜堆栈题搜到的,但是比较像搜索,就先用dfs写了一遍,后来看也有用bfs、递归和堆栈写的,递归的跟dfs很像,堆栈的方法也就是把bfs里的队列换成了堆栈,思想都一样,只写了递归的,堆栈的就没写。
首先是dfs:
Source Code
Problem: 1979 | User: | |
Memory: 212K | Time: 0MS | |
Language: C++ | Result: Accepted |
- Source Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=100;
int row,line;
int table[maxn][maxn],visit[maxn][maxn],c;
int move[4][4]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int l,int r)
{
for(int i=0;i<4;i++)
{
//cout<<i<<endl;
int tmpl=l+move[i][0],tmpr=r+move[i][1];
if(tmpl<=line && tmpl>0 && tmpr>0 &&tmpr <=row && table[tmpl][tmpr] && !visit[tmpl][tmpr]){
visit[tmpl][tmpr]=1;
c++;
dfs(tmpl,tmpr);
//cout<<tmpl<<" "<<tmpr<<endl;
}
}
return;
}
int main(){
char tmp,test;
int al,ar;
while(scanf("%d %d",&row,&line)&&row&&line)
{
al=1;ar=1;
fill(visit[0],visit[maxn],0);
fill(table[0],table[maxn],0);
c=1;
for(int i=1;i<=line;i++)
{
getchar();
for(int j=1;j<=row;j++)
{
scanf("%c",&tmp);
if(tmp=='@')
{
al=i;ar=j;visit[i][j]=1;table[i][j]=2;
}
if(tmp=='.')
table[i][j]=1;
}
}
dfs(al,ar);
printf("%d\n",c);
}
}
然后是bfs,稍微烦了点,用G++提交才过的,C++会RE,不知道为什么
Source Code
Problem: 1979 |
| User: |
Memory: 672K |
| Time: 16MS |
Language: G++ |
| Result: Accepted |
· Source Code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=30;
int row,line,front=-1,rear=-1;
int table[maxn][maxn],visit[maxn][maxn],c;
int move[4][4]={{0,1},{0,-1},{1,0},{-1,0}};
struct queue{
int l;
int r;
};
queue q[1000];
void bfs()
{
int tmpl,tmpr,templ,tempr;
while(front!=rear)
{
rear++;
templ=q[rear].l;
tempr=q[rear].r;
for(int i=0;i<4;i++)
{
tmpl=templ+move[i][0],tmpr=tempr+move[i][1];
if(tmpl<=line && tmpl>0 && tmpr>0 &&tmpr <=row && table[tmpl][tmpr] && !visit[tmpl][tmpr]){
visit[tmpl][tmpr]=1;
q[++front].l=tmpl;
q[front].r=tmpr;
c++;
}
}
}
}
int main(){
int al,ar;
char tmp;
while(scanf("%d %d",&row,&line)&&row)
{
al=1;ar=1;
fill(visit[0],visit[maxn],0);
fill(table[0],table[maxn],0);
c=1;
for(int i=1;i<=line;i++)
{
getchar();
for(int j=1;j<=row;j++)
{
scanf("%c",&tmp);
if(tmp=='@')
{
al=i;ar=j;
visit[al][ar]=1;
table[al][ar]=2;
}
if(tmp=='.')
table[i][j]=1;
}
}
q[++front].l=al;
q[front].r=ar;
bfs();
printf("%d\n",c);
}
}
递归的方法,比较简洁,但是思想和dfs差不多
Source Code
Problem: 1979 |
| User: |
Memory: 724K |
| Time: 0MS |
Language: G++ |
| Result: Accepted |
· Source Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=100;
int row,line;
int table[maxn][maxn],c;
int count(int l,int r)
{
if(l<=0 || l>line || r<=0 || r>row || !table[l][r])
return 0;
table[l][r]=0;
return 1+count(l+1,r)+count(l-1,r)+count(l,r+1)+count(l,r-1);
}
int main(){
char tmp;
int al,ar;
while(scanf("%d %d",&row,&line)&&row&&line)
{
al=1;ar=1;
fill(table[0],table[maxn],0);
c=1;
for(int i=1;i<=line;i++)
{
getchar();
for(int j=1;j<=row;j++)
{
scanf("%c",&tmp);
if(tmp=='@')
{
al=i;ar=j;table[i][j]=2;
}
if(tmp=='.')
table[i][j]=1;
}
}
printf("%d\n",count(al,ar));
}
}