HDU 1312:Red and Black

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

 

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).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13
题解:本题还是DFS搜索(上下左右),只是增加一个计数器,计算可以走的地方的个数。
      规定地图中有可通行的位置,也有不可通行的位置,已知起点,求起点的与它相连成一片的部分,在这道题里输出相连的位置的数目。
    从起点开始,遍历每一个到达的点的四个方向(不再是八个),到达一个位置就将这个位置的字符变成不可走的'#',并且计数+1。其实就是计数将可走变成不可走的操作进行了多少次。
这样可以不用担心走过了还会重复。
 
 AC
代码:如果你看过我的上一篇你一定会懂
#include<cstdio>
#include<cstring>
char pic[][];
int m,n,total;
int idx[][]; void dfs(int r,int c,int id)
{
if(r<||r>=m||c<||c>=n)
return;
if(idx[r][c]==||pic[r][c]!='.')
return;
idx[r][c]=id;
total++;
for(int dr=-; dr<=; dr++)
for(int dc=-; dc<=; dc++)
if(dr==||dc==)
dfs(r+dr,c+dc,id);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)==&&m&&n)
{
for(i =; i<m; i++)
scanf("%s",pic[i]);
memset(idx,,sizeof(idx));
total=;
for(i=; i<m; i++)
for(j=; j<n; j++)
{
if(pic[i][j]=='@')
{
pic[i][j]='.';
dfs(i,j,);
}
}
printf("%d\n",total);
}
return ;
}
 
 
 
这个是我在其他博客上看得到的方法,用#填充,可以一试!
 
#include <iostream>
using namespace std;
char a[][];
int n,m,total;
int dr[] = {,,,-};//行变化
int dc[] = {,,-,};//列变化
//上面的原来一直不会用,知道的话非常方便
bool judge(int x,int y)
{
if(x< || x>n || y< || y>m)
return ;
if(a[x][y]=='#')
return ;
return ;
}
void dfs(int r,int c)
{
total++;
a[r][c]='#'; //走过一次,“。”变为“#”,避免重复
for(int k=; k<; k++)
{
int lr = r + dr[k];
int lc = c + dc[k];
if(judge(lr,lc))
continue;
dfs(lr,lc);
} }
int main()
{
while(cin>>m>>n&&m&&n)
{
int i,j,x,y;
total=;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
{
cin>>a[i][j];
if(a[i][j]=='@') //这里必须用变量x,y
x=i,y=j; }
dfs(x,y);
cout<<total<<endl;
}
return ;
}
 

HDU 1312:Red and Black(DFS搜索)的更多相关文章

  1. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  2. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  3. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

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

  4. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  5. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

  6. HDU 1312 Red and Black (DFS)

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

  7. hdu 1312:Red and Black(DFS搜索,入门题)

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

  8. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  9. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. 课后习题 SPJ

    CREATE TABLE S ( SNO char(2) primary key, SNAME varchar(20), STATUS char(4), CITY char(10) ); create ...

  2. [Git] MAC上Git初探

    1.基本设置,包括用户名.邮箱.编辑工具.查看设置.帮助等 $ git config --global user.name "John Doe" $ git config --gl ...

  3. JVM垃圾回收机制入门

    前言 数据库是大家会普遍重视的一个领域,异步通信一般用不到,虚拟机在大部分时候不会出问题,常被人忽视,所以我打算先学习虚拟机,从零单排Java高性能问题. 堆内存存储结构 Java6是以年代来规划内存 ...

  4. @property中有哪些属性关键字?/ @property 后面可以有哪些修饰符?

    出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙(子循),目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 属性可以拥有的特质分为四类: 原子性- ...

  5. 多台计算机之间的ssh无密钥登录

    在很多分布式系统中,我们最常遇到的一个问题是,需要在服务器集群上保证多台机器之间的SSH无密钥登录.以Hadoop为例,为了方便,我们需要在master和slaves之间配置密钥登录,这样我们启动Ha ...

  6. Hadoop与HBase中遇到的问题

    1. Hadoop中遇到的问题 曾经所遇到的问题因为没有记录,所以忘了 (1)NameNode没有启动成功, 是因为你对HDFS多次格式化,导致datanode中与namenode中的VERSION文 ...

  7. "Storage Virtualization" VS "Software-Defined Storage"

    http://www.computerweekly.com/blogs/StorageBuzz/2013/07/storage-virtualisation-vs-soft.html 这篇blog的目 ...

  8. ubuntu sublime安装及配置

    安装sublime-text-2: sudo add-apt-repository ppa:webupd8team/sublime-text-2 sudo apt-get update sudo ap ...

  9. PermGen space错误解决方法

    在看下文之前,首先要确认意见事情,就是你是怎样启动tomcat的,我们在平时的开发环境其中,都是通过startup.bat方式启动tomcat的,那么你依照以下的方式,去改动/bin/catalina ...

  10. CF 19D Points 【线段树+平衡树】

    在平面上进行三种操作: 1.add x y:在平面上添加一个点(x,y) 2.remove x y:将平面上的点(x,y)删除 3.find x y:在平面上寻找一个点,使这个点的横坐标大于x,纵坐标 ...