Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25113 Accepted Submission(s): 15166
Problem 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.
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)
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)
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
Source
题意:从@这个起点出发,求所有能走的.的个数,#为墙
思路:dfs写,用一个vis数组记录是否已经走过,
代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
char k[1000][1000];
int n,m,sum,c1,c2;
int x[4]={-1,0,0,1};
int y[4]={0,-1,1,0};
int vis[1000][1000];
void dfs(int a,int b)
{
for(int i=0;i<4;i++){
int sx=x[i]+a;
int sy=y[i]+b;
if(sx<0||sx>=m||sy<0||sy>=n||vis[sx][sy]==1||k[sx][sy]=='#') continue;//sx,sy对应的n,m一定要注意
if(k[sx][sy]=='.'&&vis[sx][sy]==0){//vis判断是否已经访问
sum++;
vis[sx][sy]=1;
dfs(sx,sy);//不在此处在sum+1,回溯的时候会变??
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m),n&&m){//若在此处添加\n,而不是在下方用getchar();
getchar(); //则遇到0 0会无法退出
memset(k,'\0',sizeof(k));
memset(vis,0,sizeof(vis));
sum=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>k[i][j];
if(k[i][j]=='@') c1=i,c2=j;
}
}
dfs(c1,c2);
printf("%d\n",sum);
}
}
提供第二种写法的dfs,这个效率更高一点,写的也少
代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
char k[1000][1000];
int n,m,sum,c1,c2;
int x[4]={-1,0,0,1};
int y[4]={0,-1,1,0};
void dfs(int a,int b)
{
sum++;
k[a][b]='#';
for(int i=0;i<4;i++){
int sx=x[i]+a;
int sy=y[i]+b;
if(sx>0&&sx<=n&&sy>0&&sy<=m&&k[sx][sy]=='.'){//若起始的i,j,是否是从1开始,注意边界判断
dfs(sx,sy);
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n),n&&m){//若在此处添加\n,而不是在下方用getchar();
getchar(); //则遇到0 0会无法退出
memset(k,'\0',sizeof(k));
sum=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>k[i][j];
if(k[i][j]=='@') c1=i,c2=j;
}
}
dfs(c1,c2);
printf("%d\n",sum);
}
}