传送门:

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): 25397    Accepted Submission(s): 15306

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
 
Source
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1372 1242 1253 1240 1072 
 
分析:
只能上下左右四个方向走,问你可以走的块最多是多少?#不能走
小技巧:走过的地方字符就变为#
 
先用dfs写一下,有时间再用bfs写
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 25
char G[max_v][max_v];
int n,m;
int sx,sy;
int step;
int dir[][]={,,,,,-,-,};
void dfs(int x,int y)
{
int xx,yy;
for(int i=;i<;i++)
{
xx=x+dir[i][];
yy=y+dir[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&G[xx][yy]!='#')
{
step++;
G[xx][yy]='#';
dfs(xx,yy);
}
}
}
int main()
{
while(~scanf("%d %d",&m,&n))
{
if(n==&&m==)
break;
getchar();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>G[i][j];
if(G[i][j]=='@')
{
sx=i;
sy=j;
}
}
}
step=;
G[sx][sy]='#';
dfs(sx,sy);
cout<<step<<endl;
}
return ;
}
 

HDU 1312 Red and Black(最简单也是最经典的搜索)的更多相关文章

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

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

  2. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  3. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

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

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

  5. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  6. 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 ...

  7. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

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

  8. HDU 1312 Red and Black(bfs)

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

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

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

随机推荐

  1. 【ExtJS】自定义组件datetimefield(一)

    目的: ExtJS中提供了下拉日期选择控件Ext.form.field.Date与下拉时间选择控件Ext.form.field.Time.不过没有一个在选择日期时选择时间的控件datetimefiel ...

  2. java的访问修饰符

    Java中通过访问控制符(default,private,public,protected)来控制对类.变量.方法.构造方法的访问. 下表说明了4中修饰符的访问权限: 修饰符 当前类 同一包内 子孙类 ...

  3. go test遇到的一些问题-command-line-arguments undefined: xxxxx

    一 问题是在我写算法题的时候出的,test后缀的文件编译报command-line-arguments undefined: xxxxx 二 没记错,go test是 所有在以_test结尾的源码内以 ...

  4. Nginx反向代理与负载均衡[转]

    nginx启动和关闭(centos平台) /usr/local/nginx/sbin/nginx #启动 /usr/local/nginx/sbin/nginx -s reload #平滑启动 vi ...

  5. 自己用到的vim常用命令

    一.前言 这里整理的是我在实习期间用到的常用vim命令,特记录如下,以免忘记. 二.vim常用命令 1.vim中的光标移动 shift+6(^):跳到行首(第一个非空格字符)(注:在shell跳到行首 ...

  6. Python爬虫之requests模块(1)

    一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...

  7. 【代码笔记】XML深入学习:DTD约束与DTD语法(2)

    DTD语法之定义实体(了解即可) 实体分为一般实体和参数实体. 定义实体:定义变量 引用实体:使用变量 一般实体:定义实体在DTD中,实体引用在xml中. 参数实体:定义实体在DTD中,实体引用在DT ...

  8. 单链表的插入伪算法和用C语言创建单链表,并遍历

    非循环单链表插入结点伪算法讲解 q插入p之后的伪算法:第一种表示方法:r = p->pNext; // p->pNext表示的是所指向结点的指针域,指针域又是指向下一个结点的地址p-> ...

  9. Selenium2学习(十三)-- JS处理滚动条

    前言 selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候 ...

  10. 学习笔记-java 多线程

    背景说明: 多线程并发与管理,是java基础知识里的重点,本文根据<java核心技术第八版>中的多线程技术的学习,对知识点进行整理:这里只对基础知识点进行简单罗列,以达到对知识点有网状关联 ...