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

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

  1. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  3. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

  4. Poj1979 Red and Black (DFS)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 47466   Accepted: 25523 D ...

  5. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  6. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  7. 题解报告:hdu 1312 Red and Black(简单dfs)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  8. poj1979 Red And Black(DFS)

    题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...

  9. 【POJ - 1979 】Red and Black(dfs+染色)

    -->Red and Black Descriptions: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...

随机推荐

  1. ab压测参数说明

    ab是apache自带的压力测试工具,非常实用.ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也 ...

  2. Python 派生类子类继承类

    1.创建list类的子类Namedlist,初始化新类,创建新对象实例johnny,检查对象类型,并使用list的一些功能来存储数据 >>> class Namedlist(list ...

  3. smartgit document merge

    'Normal' Merge In case of a normal merge, a merge commit with at least two parent commits (i.e., the ...

  4. PLSQL_查询SQL的执行次数和频率(案例)

    2014-12-25 Created By BaoXinjian

  5. PO_PO系列 - 安全文件管控管理分析(案例)

    2014-07-01 Created By BaoXinjian

  6. wamp

    安装好wamp,但是图片没有变绿,大部分原因是80端口被占用. 修改端口号:可以从文件httpd.conf 将# Change this to Listen on specific IP addres ...

  7. redis 数据持久化

    1.快照(snapshots) 缺省情况情况下,Redis把数据快照存放在磁盘上的二进制文件中,文件名为dump.rdb.你可以配置Redis的持久化策略,例如数据集中每N秒钟有超过M次更新,就将数据 ...

  8. 对CSS中的Position属性的一些深入探讨

    转:http://www.cnblogs.com/coffeedeveloper/p/3145790.html Position属性 Position的属性值共有四个static.relative.a ...

  9. Memcache 问题集锦

    Memcached 集群架构方面的问题 这里收集了经常被问到的关于memcached的问题 memcached是怎么工作的? memcached最大的优势是什么? memcached和MySQL的qu ...

  10. 30天轻松学习javaweb_模拟tomcat

    运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...