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.
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)
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
45
59
6
13
思路
从起始点往4个方向开始遍历,但是最开始一直想dfs结束条件是什么?
void dfs(){
if(??){
/*
block code
*/
return ;
}
/*
block code
*/
return;
}
这题dfs其实不需要if判断结束.
以下是我的代码
/*************************************************************************
> File Name: hdu1312.cpp
> Author:gens_ukiy
> Mail:
> Created Time: 2016年12月02日 星期五 13时35分18秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}};
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
#define inf 0x3f3f3f
#define ll long long
#define pi acos(-1)
using namespace std;
char a[21][21];
int book[21][21];
int n,m;
int cnt;
void dfs(int x,int y){
if(book[x][y]==0){
cnt++;
book[x][y]=1;
rep(i,0,3){
int px=x+dire[i][0];
int py=y+dire[i][1];
if(px>=1&&px<=n&&py>=1&&py<=m&&book[px][py]==0&&a[px][py]!='#'){
dfs(px,py);
}
}
}
return ;
}
int main()
{
while(cin>>m>>n){
if(n==0&&m==0) break;
memset(book,0,sizeof(book));
cnt=0;
int sx,sy;
rep(i,1,n)
rep(j,1,m){
cin>>a[i][j];
if(a[i][j]=='@')
sx=i,sy=j;
}
dfs(sx,sy);
printf("%d\n",cnt);
}
return 0;
}
后来我又百度看了下大牛们的代码.
发现他们没有用book数组记录是否已经遍历,而是直接将当前字符修改为不可遍历字符,可以减少空间开辟.
/*************************************************************************
> File Name: hdu1312_1.cpp
> Author:gens_ukiy
> Mail:
> Created Time: 2016年12月02日 星期五 14时05分37秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}};
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
#define inf 0x3f3f3f
#define ll long long
#define pi acos(-1)
#define OnlineJudge
using namespace std;
char a[21][21];
int n,m;
int cnt;
void dfs(int x,int y){
cnt++;//能进入dfs,默认当前字符为'.'
a[x][y]='#';
rep(i,0,3){
int px=x+dire[i][0];
int py=y+dire[i][1];
if(px>=1&&px<=n&&py>=1&&py<=m&&a[px][py]!='#'){
dfs(px,py);
}
}
return;
}
int main()
{
#ifndef OnlineJudge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
while(cin>>m>>n){
if(m==0&&n==0) break;
int sx,sy;
cnt=0; //每次都要初始化
rep(i,1,n)
rep(j,1,m){
cin>>a[i][j];
if(a[i][j]=='@')
sx=i,sy=j; //记录起始位置
}
dfs(sx,sy);
printf("%d\n",cnt);
}
return 0;
}