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. 【linux】压缩和解压缩

    .gz格式 压缩gzip: gzip只能压缩文件,且压缩后文件消失,不能压缩目录. [root@andon tmp]# ls ml orbit-gdm pulse-2sLvu7UbjUYf pulse ...

  2. Python基础(三)——集合、有序 无序列表、函数、文件操作

    1.Set集合 class set(object): """ set() -> new empty set object set(iterable) -> n ...

  3. VS2012远程调试(winform+web 远程调试)

    VS2012远程调试   一.调试WinFrom 程序 安装rtools_setup_x64 下载 配置Remote 启动Remote debugger 默认端口4016,选择工具-〉选项,选择 无身 ...

  4. 高性能MySQL --- 读书笔记(2) - 2016/8/2

    第1章 MySQL架构 MySQL架构与其他数据库服务器大不相同,这使它能够适应广泛的应用.MySQL足够灵活,能适应高要求架构.例如Web应用,同时还适用于嵌入式应用.数据仓库.内容索引和分发软件. ...

  5. 重绘TabControl

    本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...

  6. 黄聪:HtmlAgilityPack,C#实用的HTML解析类简介

    HtmlAgilityPack是.net下的一个HTML解析类库.支持用XPath来解析HTML.这个意义不小,为什么呢?因为对于页面上的元素的xpath某些强大的浏览器能够直接获取得到,并不需要手动 ...

  7. [OpenGL ES 03]3D变换:模型,视图,投影与Viewport

    [OpenGL ES 03]3D变换:模型,视图,投影与Viewport 罗朝辉 (http://blog.csdn.net/kesalin) 本文遵循“署名-非商业用途-保持一致”创作公用协议 系列 ...

  8. PLSQL_性能优化系列15_Oracle Explain Plan解析计划解读

    2014-12-19 Created By BaoXinjian

  9. linux常用命令 2

    netstat 显示网络统计信息的命令 此命令用来显示整个 netstat -anp p显示名称的名字 kill -9 进程号 traceroute 显示数据包历程 查询 系统有哪些shellcat ...

  10. linux 挂载(转载)

    From:http://forum.ubuntu.org.cn/viewtopic.php?t=257333 用linux,就一定要用linux的方式去思维.嗯,说的容易做起来难.我的D盘哪去了?恐怕 ...