/*
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的更多相关文章

  1. HDU-4528 小明系列故事——捉迷藏 BFS模拟

    题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...

  2. hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解

    思路: 一道BFS题,和以前的BFS有点不同,这里的vis数组需要记录每次走时的状态,所以开了3维,只对该状态下的vis修改. 注意坑点:S的位置是可以走的 代码: #include<queue ...

  3. HDU4528 小明捉迷藏 [搜索-BFS]

    一.题意 小明S在迷宫n*m中找大明D和二明E,障碍物X不能走,问你计算是否能在时间t内找到大明和二明 二.分析 2.1与普通的BFS不同,这里可以走回头路,这里应该建立四维的标记数组标记数组,例如v ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  6. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  7. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  8. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  9. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

随机推荐

  1. 【JDK源码系列】ConcurrentHashMap

    并发永远是高性能的话题,而并发容器又是java中重要的并发工具,所以今天我们来分析一下Concurrent包中ConcurrentHashMap(以下简称Chashmap).普通容器在某些并发情况下的 ...

  2. apache ambari web页面无法访问解决办法

    ambari-server启动成功,但是页面无法访问 作者:Bo liang链接:http://www.zhihu.com/question/34405898/answer/115001510来源:知 ...

  3. php里ezpdo orm框架初探

    http://jackyrong.iteye.com/blog/238930 http://www.oschina.net/project/tag/126/orm?sort=view&lang ...

  4. EasyUI的DataGrid 分页栏英文改中文解决方案

    (一)分页栏英文改中文解决方案 这个问题其实很简单,就是引入文件jquery-easyui-1.3/locale/easyui-lang-zh_CN.js . 注意这个文件要放在本页js的后面,放在最 ...

  5. 【开发】Dialog 对话框

    提示:Dialog 继承自 Panel,有大量的方法在 Panel 中已被定义,可以复用. Dialog API:http://www.jeasyui.net/plugins/181.html Pan ...

  6. 简化版的Flappy Bird开发过程(不使用第三方框架)

    目录 .1构造世界 .2在世界中添加元素 .3碰撞检测 .4添加动画特效 .5总结 .0 开始之前 之前曾经用Html5/JavaScript/CSS实现过2048,用Cocos2d-html5/Ch ...

  7. [LCA & RMQ] [NOIP2013] 货车运输

    首先看到这题, 由于要最大, 肯定是求最大生成树 那么 o(n2) dfs 求任意点对之间的最小边是可以想到的 但是看看数据范围肯定TLE 于是暴力出来咯, 不过要注意query的时候判断的时候要 m ...

  8. mysql数据库之索引和分析索引

    分析查询语句是否用到了索引 explain sql语句\G //根据返回的信息,我们可知,该sql语句是否使用索引,从多少记录中取出,可以看到排序的方式. 主要是看 key 实际用到的索引 rows ...

  9. 【HeadFirst设计模式】12.复合模式

    定义: 复合模式结合两个或以上的模式,组成一个解决方案,解决一再发生的一般性问题. 要点: MVC模式是复合模式,结合了观察者模式.策略模式和组合模式. 模型使用了观察者模式,以便观察者更新,同时保存 ...

  10. 阿里云ECS服务器被DDoS无解,请问我该何去何从?

    阿里云ECS服务器被DDoS无解,请问我该何去何从?