hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解
思路:
一道BFS题,和以前的BFS有点不同,这里的vis数组需要记录每次走时的状态,所以开了3维,只对该状态下的vis修改。
注意坑点:S的位置是可以走的
代码:
#include<queue>
#include<cstdio>
#define ll long long
using namespace std;
const int N = 100+5;
char mp[N][N];
bool vis[4][N][N];
int X1,Y1,X2,Y2,x,y,n,m,t,to[4][2] = {0,1,0,-1,1,0,-1,0},cnt = 1;
struct node{
int x,y;
int step;
int Find;
};
void update(node &a){
if(a.x == X1 && a.Find != 1){ //等于1不改变状态
int s = min(a.y,Y1) + 1;
int e = max(a.y,Y1) - 1;
int flag = 1;
for(int i = s;i <= e;i++){
if(mp[X1][i] != '.'){
flag = 0;
break;
}
}
if(flag) a.Find = (a.Find == 0? 1 : 3);
}
if(a.x == X2 && a.Find != 2){
int s = min(a.y,Y2) + 1;
int e = max(a.y,Y2) - 1;
int flag = 1;
for(int i = s;i <= e;i++){
if(mp[X2][i] != '.'){
flag = 0;
break;
}
}
if(flag) a.Find = (a.Find == 0? 2 : 3);
}
if(a.y == Y1 && a.Find != 1){
int s = min(a.x,X1) + 1;
int e = max(a.x,X1) - 1;
int flag = 1;
for(int i = s;i <= e;i++){
if(mp[i][Y1] != '.'){
flag = 0;
break;
}
}
if(flag) a.Find = (a.Find == 0? 1 : 3);
}
if(a.y == Y2 && a.Find != 2){
int s = min(a.x,X2) + 1;
int e = max(a.x,X2) - 1;
int flag = 1;
for(int i = s;i <= e;i++){
if(mp[i][Y2] != '.'){
flag = 0;
break;
}
}
if(flag) a.Find = (a.Find == 0? 2 : 3);
}
}
void BFS(){
queue<node> q;
while(!q.empty()) q.pop();
node a,b;
a.x = x,a.y = y,a.Find = 0,a.step = 0;
update(a);
q.push(a);
while(!q.empty()){
a = q.front();q.pop();
if(a.Find == 3 && a.step <= t){
printf("Case %d:\n%d\n",cnt++,a.step);
return;
}
for(int i = 0;i < 4;i++){
b.x = a.x + to[i][0];
b.y = a.y + to[i][1];
if(b.x < 1 || b.y < 1 || b.x > n || b.y > m) continue;
if(mp[b.x][b.y] != '.') continue;
b.Find = a.Find;
update(b);
if(vis[b.Find][b.x][b.y]) continue;
vis[b.Find][b.x][b.y] = true;
b.step = a.step + 1;
q.push(b);
}
}
printf("Case %d:\n-1\n",cnt++);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&t);
for(int i = 1;i <= n;i++){
scanf("%s",mp[i] + 1);
for(int j = 1;j <= m;j++){
if(mp[i][j] == 'D') X1 = i,Y1 = j;
else if(mp[i][j] == 'E') X2 = i,Y2 = j;
else if(mp[i][j] == 'S') x = i,y = j,mp[i][j] = '.';
}
}
memset(vis,0,sizeof(vis));
BFS();
}
return 0;
}
hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解的更多相关文章
- hdu4528 小明系列故事——捉迷藏
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s ...
- HDU-4528 小明系列故事——捉迷藏 BFS模拟
题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- HDU 4528 BFS 小明系列故事——捉迷藏
原题直通车:HDU 4528 小明系列故事——捉迷藏 分析: 标记时加两种状态就行. 代码: #include<iostream> #include<cstring> #inc ...
- HDU 4828 小明系列故事——捉迷藏
漂亮妹子点击就送:http://acm.hdu.edu.cn/showproblem.php?pid=4528 Time Limit: 500/200 MS (Java/Others) Memo ...
- HDU 4528 小明系列故事――捉迷藏
广搜. 根据题意,可以知道状态总共有$4*n*m$种.每一个位置四种状态:两个都没有发现:发现$E$没发现$D$:发现$D$没发现$E$:两个都发现. 每次移动的花费都是$1$,队列里面状态的费用是单 ...
- HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)
小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- 敌兵布阵---hud1166(线段树或者树状数组模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 线段树中对某一点的值进行改变: #include<iostream> #includ ...
- Git 安装及使用小结
Git 安装及使用小结 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; outline ...
- web 开发常见问题--GET POST 区别
首先,get和post是什么? --两种 HTTP 请求方法:GET 和 POST HTTP Request Methods GET.POST 专业名称是 HTTP Request Methods.但 ...
- mysql 数据库的操作
1.数据库的查看 1)查看mysql中所有的数据库 "show databases;" mysql> show databases; +---------------- ...
- 启动rabbitmq,提示ERROR: node with name "rabbit" already running on "localhost"
➜ ~ rabbitmq-server ERROR: node with name "rabbit" already running on "localhost" ...
- XenServer:使用XenCenter开设VPS(多图完整版)
打铁要趁热,咱们接着来玩XenServer.昨天赵容用机房提供的KVM给服务器装了XenServer,今天我们来玩更有意思的:开小鸡.装好XenServer之后,访问我们的服务器IP,就可以看到Xen ...
- iOS常用第三方类库及Xcode插件
第三方类库(github地址): 1.AFNetworking 网络数据 https://github.com/AFNetworking/AFNetworking 2.SDWebImage 图 ...
- jquery 实现iframe 自适应高度
转自: http://www.cnblogs.com/luluping/archive/2009/04/17/1437843.html 超级简单的方法,也不用写什么判断浏览器高度.宽度啥的.下面的两种 ...
- BootStrap同时显示多个Modal解决方案
使用BootStrap自带的Modal的时候,如果同时调用多个Modal,那么只能看到背景颜色加深但是看不见新的Modal页面. 问题主要是Modal的z-index有问题,重新计算z-index并赋 ...
- VS2010/MFC编程入门之三十六(工具栏:工具栏资源及CToolBar类)
上一节中鸡啄米讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的上部,菜单栏的下方,由一些带图片的按钮组成.当用户用鼠标单击工具栏上某个按钮时,程 ...