poj 1979 Red and Black - bfs

时间:2023-02-10 14:44:40

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

思路:
先找到,然后用bfs进行搜索,查找此点附近的四个点,如果是'.'并且之前没有走过这个点则把此点加入队列并且标记为'走过的点',同时可以走的点计数加1。

源代码:

 1 #include <iostream>
 2 #include<queue>
 3 #include <stdio.h>
 4 #include<string.h>
 5 char s[25][25];  6 int visit[25][25];  7 using namespace std;  8 int main()  9 { 10 int h,w; 11    int x,y,a,b,i,j; 12     int sum=0; 13    while(scanf("%d %d",&w,&h)) 14  { 15        if(w==0&&h==0) break; 16         memset(visit,0,sizeof(visit)); 17        sum=0; 18        for(i=0;i<h;i++) 19  { 20           scanf("%s",s[i]); 21         
22            for(j=0;j<w;j++) 23  { 24                
25                 if(s[i][j]=='@') 26              {x=i; 27              y=j; 28              visit[i][j]=1; 29            
30  } 31              if(s[i][j]=='#') 32              visit[i][j]=1; 33  } 34  } 35    int ok; 36    queue<int>q; 37  q.push(x); 38  q.push(y); 39    while(!q.empty()) 40    {ok=1; 41        a=q.front(); 42  q.pop(); 43        b=q.front(); 44  q.pop(); 45     if(a-1>=0&&s[a-1][b]=='.'&&visit[a-1][b]==0) 46  { 47        sum++; 48        visit[a-1][b]=1; 49        q.push(a-1); 50  q.push(b); 51       
52  } 53     if(a+1<h&&s[a+1][b]=='.'&&visit[a+1][b]==0) 54  { 55         sum++; 56         visit[a+1][b]=1; 57         q.push(a+1); 58  q.push(b); 59        
60  } 61     if(b-1>=0&&s[a][b-1]=='.'&&visit[a][b-1]==0) 62  { 63         sum++; 64         visit[a][b-1]=1; 65  q.push(a); 66         q.push(b-1); 67       
68  } 69     if(b+1<w&&s[a][b+1]=='.'&&visit[a][b+1]==0) 70  { 71         sum++; 72         visit[a][b+1]=1; 73  q.push(a); 74         q.push(b+1); 75        
76  } 77     for(i=0;i<h;i++) 78  { 79         for(j=0;j<w;j++) 80  { 81          
82  } 83  } 84  } 85   printf("%d\n",sum+1); 86  
87  } 88     return 0; 89 }