题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

Red and Black

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

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
题目大意: “.”代表黑地板,“#”代表红地板,“@”代表人的位置,人可以经过黑地板不能经过红地板,问在给定的图中人一共可以经过多少块地板
解题思路:深搜,在每个位置都搜索该位置的上下左右四个位置,将图中能够经过的点全部搜一遍
AC代码:
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int w,h;
int a[][] = {{-,},{,},{,-},{,}}; //位置数组
char str[][];
int f[][]; //标记该位置是否走过
int sum;
void dfs(int x,int y)
{
f[x][y] = ;
for (int i = ; i < ; i ++)
{
int x1=x + a[i][];
int y1=y + a[i][];
if (x1 >= && x1 < h && y1 >= && y1 < w && str[x1][y1]!='#' && f[x1][y1] == )
{ //判断边界、是否是不能经过的红地板、该地板是否已经经过
sum ++;
dfs(x1,y1);
}
}
}
int main ()
{
int i,j,x,y;
while (scanf("%d%d",&w,&h),w&&h)
{
for (i = ; i < h; i ++)
scanf("%s",str[i]); memset(f,,sizeof(f));
for (i = ; i < h; i ++)
for (j = ; j < w; j ++)
if (str[i][j] == '@') //找出人的位置
{
x = i;
y = j;
break;
}
sum = ;
dfs(x,y);
printf("%d\n",sum);
}
return ;
}

HDU 1312 Red and Black (dfs)的更多相关文章

  1. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  2. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU 1312 Red and Black(bfs)

    Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  5. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  6. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

  7. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  8. 题解报告:hdu 1312 Red and Black(简单dfs)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. Red and Black---hdu1312(dfs)

    2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...

随机推荐

  1. 《BI项目笔记》创建时间维度(2)

    创建步骤:   序号 选择的属性 重命名后的名称 属性类别 1 DateKey DateKey 常规 2 Month Key Month Key 月份 3 English Month Name Eng ...

  2. C语言中常见的排序方法

    在C语言中,常见的排序方法有冒泡法,排序法,插入法等等.所谓的冒泡法,就是对一组数字进行从大到小或者从小到大的一种排序方法.主要就是相邻的数值相互交换.从第一个数值开始,如果这相邻的两个数值排序与我们 ...

  3. boost 编译,windows平台

    下载Boost及生成bjam.exe文件 到Google网站搜索下载boost_1.52版本库,下载完成后,解压到X:下,这个地址自己随便定义.在解压的文件中搜索build.bat文件,把它所在的目录 ...

  4. 拼接字符串去掉最后多余的串,JSON的遍历

    一.遍历json change_url: function(key, value){ condition[key] = value; var string_url = "?"; f ...

  5. Linux 磁盘管理

    Linux磁盘管理好坏管理直接关系到整个系统的性能问题. Linux磁盘管理常用三个命令为df.du和fdisk. df:列出文件系统的整体磁盘使用量 du:检查磁盘空间使用量 fdisk:用于磁盘分 ...

  6. Windows Store App 全球化:在后台代码中引用字符串资源

    上文提到了引用字符串资源具有两种方式,分别是在XAML元素中和在后台代码中引用资源文件中的字符串资源.在第一小节已经介绍了如何在XAML元素中引用字符串资源,本小节将讲解在后台代码中引用字符串资源的相 ...

  7. jQuery:常用方法一览

    Attribute:$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”test Image”}); 给 ...

  8. ASP.NET Redis 开发 [转]

    Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redis纯粹为应用而产生,它是一个高性能的 ...

  9. @HTML.checkboxFor()用法

    <%=Html.CheckBox("chk1",true) %> <%=Html.CheckBox("chk1", new { @class= ...

  10. JavaWeb chapter9 JSP基础

    1.  Servlet的缺陷 一个动态网页中,大部分内容都是HTML代码等固定不变的内容,编写和修改HTML非常不方便,令人厌恶: 部署Servlet是繁琐而且容易出错的任务:(Servlet3.0规 ...