HDU 1312 Red and Black (dfs)
题目链接: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
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.
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)
which contains the number of tiles he can reach from the initial tile (including
itself).
#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)的更多相关文章
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1312 Red and Black(bfs)
Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 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 ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- Red and Black---hdu1312(dfs)
2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...
随机推荐
- 用Dictionary替换switch case
用switch case处理一个很长的判断,例如56个民族01代表汉族,02代表藏族,03代表壮族...,当传入数字想获取民族名称时就得写56个case,当传入民族获取背后的数字时,又得再写56个ca ...
- UVa 12299 RMQ with Shifts(移位RMQ)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- 【转】Vim十大必备插件
[转]Vim十大必备插件 转自:http://my.oschina.net/zhoukuo/blog/336315 Taglist taglist是一个用于显示定位程序中各种符号的插件,例如宏定义.变 ...
- 转:不应该不知道C++的常用库
不应该不知道C++的常用库 非常惭愧,我过去也仅仅了解boost.STLport这样的库,以及一些GUI库,但是居然有如此众多的C++库,其实令我惊讶.当然,这个问题应该辩证的看,对于拿来主义确实可以 ...
- Linux下使用autoconf 和 automake 编译简单的HelloWorld
使用过开源C/C++项目的同学都知道,标准的编译过程已经变成简单的三部曲:./configure /make/make install,使用起来很方便,不像平时自己写代码,要手写一堆复杂的makefi ...
- hdu 3518 (后缀数组)
题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). code: 后缀数组处理,对于得到height 进行查找... 参考http://blog.csdn.net/mis ...
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- TabControl控件的DrawItem事件怎么注册
只有DrawMode等于OwnerDrawFixed时,才会在绘制选项卡时发生DrawItem事件tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
- c#调用c++动态链接库的问题
1. Server Error in '/' Application. Object reference not set to an instance of an object. 这个问题是接口中的方 ...
- 求解,ASP.Net MVC Redirect 无法跳转问题
①JS代码 $.post("/Home/CheckLogin", { "username": $("#username").val(), & ...