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. BZOJ 1831: [AHOI2008]逆序对

    题目大意: 给出一个序列,有几个位置上的数字任意.求最小的逆序对数. 题解: 自己决定放置的数一定是单调不降的.不然把任意两个交换一下就能证明一定会增加逆序对. 然后就可以DP了,f[i][j]表示第 ...

  2. 图论trainning-part-1 E. Invitation Cards

    E. Invitation Cards Time Limit: 8000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Ja ...

  3. 长沙理工大学第十二届ACM大赛-重现赛

    年轮广场 时间限制:1秒 空间限制:131072K 题目描述 在云塘校区,有一个很适合晒太阳的地方————年轮广场 年轮广场可以看成n个位置顺时针围成一个环. 这天,天气非常好,Mathon带着他的小 ...

  4. 【C#】C#数据类型和VB的区别

    导读:看完了C#,需要总结的有很多东西.开始没有怎么在意,根本没有意识到,那些视频是教给了我一种新的编程语言,我就真的是像看电视剧一样的看完了.猛然想起了学过的VB,这是目前为止,我接触到的仅有的语言 ...

  5. 九度oj 题目1131:合唱队形

    题目描述: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1, 2, …, K,他们 ...

  6. 怎么样给CentOS6.5增加swap分区

    再给服务器添加zabbix监控的时候,发现服务器有个报错“Lack of free swap space on localhost”,通过查找得知,在安装服务器的时候忘了划分swap分区.为了减少报错 ...

  7. 性能测试之五--webservices接口测试

    下面我们进行webservices接口的讲解,包括脚本生成,参数化和关联. 以天气预报的接口为例,接口地址为: http://ws.webxml.com.cn/WebServices/WeatherW ...

  8. 【Luogu】P3865ST表模板(ST表)

    题目链接 本来准备自己yy一个倍增来着,然而一看要求O1查询就怂了. ST表模板.放上代码. #include<cstdio> #include<cstdlib> #inclu ...

  9. JMS API学习总结(一)

    三.JMS API简析 顶级接口 P2P Pub/sub 备注 ConnectionFactory QueueConnectionFactory TopicConnectionFactory 基于工厂 ...

  10. msp430项目编程41

    msp430综合项目---红外遥控直流电机调速系统41