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. 根据用户id生成一个唯一邀请码

    需求描述:根据用户id生成与之对应的唯一邀请码,范围为‘0-9A-Z’. 这个需求的重点在于加粗的部分,也就是要能够根据邀请码反推出用户ID,这样邀请码就不用入库了,在用户量很大的情况下,性能可以得到 ...

  2. 十五、详述 IntelliJ IDEA 插件的安装及使用方法

    正文 首先,进入插件安装界面: Mac:IntelliJ IDEA -> Preferences -> Plugins; Windows:File -> Settings -> ...

  3. Many-to-many relationships in EF Core 2.0 – Part 2: Hiding as IEnumerable

    In the previous post we looked at how many-to-many relationships can be mapped using a join entity. ...

  4. canvas背景

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [videos系列]日本的videos视频让男人产生了哪些误解?

    转载自:[videos系列]日本的videos视频让男人产生了哪些误解? 日本的videos视频是每个男人成长过程中都会看的启蒙教育片,也是男人在成年后调剂生活的必需品,但是由于影视作品是艺术的,是属 ...

  6. JQuery的焦点事件focus() 与按键事件keydown() 及js判断当前页面是否为顶级页面 子页面刷新将顶级页面刷新 window.top.location

    相关代码如下,使用看注解 <script type="text/javascript"> if(window.self != window.top){ window.t ...

  7. Archlinux下安装微信小程序开发工具

    由于微信小程序没有Linux版本,所以需要用wine来跑 一.安装wine sudo pacman -S wine 二.安装nwjs-sdk 微信开发工具包基于nwjs-sdk #没有wget就先安装 ...

  8. webpack安装与使用

    webpack: JavaScript 应用程序的静态模块打包器 安装webpack前需要搭建node环境: 1. 安装node.js(https://nodejs.org/en/), 安装完后会自动 ...

  9. QWebView 与Js 交互

    我本愚钝,在网上搜了一下没找到可以运行的栗子,遂在这记录一下吧. 环境:win10 64位系统  qt 4.8.7 (mingw32) qtcreator(4.5.0) 1. 建立一个 Widgets ...

  10. 利用HaoheDI从数据库抽取数据导入到hbase中

    下载apache-phoenix-4.14.0-HBase-1.4-bin.tar.gz 将其中的 phoenix-4.14.0-HBase-1.4-client.jar phoenix-core-4 ...