HDU4528+BFS
/*
bfs+标记状态
如何记录状态是关键!!
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
using namespace std;
const int maxn = ;
const int inf = ;
char mat[ maxn ][ maxn ];
int vis[ maxn ][ maxn ][ ][ ];
const int dx[]={,-,,};
const int dy[]={,,,-};
struct Pos{
int x,y;
};
struct Node{
int x,y,ti;
int D,E;//flag
};
Pos D,S,E;
void init(){
for( int i=;i<maxn;i++ )
for( int j=;j<maxn;j++ )
mat[i][j] = 'X';
} bool in( Node p,int n,int m ){
if( p.x>=&&p.x<n&&p.y>=&&p.y<m )
return true;
else
return false;
}
bool Judge1( Node tmp ){
int x = tmp.x;
int y1 = min( tmp.y,D.y );
int y2 = max( tmp.y,D.y );
for( int i=y1+;i<y2;i++ ){
if( mat[x][i]=='X' ) return false;
else if( mat[x][i]=='.' ){
if( x==D.x&&i==D.y ) return false;
else if( x==E.x&&i==E.y ) return false;
}
}
return true;
}
bool Judge2( Node tmp ){
int x = tmp.x;
int y1 = min( tmp.y,E.y );
int y2 = max( tmp.y,E.y );
for( int i=y1+;i<y2;i++ ){
if( mat[x][i]=='X' ) return false;
else if( mat[x][i]=='.' ){
if( x==D.x&&i==D.y ) return false;
else if( x==E.x&&i==E.y ) return false;
}
}
return true;
}
bool Judge3( Node tmp ){
int y = tmp.y;
int x1 = min( tmp.x,D.x );
int x2 = max( tmp.x,D.x );
for( int i=x1+;i<x2;i++ ){
if( mat[i][y]=='X' ) return false;
else if( mat[i][y]=='.' ){
if( i==D.x&&y==D.y ) return false;
else if( i==E.x&&y==E.y ) return false;
}
}
return true;
}
bool Judge4( Node tmp ){
int y = tmp.y;
int x1 = min( tmp.x,E.x );
int x2 = max( tmp.x,E.x );
for( int i=x1+;i<x2;i++ ){
if( mat[i][y]=='X' ) return false;
else if( mat[i][y]=='.' ){
if( i==D.x&&y==D.y ) return false;
else if( i==E.x&&y==E.y ) return false;
}
}
return true;
} int bfs( int n,int m,int aim_ti ){
Node cur,nxt;
cur.x = S.x;
cur.y = S.y;
cur.ti = ;
cur.D = cur.E = ;
queue<Node>q;
while( !q.empty() )
q.pop();
q.push( cur );
int ans = inf;
vis[ cur.x ][ cur.y ][ cur.D ][ cur.E ] = ;
while( !q.empty() ){
cur = q.front();
q.pop();
if( cur.ti>aim_ti ) continue;
//printf("cur:x=%d,y=%d,ti=%d\n",cur.x,cur.y,cur.ti);
if( cur.x==D.x&&Judge1( cur )==true ) cur.D = ;
if( cur.x==E.x&&Judge2( cur )==true ) cur.E = ;
if( cur.y==D.y&&Judge3( cur )==true ) cur.D = ;
if( cur.y==E.y&&Judge4( cur )==true ) cur.E = ;
if( cur.D== ) {
if( cur.E== ){
if( ans>cur.ti ){
ans = cur.ti;
}
}
}
//printf("ans:%d\n",ans);
for( int i=;i<;i++ ){
nxt = cur;
nxt.x+=dx[i];
nxt.y+=dy[i];
nxt.ti++;
if( in( nxt,n,m )==true&&mat[nxt.x][nxt.y]!='X'&&vis[nxt.x][nxt.y][nxt.D][nxt.E]== ){
vis[nxt.x][nxt.y][nxt.D][nxt.E] = ;
q.push(nxt);
}
}
}
if( ans>aim_ti ) return -;
else return ans;
} int main(){
int ca,T;
scanf("%d",&ca);
T = ;
while( ca-- ){
int n,m,aim_ti;
printf("Case %d:\n",T++);
init();
scanf("%d%d%d",&n,&m,&aim_ti);
for( int i=;i<n;i++ ){
scanf("%s",mat[i]);
for( int j=;j<m;j++ ){
if( mat[i][j]=='D' ){
D.x = i;
D.y = j;
mat[i][j]='X';
}
else if( mat[i][j]=='S' ){
S.x = i;
S.y = j;
mat[i][j]='.';
}
else if( mat[i][j]=='E' ){
E.x = i;
E.y = j;
mat[i][j]='X';
}
}
}
memset( vis,,sizeof( vis ) );
int ans = bfs( n,m,aim_ti );
printf("%d\n",ans);
}
return ;
}
HDU4528+BFS的更多相关文章
- HDU-4528 小明系列故事——捉迷藏 BFS模拟
题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...
- hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解
思路: 一道BFS题,和以前的BFS有点不同,这里的vis数组需要记录每次走时的状态,所以开了3维,只对该状态下的vis修改. 注意坑点:S的位置是可以走的 代码: #include<queue ...
- HDU4528 小明捉迷藏 [搜索-BFS]
一.题意 小明S在迷宫n*m中找大明D和二明E,障碍物X不能走,问你计算是否能在时间t内找到大明和二明 二.分析 2.1与普通的BFS不同,这里可以走回头路,这里应该建立四维的标记数组标记数组,例如v ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
随机推荐
- requirejs 定义模块中含有prototype
因为我对requirejs不熟悉,不清楚如何定义带有prototype的模块, 在看了:https://gist.github.com/jonnyreeves/2474026 的demo之后,就明白了 ...
- mysql datetime 排序
在项目里面,使用mysql datetime desc,看见别人使用UNIX_TIMESTAMP(datetime) desc,就用了 datetime进行比较,使用UNIX_TIMESTAMP()进 ...
- (译文)12个简单(但强大)的JavaScript技巧(二)
原文链接: 12 Simple (Yet Powerful) JavaScript Tips 其他链接: (译文)12个简单(但强大)的JavaScript技巧(一) 强大的立即调用函数表达式 (什么 ...
- Exchanger, Changing data between concurrent tasks
The Java concurrency API provides a synchronization utility that allows the interchange of data betw ...
- 工作“触雷”经历与总结--记博弈论的应用
工作三年,职场受挫.一些值得说或者不值得说的事情,也懒得去记录.无奈,更多时无奈.内心的骄傲或者自负也不值得炫耀.天生骄傲,或者也只是自身内心的呐喊.毕竟,骄傲的人也不会说出来,搞的好像是有点似得. ...
- 用PL0语言求Fibonacci数列前m个中偶数位的数
程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...
- FreeMarker-Built-ins for numbers
http://freemarker.org/docs/ref_builtins_number.html#topic.extendedJavaDecimalFormat Page Contents ab ...
- springmvc学习(五)——处理模型数据
Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据Map 及 Model: 入参 ...
- 学习C++ Primer 的个人理解(十一)
关联容器 就像是个字典, 其元素是 键 - 值 对. 关键字起到索引作用. 有序: map:关联数组:保存 健-值 对 set : 关键字既是值. multimap : 关键字可重复出现的map mu ...
- JavaScript中的apply和call函数详解
本文是翻译Function.apply and Function.call in JavaScript,希望对大家有所帮助 转自“http://www.jb51.net/article/52416.h ...