HDU 1979 Red and Black
题目:
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
Sample Input
- 6 9
- ....#.
- .....#
- ......
- ......
- ......
- ......
- ......
- #@...#
- .#..#.
- 11 9
- .#.........
- .#.#######.
- .#.#.....#.
- .#.#.###.#.
- .#.#..@#.#.
- .#.#####.#.
- .#.......#.
- .#########.
- ...........
- 11 6
- ..#..#..#..
- ..#..#..#..
- ..#..#..###
- ..#..#..#@.
- ..#..#..#..
- ..#..#..#..
- 7 7
- ..#.#..
- ..#.#..
- ###.###
- ...@...
- ###.###
- ..#.#..
- ..#.#..
- 0 0
Sample Output
- 45
- 59
- 6
- 13
题意描述:
输入矩阵的大小W和H(均小于20)
计算并输出从'@'位置最多能走多少块'.'
解题思路:
输入的时候找到'@'的位置,随后对其进行DFS搜索,下面的代码实现的搜索有点模拟广搜的意思。
代码实现:
- #include<stdio.h>
- char map[][];
- int dfs(int x,int y);
- int w,h;
- int main()
- {
- int i,j,sx,sy;
- while(scanf("%d%d",&w,&h),w+h != )
- {
- for(i=;i<=h;i++)
- {
- for(j=;j<=w;j++){
- scanf(" %c",&map[i][j]);
- if(map[i][j]=='@')
- { sx=i;sy=j; }
- }
- getchar();
- }
- printf("%d\n",dfs(sx,sy));
- }
- return ;
- }
- int dfs(int x,int y)
- {
- if(x< || x>h || y< || y>w)
- return ;
- //如果进入不了dfs函数就是边界问题,注意行数和列数就是x和y的范围
- if(map[x][y]=='#')
- return ;
- else
- {
- map[x][y]='#';
- return +dfs(x-,y)+dfs(x+,y)+dfs(x,y-)+dfs(x,y+);
- }
- }
易错分析:
1、如果搜索进入不了注意边界的设置问题
HDU 1979 Red and Black的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- 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 ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- 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 ...
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
随机推荐
- userdel 命令详解
userdel 作用: 删除指定用户,以及用户相关的文件. 如不加选项,则仅删除用户账号,而不删除相关文件 选项: -f:强制删除用户,即时用户当前已登录 -r:删除用户的同时删除与用户相关的所有文 ...
- git pull与git fetch的区别
git pull: 取回远程主机某个分支的更新,再与本地的指定分支合并. 用法: git pull <远程仓库> <远程分支名>:<本地分支名> // 如 git ...
- python:发送消息给微信企业号
# -*- coding:utf-8 -*- import requests import json ''' 基础环境:微信企业号 version:python 2.7 ''' class Send_ ...
- COMPUTE子句和Group By
首先声明一下,这个COMPUTE语法在SQLServer 2012之后,就废弃使用了,详情请看https://msdn.microsoft.com/librar ...
- Zabbix Agent端配置文件说明
Zabbix Agent端配置文件说明 由于工作中经常接触到zabbix,所以将agent配置整理一下,方便日常查看. # This is a config file for the Zabbix a ...
- Android 快速点击的处理
为了对付拥有麒麟臂的测试人员或者用户对我们的按钮等控件展开惨无人道的快速啄击.厮以为可以用如下方法: 1 setEnabled 大法:在用户点击发生后调用setEnable(false);阻止持续受到 ...
- js 客户端打印html 并且去掉页眉、页脚
print() 方法用于打印当前窗口的内容,支持部分或者整个网页打印. 调用 print() 方法所引发的行为就像用户单击浏览器的打印按钮.通常,这会产生一个对话框,让用户可以取消或定制打印请求. w ...
- asp.net mvc 客户端验证
插件 jQuery unobtrusive Validation @Html.TextBoxFor(x=>x.UserName) [StringLength(7,MinimumLength=2, ...
- 解决打开png图片黑屏问题(批量还原Xcode优化后的png)
window 打开Xcode 里面的png图片会黑屏,但是在mac 打开就显示正常, 这是因为Xocde里面的png图片被 pngcrush 优化过了,需要还原它的优化,window 平台才可以打开. ...
- 【三十】php之PDO抽象层
1.PDO介绍(php data object) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口. PDO 提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可 ...