HDU 1312 Red and Black(bfs)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
Sample Input
Sample Output
#include<cstdio>
#include<cstring> using namespace std;
int n,m,sx,sy;
char mp[][];
int vis[][];
int dx[]={,,,-};
int dy[]={,-,,};
bool check(int x,int y)
{
return x>=&&x<n&&y>=&&y<m;
}
struct node
{
int x,y;
}St[]; int bfs()//手写的队列,队列的尾端就是到达的点的数量
{
memset(vis,,sizeof vis);
int st=,en=;
vis[St[].x][St[].y]=;
while(st<en)
{
node e=St[st++];
for(int i=;i<;i++)
{
node w=e;
w.x=e.x+dx[i],w.y=e.y+dy[i];
if(check(w.x,w.y)&&vis[w.x][w.y]==&&mp[w.x][w.y]!='#')
{
vis[w.x][w.y]=;
St[en++]=w;
}
}
}
return en;
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF&&(n||m))
{
for(int i=;i<n;i++)
{
scanf("%s",mp[i]);
for(int j=;j<m;j++)
if(mp[i][j]=='@')
St[].x=i,St[].y=j;
}
printf("%d\n",bfs());
}
return ;
}
Red and Black
HDU 1312 Red and Black(bfs)的更多相关文章
- 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 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- 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(最简单也是最经典的搜索)
传送门: 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)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
- 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(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black (DFS & BFS)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
随机推荐
- h5 如何打包apk
1.需要下载安装MyEclipse2014,Android SDK,eclipse(需配置Android开发环境) Java和Android环境安装与配置. 2.打开MyEclipse2014,新建一 ...
- linux查找命令find
-1 linux的查找命令有两个: locate find locate:有一个索引库,故速度快,但是新加入的一般不再索引库中,故可能无法查到 find:搜索速度慢,但是功能及其强大,可以追加命令动作 ...
- linux下mysql忘记root密码的解决方案
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- jdbc 处理mysql procedure返回的多个结果集
1:测试数据库表user mysql> desc user$$ +-------+-------------+------+-----+---------+----------------+ | ...
- QTabWidget and QTabBar.的文字的颜色设置,三种方法
see the code after subclassingTabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFl ...
- Qt5中生成和使用静态库
在QT中静态库的后缀名为.a,在vs中开发的静态库后缀名为.lib.QT版本为5.2.1,系统为Windows. 一. 静态库的生成 新建项目. 新建一个静态库的项目,如图1.1所示:项目名称为tes ...
- Eclipse上GIT插件EGIT使用手册ᄃ
Eclipse上GIT插件EGIT使用手册 一_安装EGIT插件 http://download.eclipse.org/egit/updates/ 或者使用Eclipse Marketplace,搜 ...
- Linux系统编程(14)——shell常用命令
1. ls命令 ls命令是列出目录内容(ListDirectory Contents)的意思.运行它就是列出文件夹里的内容,可能是文件也可能是文件夹. "ls -l"命令已详情模式 ...
- Combinations 解答
Question Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. F ...
- hdu 1987-How many ways(dp)
解析:假设机器人在(x,y)这个点,能量为power,那么可以到达它右下角曼哈顿距离小于等于power的地方,再以该点为起点继续搜索. 代码如下: #include<cstdio> #in ...