Problem 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) 
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
解题思路:简单深搜,水过!
AC代码:
 #include<iostream>
using namespace std;
const int maxn=;
int col,row,cnt,si,sj,dir[][]={{-,},{,},{,-},{,}};char mp[maxn][maxn];
void dfs(int x,int y){
if(x<||y<||x>=row||y>=col||mp[x][y]=='#')return;
mp[x][y]='#';cnt++;//标记为'#',并且计数器加1
for(int i=;i<;++i)
dfs(x+dir[i][],y+dir[i][]);//直接搜索四个方向
}
int main(){
while(cin>>col>>row&&(col+row)){
for(int i=;i<row;++i){
for(int j=;j<col;++j){
cin>>mp[i][j];
if(mp[i][j]=='@'){si=i;sj=j;}
}
}
cnt=;mp[si][sj]='.';dfs(si,sj);//从'@'开始搜索
cout<<cnt<<endl;
}
return ;
}

题解报告:hdu 1312 Red and Black(简单dfs)的更多相关文章

  1. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

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

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

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

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

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

  5. HDU 1312 Red and Black【DFS】

    搜索虐我千万遍@_@-----一道搜索的水题,WA了好多好多次@_@发现是n,m搞反了-_- 题意-- 给出m行 n列的矩形,其中从@出发,不能跳到#,只能跳到'.'问最多能够跳到多少块'.' 直接搜 ...

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

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

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

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

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

    传送门: 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(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  10. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

随机推荐

  1. 开车旅行(codevs 1199)

    题目描述 Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i的海拔高度为Hi, ...

  2. Codeforces700C. Break Up

    n<=1000,m<=30000的图,问割掉边权和尽量小的0.1或2条边使S和T不连通,输出割了哪些边,无解-1. 道理是很好懂的,先随便找S到T的一条路径,找不到输出0,找到的话这条路上 ...

  3. 封装java-get-post请求方式

    package com.ecar.eoc.content.platform.utils; import java.io.IOException;import java.util.HashMap;imp ...

  4. ZOJ3953 ZJU2017校赛(贪心)

    题意:给出n个区间,求至少删掉多少个区间使得不存在区间a, b, c 两两相交    (定义两个区间相交是,区间[l1, r1]和区间[l2, r2]相交,当且仅当存在一个数x,l1<=x< ...

  5. 苹果装WIN 7

    一.准备:1.8G或以上的正版U盘或者移动硬盘,提前备份U盘数据(2013款air要求安装64位系统,市面上比较多U盘不是正版盘,一般做不成功,要换盘) 2.64位纯净版Windows7 ISO的下载 ...

  6. 【SQL Server 学习系列】-- 清除SQL Server 2012连接过的服务器名称

    需要删除两个地方,删除前请注意备份! 1.  %AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin 2.  %A ...

  7. 条款十七: 在operator=中检查给自己赋值的情况

    在赋值运算符中要特别注意可能出现别名的情况,其理由基于两点.其中之一是效率.如果可以在赋值运算符函数体的首部检测到是给自己赋值,就可以立即返回,从而可以节省大量的工作,否则必须去实现整个赋值操作. 另 ...

  8. [Java Sprint] AutoWire

    Previous we have seen constructore injection: https://www.cnblogs.com/Answer1215/p/9484872.html It w ...

  9. makefile中的一点知识

    makefile文件里以下这一部分展开是什么样的呢? .. . mytarget=foo  $(mytarget): $(mytarget).c        gcc -o $(mytarget) $ ...

  10. Spring MVC 数据验证——validate编码方式

    1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...