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. RPM Fusion on CentOS7

    RPM Fusion RPM Fusion provides software that the Fedora Project or Red Hat doesn't want to ship. Tha ...

  2. HDU 3480 division

    题目大意:一个有n个数的集合,现在要求将他分成m+1个子集,对子集i设si表示该集合中最大数与最小数的差的平方.求所有si的和的最小值.n<=10000,m<=5000. 分析:最优解的m ...

  3. Smartclient发布的几个异常问题

    1.不能下载.config文件 如果是iis6右键网站属性--->主目录--->配置 1.右键虚拟目录,在虚拟目录-配置-映射,应用程序扩展中删除.config 项目 2.IIS属性--& ...

  4. [系统开发] FileMaker进销存系统

    一.简介 这是我用 FileMaker 编写的进销存系统: FileMaker 是一种在欧美流行的桌面型数据库:它使用非常方便,功能也很强大,用户可以在它上面开发自己的系统: 开发时间:2008年 二 ...

  5. 黄聪:Discuz!的SEO优化策略二:如何去掉页脚多余的信息

    论坛搭建好,首先是把多余的东西都砍掉. 页脚的信息在我看来,都是很多余的信息,如下图: 要怎么消灭掉它们呢? 1.进入 全局 -- 站点信息 2.站点名称改为你的论坛名称,它会出现在内页的标题最末位. ...

  6. [JS]Javascript的this用法

    转自:阮一峰 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着 ...

  7. Spark1.6 DataSets简介

    Apache Spark提供了强大的API,以便使开发者为使用复杂的分析成为了可能.通过引入SparkSQL,让开发者可以使用这些高级API接口来从事结构化数据的工作(例如数据库表,JSON文件),并 ...

  8. Workflow_工作流发送Document和Form链接的实现(案例)

    2014-06-01 Created By BaoXinjian

  9. 实现图片的2次缩放后再进行candy边缘检测

    //实现图片的2次缩放后再进行candy边缘检测//Author:SD//Date:2015-9-27#include "cv.h"#include "highgui.h ...

  10. Centos 7 修改SSH端口号

    注意!这里的Centos版本是7   step1 修改/etc/ssh/sshd_config vi /etc/ssh/sshd_config #Port 22         //这行去掉#号 Po ...