HDOJ 1312题Red and Black
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13508 Accepted Submission(s): 8375
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
题意:
n*m的方阵有红格或是黑格,只能走黑格
每次只能走上下左右四个紧邻方向的格子,求
这个人最后能走多少个黑格子。
分析:
dfs水题。从第一个黑格子开始递归的搜索,
每次搜索一个黑格子后为了以后不再重复走
这个黑格子,就把当前搜索的这个黑格子换
成红格子,然后继续dfs。。。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312;
题目大意:一个长方形空间,上面铺红色和黑色瓦片,一个人起初站在黑色瓦片上,每次可以走到相邻的4个黑色瓦片上,输入数据,求其能走过多少瓦片
题意:某人在@处为起点(也包括@点)#为墙,点(.)为通路,问最多能走多远统计能走几个点(加上@这个点)
思路:用dfs;
代码:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char a[30][30];
int ss,n,m;//这3个值需要用全局变量
int b[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
int dfs(int x,int y)
{
int xx,yy;
if(x<0||y<0||x>=m||y>=n)
return 0;
int i;
for(i=0; i<4; i++)
{
xx=x+b[i][0];
yy=y+b[i][1];
if(xx<0||yy<0||xx>=m||yy>=n||a[xx][yy]=='#')
//检查该点上下左右的点是否符合题目要求。
continue;
ss++;
a[xx][yy]='#';
//如果该点已经检查过,就把它变成'#',防止再次被检查。
dfs(xx,yy);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n||m))//n,m不能同时为0
{
int i,j;
int pi,pj;
getchar();//吸收换行符。
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='@')
{
pi=i;
pj=j;
}
}
getchar();//吸收换行符。
}
a[pi][pj]='#';
ss=1;
dfs(pi,pj);
printf("%d\n",ss);
}
return 0;
}
HDOJ 1312题Red and Black的更多相关文章
- HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- Hdoj 1312.Red and Black 题解
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj-1979 && hdoj - 1312 Red and Black (简单dfs)
http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...
- HDOJ 1312 DFS&BFS
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004题 Let the Balloon Rise strcmp()函数
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDOJ 1237题 简单计算器
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- HDOJ 1013题Digital Roots 大数,9余数定理
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
- BFS && DFS
HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...
随机推荐
- javascript创建对象(一)
对象定义:无序属性的集合,属性包含基本值.对象.函数,相当于一组没有特定顺序的值. 创建自定义对象最简单的方式就是: var movie=new Object(); movie.name=&qu ...
- How to hanganalyze and systemstate dumps
Oracle support request hang analysis and system state dumps when rasing SR. One 10.1 or higher versi ...
- Developing iOS8 Apps with Swift——iOS8概览
iOS 8 概览 斯坦福公开课--Developing iOS8 Apps with Swift学习笔记 想学习Swift,但是相应的教程不是很多,在CoCoaChina社区闲逛时恰好发现了这门课程, ...
- java动态绑定的情况分析
java是面向对象的语言,java中多态的一种情况是动态绑定.所谓的动态绑定,分两种情况:当调用类方法的时候,java虚拟机会基于对象的引用类型来选择执行方法.当java调用一个实例方法的时候,他会根 ...
- ie8中parseInt字符型数值转换数值型问题
今天在ie8中测试项目发现一个奇怪的问题,"08" "09" 强转竟然变成了: 后来发现ie8把"08" "09" 默认 ...
- java中关于static的小知识
static能够修饰属性和方法.凡是static修饰的方法和属性都是和类的关系较大,都在加载的时候要特殊处理(包括属性和类的优先加载).下面比较下static修饰属性和方法时的区别: 一.修饰属性的时 ...
- 我也来玩玩WinForm~BeginInvoke让用户体验更好!
前言 先说明一下,本人不太做winform的项目,工作10年以来,一直奋斗在webform的舞台上,今天有机会也接触了一下winform,下面对工作中用到的BeginInvoke方法作一下说明,和大家 ...
- C#.Net网页加载等待效果漂亮并且简单
最近网页加载数据比较多,点击后给用户就是白板很不友好,想了很久找了些资料,在网页加载中显示等待画面给客户,页面加载完成自动隐藏等待效果. 在网页后台cs代码: protected void Pa ...
- C#程序中:如何启用进程、结束进程、查找进程
在启动某个程序之前,如果需要先检查改程序是否已经运行,可以查找进程里有没有这个进程,再根据查找进程后的结果进行相应的判断操作. 产找进程的范围是任务管理器中的进程列表.如果进程被隐藏了,结果……(我没 ...
- UI图标不用愁:矢量字体图标Font-Awesome
Font-Awesome,这个项目主要是css3的一个应用,准确的说是一段css,这里的把很多图标的东西做到了font文件里面,然后通过引用外部font文件的方式,来展现图标. Font Awesom ...