ZOJ 2165 Red and Black

原题传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2165

Red and Black
Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


Source: Asia 2004, Ehime (Japan), Japan Domestic

这道题主要是考察BFS(广度优先搜索),每次去搜索与某个点相邻的(上下左右)四个点,如果这四个点中有符合条件---1.x>0,y>0,x<row,y<cloumn,并且没有被访问过,以及不是红色砖块。那么将这个个点入队。那么用什么条件来控制这个循环呢?答案当然是队列为空!

首先将起始点Pos,进队,然后while(队列不为空){  count++; 检查四周有木有符合上述条件的点,有则入队} printf count is ok!

//图的BFS ZOJ 2165
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
struct Pos{
    int x;
    int y;
};
const int MAXN = 21;
char list[MAXN][MAXN];
bool isVisited[MAXN][MAXN];
int row,cl,count;
Pos staPos;
int main(){
  queue<Pos> bfs;
  while(scanf("%d %d",&cl,&row)!= EOF){
        if(cl == 0&&row==0)break;
        count = 0;
        for(int i=1;i<MAXN;i++){
            for(int j=1;j<MAXN;j++){
                isVisited[i][j] = false;
            }
        }
        for(int i=1;i<=row;i++){
            scanf("%s",list[i]+1);
        }
        for(int i=1;i<=row;i++){
            for(int j=1;j<=cl;j++){
                if(list[i][j] == '@'){
                    staPos.x = i;
                    staPos.y = j;
                }
            }
        }
        bfs.push(staPos);
        isVisited[staPos.x][staPos.y] = true;
        while(!bfs.empty()){

            count++;
            Pos tempPos = bfs.front();
            bfs.pop();
            if((list[tempPos.x+1][tempPos.y] == '.')&& (tempPos.x+1<=row) && (!isVisited[tempPos.x+1][tempPos.y])){
                Pos newPos1;
                newPos1.x = tempPos.x+1;
                newPos1.y = tempPos.y;
                bfs.push(newPos1);
                isVisited[newPos1.x][newPos1.y] = true;
            }

            if((list[tempPos.x-1][tempPos.y] == '.')&& (tempPos.x-1>0) && (!isVisited[tempPos.x-1][tempPos.y])){
                Pos newPos2;
                newPos2.x = tempPos.x-1;
                newPos2.y = tempPos.y;
                bfs.push(newPos2);
                isVisited[newPos2.x][newPos2.y] = true;
            }
            if((list[tempPos.x][tempPos.y+1] == '.')&& (tempPos.y+1<=cl) && (!isVisited[tempPos.x][tempPos.y+1])){
                Pos newPos3;
                newPos3.x = tempPos.x;
                newPos3.y = tempPos.y+1;
                bfs.push(newPos3);
                isVisited[newPos3.x][newPos3.y] = true;
            }
            if((list[tempPos.x][tempPos.y-1] == '.')&& (tempPos.y-1>0) && (!isVisited[tempPos.x][tempPos.y-1])){
                Pos newPos4;
                newPos4.x = tempPos.x;
                newPos4.y = tempPos.y-1;
                bfs.push(newPos4);
                isVisited[newPos4.x][newPos4.y] = true;
            }

        }

        printf("%d\n",count);
  }
  return 0;

}

 

猜你喜欢

转载自hellojyj.iteye.com/blog/2091837