Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8336 Accepted Submission(s): 5184
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
45 59 6 13
题解:一道经典的搜索题。
DFS:
/*
题意:'#'不可走,'.'可走,'@'为起点。
求从'@'开始走,一共可以走完多少个'.','@'也算一个。
思路:dfs深度遍历周围的四个方向
优化:遍历前,图的四周加边。
注意:先输入高度,再输入宽。也就是先输入行数再输入列数。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char map[25][25];
int w, h;
int sx, sy; //起点
int ans;
void dfs(int x, int y) //深度遍历
{
if(map[x][y] == '#') return; //不可走,马上跳出
ans++; //可以走 结果+1
map[x][y] = '#'; // 标记已走
dfs(x-1, y); dfs(x+1, y); //遍历周围的四个方向
dfs(x, y-1); dfs(x, y+1);
}
int main()
{
while(scanf("%d%d", &w, &h) != EOF) //先输入列数 再输入行数
{
if(w == 0) break; // 输入0即结束
for(int i = 1; i <= h; i++)
{
for(int j = 1; j <= w; j++)
{
cin>>map[i][j];
if(map[i][j] == '@') //起点
{
sx = i;
sy = j;
}
}
}
for(int i = 0; i <= h+1; i++) map[i][0] = map[i][w+1] = '#'; //第0列和第w+1列加边 不可走
for(int i = 0; i <= w+1; i++) map[0][i] = map[h+1][i] = '#'; //第0行和第h+1行加边 不可走
ans = 0; //初始化结果
dfs(sx, sy);
printf("%d\n", ans);
}
return 0;
}
BFS:
/************************************************************************/
/*
poj 1979 red and black
使用方法为BFS */
/************************************************************************/
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int n,m,sx,sy,step;
struct Node{
int x,y;
};
#define MAX 25
char map[MAX][MAX];
int vist[MAX][MAX];
int dist[4][2]={1,0,-1,0,0,1,0,-1};
int inMAP(int x,int y){
if(x<0 || x>=n || y<0 || y>=m)
return 0;
return 1;
}
void BFS(){
queue <Node> Q;
Node start,now,next;
start.x = sx;
start.y = sy;
step=1;//开始位置
vist[sx][sy]=1;
Q.push(start);
while(!Q.empty()){
now = Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
next.x =now.x +dist[i][0];
next.y =now.y +dist[i][1];
//满足条件的情况
if(!vist[next.x][next.y] && map[next.x][next.y]=='.' && inMAP(next.x,next.y)){
vist[next.x][next.y]=1;
step+=1;
Q.push(next);
}
}
}
}
int main(){
int i,j;
//注意这里的行和列,被坑了。
while(cin>>m>>n){ //m=6列 y ,行n=9 x
memset(vist,0,sizeof(vist));
if(n+m==0)break;
for(i=0; i<n;i++)
for(j=0;j<m;j++)
cin>>map[i][j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(map[i][j]=='@')
{
sx=i;
sy=j;
break;
}
BFS();
cout<<step<<endl;
}
return 0;
}
HDU题目链接
POJ题目链接