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. HDU 2018 Multi-University Training Contest 1 Triangle Partition 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6300 Triangle Partition Time Limit: 2000/1000 MS (Java ...

  2. 2spring注解:@Lazy,@Conditional,@import,FactoryBean接口

    大致的工程内容也来源于上一篇博文! https://www.cnblogs.com/Mrchengs/p/10108603.html 1.@Lazy懒加载 懒加载:针对单实例的 单实例bean,默认在 ...

  3. 清空oracle session

    select 'alter system kill session '''||sid|| ','||serial#||''';' from v$session where username =' ';

  4. div拖动

    <!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta ...

  5. C#串口通讯

    本文提供一个用C#实现串口通讯实例,亲自编写,亲测可用! 开发环境:VS2008+.net FrameWork3.5(实际上2.0应该也可以) 第一步 创建一个WinForm窗体,拉入一些界面元素 重 ...

  6. mysql where 加 多个 或者条件

    select * from table where id=1 and uid=2 and (status=2 or status=3 or status=4);

  7. POJ 2208--Pyramids(欧拉四面体体积计算)

    Pyramids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3451   Accepted: 1123   Specia ...

  8. linux系统的介绍与环境搭建准备38-40

    操作系统(OS):用于控制管理计算机,形成在用户和机器之间传递信息的系统软件 linux是什么? <--unix系统是linux的前身---> 特点: 开放的源代码,自由修改 自由传播,没 ...

  9. Xshell配色方案推荐

    使用方法: 新建mycolor.xcs文件 复制粘贴如下代码,将文件导入,修改自己喜欢的字体即可 [mycolor] text=00ff80 cyan(bold)=00ffff text(bold)= ...

  10. 『Python基础-14』匿名函数 `lambda`

    匿名函数和关键字lambda 匿名函数就是没有名称的函数,也就是不再使用def语句定义的函数 在Python中,如果要声匿名函数,则需要使用lambda关键字 使用lambda声明的匿名函数能接收任何 ...