Red and Black

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

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

题意:给出一个类似迷宫的东西,#表示红砖,@表示起点,也是黑砖,点号表示黑砖,每次只能走黑砖,问最多经过多少块黑砖。每次只能走上下左右四个方向

题解:不用剪枝的dfs.....感动.jpg。 就直接看下一个地方能不能走,能走就标记并计数。只要走一遍,所以不需要回溯。因为写的是看下一步能不能走,所以没有算起点,答案就要加1

 #include<bits/stdc++.h>
using namespace std;
int w,h;
char s[][];
int maxx=;
int sx,sy;
int dirx[]={,-,,},diry[]={,,,-};
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
int xx=x+dirx[i];
int yy=y+diry[i];
if(xx<||yy<||xx>=h||yy>=w)continue;
if(s[xx][yy]=='.')
{
s[xx][yy]='#';
maxx++;
dfs(xx,yy);
}
}
}
int main()
{
while(~scanf("%d %d",&w,&h),w+h)
{
maxx=;
memset(s,'\0',sizeof(s));
for(int i=;i<h;i++)
{
getchar();
for(int j=;j<w;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j]=='@')
{
sx=i;
sy=j;
}
}
}
dfs(sx,sy);
printf("%d\n",maxx+);//最后答案加1是因为我每次判断下一个是可走的,就+1,这样就没有把起点计算进去
}
return ;
}

hdu1312Red and Black(迷宫dfs,一遍)的更多相关文章

  1. NYOJ306 走迷宫(dfs+二分搜索)

    题目描写叙述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩.它经常偷偷跑出实验室,在某个游乐场玩之不 ...

  2. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  3. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  4. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. P1605 迷宫 dfs回溯法

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  6. P1141 01迷宫 dfs连通块

    题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...

  7. P1141 01迷宫 DFS (用并查集优化)

    题目描述 有一个仅由数字00与11组成的n \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上 ...

  8. hdu1010Tempter of the Bone(迷宫dfs+剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. luogu P1238 走迷宫--DFS模板好(水)题

    题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...

随机推荐

  1. Sublime Text 3中关闭记住上次打开的文件

    使用UltraEdit的时候,每次安装后就得修改一堆配置,其中一项便是关闭“打开上一次未关闭的文件”,Sublime Text 2也有这么一个默认的功能,在实际使用中,这种方式确实可以较快速的访问文件 ...

  2. python中__init__.py与def __init__(self)的使用

    一直对__init__的使用很迷茫,这里系统的学习了解下 1.__init__.py文件-package的标识 python中每个package实际上是一个目录(Directory),程序运行时如何识 ...

  3. tensorflow的一些函数

    1.tf.constant(value,dtype=None,shape=None,name='Const') 注意这个函数创造的是一个常数tensor,而不是一个具体的常数 value:即可以是li ...

  4. springAOP(Aspect)权限访问页面

    1.XML进行配置切面 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  5. java中printf的用法

    目前printf支持以下格式:           %c        单个字符           %d        十进制整数           %f        十进制浮点数        ...

  6. x+=i和x = x+i比较 -- 简单赋值和复合赋值

    这两个赋值方式其实是有区别的,如果最后结果的类型和左操作数的类型一样,那么这两个表达式就完全等价. 下面来看看两个例子来理解它们的区别: 编写一个程序,使得x+=i合法, x = x+i: 不合法. ...

  7. win8安装wampserver报403错误解决方法

    看着别人开始体验win8了,前几天我也安装了win8系统,总体来说还不错,但是今天安装完Wampserver后,浏览器输入localhost,竟然报了403错误,我以为我安装出错了,后来研究了半天,发 ...

  8. 【题解】洛谷P3952 [NOIP2017TG] 时间复杂度(模拟)

    题目来源:洛谷P3952 思路 纯模拟没啥可说的了 果然好复杂 参考了你谷一个40行代码 代码 #include<iostream> #include<cstdio> #inc ...

  9. ArrayList的去重问题

    面试被问及arraylist的去重问题,现将自己想的两种解决方案写在下面 /** * Description: * ClassName:Uniq * Package:com.syd.interview ...

  10. Swift_方法

    Swift_方法 点击查看源码 ///方法 class Methods: NSObject { func test() { // self.testInstanceMethods() //实例方法 s ...