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....#......#............... ...
随机推荐
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第三篇:排序.筛选和分页 原文:Sort ...
- 关于jQuery源码分析
http://www.w3ctech.com/topic/256 jQuery源码剖析(一)——概览&工具方法
- REST 架构风格
目前基于网络应用的架构风格主要有三种: RPC架构风格 将服务器看作是由一些过程组成,客户端调用这些过程来执行特定的任务.SOAP就是RPC风格的一种架构.过程是动词性的(做某件事),因此RPC建 ...
- 程序设计入门——C语言 第3周编程练习 2 数字特征值(5分)
2 数字特征值(5分) 题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作 ...
- CSS 3 颜色表示法
1,Color Name:red 优点:方便记忆 缺点:数量少不支持透明度 2,HEX方式:#FFFFFF 优点:颜色种类多 缺点:换算复杂需要借助工具 3,RGB方式:rgb(25 ...
- 数组的sort方法
今天在看<JavaScript高级程序设计第三版>时,学到了数组的sort方法.知道这个方法,但是一直没仔细研究过,这次发现,它是把数组内的值用toSting()变为字符串再进行比较,这样 ...
- SPSS常用基础操作(2)——连续变量离散化
首先说一下什么是离散化以及连续变量离散化的必要性. 离散化是把无限空间中无限的个体映射到有限的空间中去,通俗点讲就是把连续型数据切分为若干“段”,也称bin,离散化在数据分析中特别是数据挖掘中被普遍采 ...
- 工作中使用的html5和css3 新特性
1.placeholder <input type="text" placeholder="请输入手机号码" class="phone" ...
- angular router-ui
将模块注入到控制器中的方法: 1.export module 2.在router中resolve解决: 2.1 resolve中直接return值 /*ngInject*/ worker : 'hi' ...
- C#高级进阶--重写函数
这里要说一下,重写是继承时发生,在子类中重新定义父类中的方法.例如:基类方法声明为virtual方法,派生类中使用override声明此方法的重写. 基类中的访问修饰符在子类中是不能被修改的.比如说基 ...