POJ1979 Red and Black (简单DFS)

时间:2022-03-06 16:11:55

POJ1979

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).
 
题意解析:
这个题的意思是说给定一个W*H的矩形,里面的值为“.” "#",其中“.”代表可到达的,“#”表示障碍,某人在一个“@”的起始点,求他所能到达的格子有多少个(包括第一所占的格子)。
 
思路:
1)将这个矩形根据值进行初始化,用map数组表示,“.”代表可到达的,用0表示,“#”表示障碍用1表示。记录起始点。
2)用一个数组visited存储位置的访问情况
3)进行一个简单的dfs,从起始点向四个方向分别进行dfs,如果可以到达(值为0),则将该位置的标志位visited标记为1.同时将结果数加1.
 
代码如下:

#include <stdio.h>
#define MAX_H 20

int map[MAX_H][MAX_H];
int visited[MAX_H][MAX_H];
int height;
int width;
int resultCount = 0;
int arr[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //right down left up

int dfs(int x, int y);
int main()
{
      //freopen("input.txt","r",stdin);
      int i = 0;
      int j = 0;
      char tempChar;
      scanf("%d %d",&width,&height);
      int startWidth = 0;
      int startHeight = 0;
      while(!(width==0 && height==0))
      {
            resultCount = 1;
            for(i=0;i<height;i++)
            {
                  for(j=0;j<width;j++)
                  {
                      visited[i][j] = 0;
                      scanf(" %c",&tempChar);
                      if(tempChar=='.')
                      {
                             map[i][j] = 0;
                      }
                      else if(tempChar=='#')
                      {
                             map[i][j] = 1;
                      }
                      else if(tempChar=='@')
                      {
                             map[i][j] = 2;
                             startWidth = j;
                             startHeight = i;
                      }
              }
        }

dfs(startHeight,startWidth);

printf("%d\n",resultCount);

scanf(" %d %d",&width,&height);
    }

return 0;
}

int dfs(int x, int y)
{
       int i = 0;
       int tempx = 0;
       int tempy = 0;
       for(i=0;i<4;i++)
       {
              tempx = x + arr[i][0];
              tempy = y + arr[i][1];
              if(tempx>=0 && tempx<height && tempy>=0 && tempy<width)
              {
                       if(map[tempx][tempy]==0 && visited[tempx][tempy]==0)
                       {
                                visited[tempx][tempy] = 1;

resultCount++;

dfs(tempx,tempy);

}
              }

}
       return 0;
}