传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1312

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25397    Accepted Submission(s): 15306

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
Source
Asia 2004, Ehime (Japan), Japan Domestic
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1372 1242 1253 1240 1072 
分析:
只能上下左右四个方向走,问你可以走的块最多是多少?#不能走
小技巧:走过的地方字符就变为#
先用dfs写一下,有时间再用bfs写
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 25
char G[max_v][max_v];
int n,m;
int sx,sy;
int step;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
void dfs(int x,int y)
{int xx,yy;for(int i=0;i<4;i++){xx=x+dir[i][0];yy=y+dir[i][1];if(xx>=0&&xx<n&&yy>=0&&yy<m&&G[xx][yy]!='#'){step++;G[xx][yy]='#';dfs(xx,yy);}}
}
int main()
{while(~scanf("%d %d",&m,&n)){if(n==0&&m==0)break;getchar();for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>G[i][j];if(G[i][j]=='@'){sx=i;sy=j;}}}step=1;G[sx][sy]='#';dfs(sx,sy);cout<<step<<endl;}return 0;
}

转载于:https://www.cnblogs.com/yinbiao/p/9350674.html

HDU 1312 Red and Black(最简单也是最经典的搜索)相关推荐

  1. Hdu 1312 Red and Black

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 //极其简单的dfs #include<iostream> using namesp ...

  2. hdu 1312 Red and Black 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 第二条深搜,题目并不难,但是做了我好久好久,由于一个细节,让我赌上了一个晚上的时间. 题目大意: ...

  3. HDU 1576 A/B(数论简单题,求逆元)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. c语言记忆化搜索,HNUSTC语言基础简单数据结构acm入门第一讲搜索.ppt

    C语言基础,简单数据结构,ACM入门讲座搜索部分 Bjut:mark063 2010.10.30 1 Evaluation only. Created with Aspose.Slides for . ...

  5. hdu 1312深搜入门题

    2019独角兽企业重金招聘Python工程师标准>>> Red and Black Problem Description There is a rectangular room, ...

  6. HDU - 3126 Nova(最大流+二分+简单几何)

    题目链接:点击查看 题目大意:给出一个二维平面坐标系,其中有n个巫师,m个敌人,以及k棵树,规定每个巫师都有一个攻击范围,可以攻击以巫师为圆心,以攻击范围为半径,形成的圆内的所有敌人,对于每棵树都有一 ...

  7. HDU 1012 u Calculate e(简单阶乘计算)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1012 u Calculate e Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 1565 方格取数(简单状态压缩DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 对于每一个数,取或者不取,用0表示不取,1表示取,那么对于每一行的状态,就可以用一个二进制的数来表示.比如 ...

  9. HDU 1712 ACboy needs your help(简单分组DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 m天复习n门课程,得分最高 代码: #include<iostream> #include&l ...

  10. 杭电 HOJ 1312 Red and Black 解题报告

    搜索,bfs.依旧用队列做.边界处懒得处理,全部初始化为-1.当然,0也可以.AC代码如下: #include<iostream> #include<deque> using ...

最新文章

  1. 做一个计算器_2019年初级会计考试考生能不能带计算器?现在统一回复!
  2. 微信扫码支付模式二【无法回调】解决方案
  3. python对笔记本电脑的要求-学Python买什么笔记本电脑?
  4. python语言介绍-Python这门语言的大概介绍
  5. java开发组态软件下载_基于JAVA的煤矿安全监控系统组态软件设计研究.pdf
  6. 作为IT从业者,你是如何做好个人职业规划?
  7. Ubuntu各版本主要差异
  8. redis多种模式部署(持续更新)
  9. php 日期 星期_php日期如何转星期
  10. 工欲善其事必先利其器——开发篇
  11. java使用itext实现把数据库中查到的数据转换成pdf
  12. 计算机c盘小了,电脑的C盘内存太小,怎么办?
  13. java除法运算有小数_Java 整数间的除法运算如何保留所有小数位?
  14. 读书笔记 《TAOCP》 V1 S1.2
  15. 线性回归 西瓜数据集 Python--sklearn
  16. TikTok搬运视频怎么做,搬运怎样的视频最好
  17. 专硕计算机学院排名,考研计算机院校排名
  18. Ajax异步配合数据字典完成下拉选框
  19. c语言双精度浮点数,使用C语言的十进制(浮点数,双精度)
  20. Linux 运维工程师(一)

热门文章

  1. Hibernate的双向N-N关联(六)
  2. 【渝粤教育】国家开放大学2018年秋季 0053-22T移动通信技术与移动通信设备 参考试题
  3. 【渝粤教育】电大中专药剂学基础知识 (2)_1作业 题库
  4. 【5分钟 Paper】Deterministic Policy Gradient Algorithms
  5. 伯克利,斯坦福,宾大联合发表:从观察和交互中学习预测模型
  6. Python进阶(一)Python中的内置函数、内置方法、私有属性和方法详解
  7. 整个社会总嫌自己不够“快”,为啥?
  8. 第一次迭代 一次会议
  9. 【转】郭敬明作品中100句经典的话
  10. [转]日常交际技巧经验总结100句