POJ1979 Red and Black (简单DFS)

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;
}

时间: 2024-10-05 08:12:01

POJ1979 Red and Black (简单DFS)的相关文章

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

POJ 1979 Red and Black (简单dfs)

题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0; void dfs(int x,int y){ if(x < 0 || x >= h || y < 0 || y &g

poj1979 Red and Black(DFS)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25797   Accepted: 13967 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

POJ1979 Red and Black

问题链接:POJ1979 Red and Black. 题意简述:输入正整数w和h,w为列数,h为行数.输入h×w矩阵 (1 <= h <= 20; 1 <= w <= w),其中'.'代表可到达,'#'代表不可到达,'@'代表开始点.问从'@'开始可以到达最多多少个点. 问题分析:本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大.需要注意的是'@'也算一个可以到达的点. 程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

POJ 1979 Red and Black(简单DFS)

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