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. 总结PHP删除字符串最后一个字符的三种方法

    一.前言 从数据库中select()读取一对多的信息时,经常需要将取出的数组用某个特定的字符分割,然后拼接成字符串. 常见的语法格式: foreach ($arr as $key => $val ...

  2. ZooKeeper系列(1)--分布式系统的基石

    分布式架构有以下几点普适性的共性需求: 1. 提供集群的集中化的配置管理功能,可以不重启就让新的配置参数生效,类似与配置中心      2. 简单可靠的集群节点动态发现机制,便于动态发现服务,动态扩展 ...

  3. Oracle中转义下划线

    原意是查询出所有的月粒度模型,但是在oracle中,下划线也代表匹配单一任何字符,导致15分钟粒度的模型也被查询出来,在此,需要对下划线做转义,使其只表示下划线的含义,可以使用ESCAPE()函数. ...

  4. Oracle中按规定的字符截取字符串

    CREATE OR REPLACE FUNCTION "F_SPLIT" (p_str IN CLOB, p_delimiter IN VARCHAR2) RETURN ty_st ...

  5. 【读书笔记】The Swift Programming Language (Swift 4.0.3)

    素材:Language Guide 初次接触 Swift,建议先看下 A Swift Tour,否则思维转换会很费力,容易卡死或钻牛角尖. 同样是每一章只总结3个自己认为最重要的点.这样挺好!强迫你去 ...

  6. input 输入的一些限制说明

    input输入框 只能输入 数字可以有小数点 <input class="form_text" id="purchasePrice" name=" ...

  7. Django忘记超级用户密码||账号

    第一步:运行django shell python3 manage.py shell 第二步:重设密码 >>> from django.contrib.auth.models imp ...

  8. AJAX 动态加载后台数据 绑定select

    <select id="select"> <!--下拉框数据动态加载--> </select> js:(使用jquery) $(document ...

  9. Ajax的open()方法

    Ajax的open()方法有3个参数:1.method:2.url:3.boolean: 参数1有get和post两个取值 参数2表示什么就不用说了 重点说下第3个参数:boolean的取值 当该bo ...

  10. 面试题——Java虚拟机

    一.运行时数据区域 Java虚拟机在执行Java程序的时候会把它所管理的内存划分为若干个不同的数据区域,这些区域各有用途: 程序计数器:(线程私有的) 程序计数器是一块较小的内存,可以看作是当前线程所 ...