poj1979Red and Black(广度优先搜索)
2011-05-27 09:25
471 查看
Red and Black
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
Sample Output
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14020 | Accepted: 7218 |
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
45 59 6 13
广搜的一道基础题,但是要注意程序的组织结构
#include<cstdio> #include<cstring> #include<queue> using namespace std; int visited[20][20]; char map[20][20]; struct point{ int x,y; }; int move[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int w,h; int BFS(point s) { int i; int cx,cy; queue<point> qu; visited[s.x][s.y]=1; int max=1; qu.push(s); while(!qu.empty()) { point temp=qu.front(); qu.pop(); for(i=0;i<4;i++) { cx=temp.x+move[i][0];cy=temp.y+move[i][1]; if(cx>=0&&cx<h&&cy>=0&&cy<w&&!visited[cx][cy]&&map[cx][cy]!='#') { max++; visited[cx][cy]=1; point temp1; temp1.x=cx;temp1.y=cy; qu.push(temp1); } } } return max; } int main() { int i,j; while(scanf("%d%d",&w,&h)) { getchar(); int sx,sy; point start; memset(visited,0,sizeof(visited)); if(w==0&&h==0) break; for(i=0;i<h;i++) { for(j=0;j<w;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='@'){sx=i;sy=j;} } getchar(); } start.x=sx;start.y=sy; int max=BFS(start); printf("%d/n",max); } return 0; }
相关文章推荐
- poj1979 Red and Black DFS深度优先搜索+剪枝
- poj1979 Red and Black【搜索】
- Hdu1312 - Red and Black - 广度优先搜索
- poj1979(Red and Black)
- POJ1979 Red and Black【DFS】
- POJ1979:Red and Black(广度优先搜索)
- POJ1979(Red and Black)
- POJ 1979 Red and Black(BFS广度优先搜索)
- POJ1979_Red and Black【DFS】
- 北大ACM1979 - Red and Black (广度优先搜索)
- POJ1979red and black
- POJ1979-Red and Black-DFS染色
- POJ1979(DFS深度优先搜索)之 Red and Black
- POJ1979 Red and Black (简单DFS)
- poj1979 Red and Black
- POJ1979 Red and Black 题解
- HDU 1312:Red and Black (广度优先搜索)
- POJ1979 Red and Black (DFS)
- POJ1979 - Red and Black
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)