Red and Black

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

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:  1241 1016 1010 1372 1242
 
这是我写搜索的第一题,dfs bfs都是在这一题上学会的。从今天开始我要开始搞搜索了,我必须全面提高自己的能力,所有的类型都得有个差不多,不然根本没法和他们讨论。
 

bfs & dfs

 #include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234
struct point
{
int x,y;
}st; int n,m,ans;
int dx[]={,,,-};
int dy[]={,-,,};
char mat[N][N];
int vis[N][N]; void bfs()
{
queue<point>q;
q.push(st);vis[st.x][st.y]=;
while(!q.empty())
{
point cur=q.front();
q.pop();
ans++;
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i];next.y+=dy[i];
if(next.x<||next.x>n||next.y<||next.y>m)continue;
if(mat[next.x][next.y]=='#'||vis[next.x][next.y]==)continue;
q.push(next);vis[next.x][next.y]=;
}
}
}
void dfs(int x,int y)
{
if(x<||x>n||y<||y>m)return;
if(mat[x][y]=='#'||vis[x][y]==)return;
vis[x][y]=;
ans++;
for(int i=;i<;i++)
dfs(x+dx[i],y+dy[i]);
} int main()
{
while(~scanf("%d%d",&m,&n)&&(m+n))
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf(" %c",&mat[i][j]);
if(mat[i][j]=='@')
{
st.x=i;st.y=j;
}
}
ans=;
bfs();
// dfs(st.x,st.y);
cout<<ans<<endl;
}
return ;
}
 
 

HDU 1312 Red and Black 第一题搜索!的更多相关文章

  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 DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 1,python初识

    什么是变量? 变量:将程序的中间结果暂时存储起来,以便后续程序调用. 什么是字符串类型? python中被引号引起来的数据就是字符串.字符串类型,也简称str类型. 在python中 int是什么? ...

  2. PYDay7&8-递归、冒泡算法、装饰器

    1.登录验证代码 1.1纯登录验证-函数实现 def login(username,password): ''' 用于用户名密码的验证 :param username: 用户名 :param pass ...

  3. 【06】GitHub WiKi

    [09]GitHub WiKi GitHub WiKi 能够帮助我们处理非结构化的页面集合,就像维基百科那样.我自己 NodeJS docs 就被我弄成 wiki 的样子. 几个页面,然后自定义侧边栏 ...

  4. OSPF 提升 三 type of Areas

    ospf  ccnp 三 上图中 rip域中的不连续的100条路由   在a1中导致LSDB太大      在保证网段的可达性的前提下   尽可能减少区域内路由器的lsdb       可以将a1设置 ...

  5. zoj2112 主席树动态第k大 ( 参考资料链接)

    参考链接: http://blog.csdn.net/acm_cxlove/article/details/8565309 http://www.cnblogs.com/Rlemon/archive/ ...

  6. sqlserver查询表大小

    IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...

  7. iOS大转盘抽奖

    功能 点击大转盘旋转后固定到某个自己可以确定的位置 结构 转盘,开始按钮,指针 技术 CADisplayLink不停重绘,CGAffineTransform旋转,简单数学公式 核心代码 1.使用CAD ...

  8. 刷题总结——小凸玩矩阵(scoi)

    题目: 题目背景 SCOI2015 DAY1 T1 题目描述 小凸和小方是好朋友,小方给了小凸一个 n×m(n≤m)的矩阵 A,并且要求小凸从矩阵中选出 n 个数,其中任意两个数都不能在同一行或者同一 ...

  9. LightOJ1106 Gone Fishing

    Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...

  10. MySQL与MSSQL的一些语法差异(持续更新中)

    分号不能少:分号不能少:分号不能少:重要的事情说3遍 Insert或者Update的数据包含反斜杠\的时候需要进行转义\\,例:insert into tablename(id,name) value ...