HDU 5025图论之BFS
题意:从K走到T,S为怪,走的时候就多花费一秒,走到T时收集m把不同的钥匙。可是规定收集n之前,必须1~n-1所有收集完成,怪最多有5个
思路:怪最多就有5个,然后钥匙是1~9把,我们每一个点的状态就不会非常多,在BFS时每一个点的状态进行标记即可了。5个怪状态压缩着推断,由于这个怪在第二次经过的时候已经死了,不用花费时间去杀死它
#include <map>
#include <queue>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=110;
int sx,sy,ex,ey,n,m,cnt;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[maxn][maxn][10][40];
char str[maxn][maxn];
struct edge{
int x,y,step,numkey,ss;
};
struct snake{
int x,y;
}sna[10];
int bfs(){
queue<edge>que;
edge c,ne;
memset(vis,0,sizeof(vis));
c.x=sx,c.y=sy,c.step=0,c.numkey=0,c.ss=0;
vis[c.x][c.y][0][0]=1;
que.push(c);
int ans=inf;
while(!que.empty()){
c=que.front();que.pop();
if(c.x==ex&&c.y==ey&&c.numkey==m){
ans=min(ans,c.step);
}
for(int i=0;i<4;i++){
int xx=dir[i][0]+c.x;
int yy=dir[i][1]+c.y;
if(xx<0||xx>n-1||yy<0||yy>n-1||str[xx][yy]=='#') continue;
if(vis[xx][yy][c.numkey][c.ss]) continue;
ne.x=xx;ne.y=yy;ne.step=c.step+1;ne.numkey=c.numkey;ne.ss=c.ss;
if(str[xx][yy]>='1'&&str[xx][yy]<='9'){//假设走到的是钥匙的位置进行推断
int t=str[xx][yy]-'0';
if(ne.numkey==t-1){//钥匙刚好是当前钥匙数+1,就符合条件
ne.numkey++;
vis[ne.x][ne.y][ne.numkey][ne.ss]=1;
que.push(ne);
}else{//不符合直接压进队列
vis[ne.x][ne.y][ne.numkey][ne.ss]=1;
que.push(ne);
}
}else if(str[xx][yy]=='S'){//遇到怪进行推断
for(int i=0;i<cnt;i++){
if(xx==sna[i].x&&yy==sna[i].y){
if((ne.ss>>i)&1){//说明这个怪已经死掉了
vis[ne.x][ne.y][ne.numkey][ne.ss]=1;
que.push(ne);
}else{//没有死掉的话时间+1,状态更新
ne.step++;
ne.ss+=(1<<i);
vis[ne.x][ne.y][ne.numkey][ne.ss]=1;
que.push(ne);
}
break;
}
}
}else{
vis[ne.x][ne.y][ne.numkey][ne.ss]=1;
que.push(ne);
}
}
}
return ans;
}
int main(){
while(scanf("%d%d",&n,&m)!=-1){
if(n==0&&m==0) break;
cnt=0;
for(int i=0;i<n;i++) scanf("%s",str[i]);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(str[i][j]=='K')sx=i,sy=j;
if(str[i][j]=='T')ex=i,ey=j;
if(str[i][j]=='S'){
sna[cnt].x=i,
sna[cnt++].y=j;//记录怪的位置和数量
}
}
}
int ans=bfs();
if(ans==inf) puts("impossible");
else printf("%d\n",ans);
}
return 0;
}
HDU 5025图论之BFS的更多相关文章
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
- hdu 5025 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...
- HDU 5025 Saving Tang Monk 【状态压缩BFS】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- HDU 5025 Saving Tang Monk --BFS
题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...
- hdu 5025 Saving Tang Monk(bfs+状态压缩)
Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...
随机推荐
- JSTL-2
流程控制标签:if标签, choose标签, when标签, otherwise标签 <c:if>:的两种语法 1.<c:if test="" var=&qu ...
- struts2框架的大致处理流程
1,浏览器发送请求,例如请求 /mypage.action /report/myreport.pdf等. 2,核心控制器FilterDispatcher根据请求决定调用合适的Action. 3,Web ...
- HDU 1698 Just a Hook (线段树)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- Java后台直接生成二维码介绍
Java后台直接生成二维码 1.其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别.所以在这里介绍在后来生成二维码的方式. 2.不善于文字描述,直接上代码了. imp ...
- bzoj 3298: [USACO 2011Open]cow checkers -- 数学
3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec Memory Limit: 128 MB Description 一天,Besssie准备 ...
- lightoj 1381 - Scientific Experiment dp
1381 - Scientific Experiment Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lightoj.com/vo ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 【原】配置Log4j,使得MyBatis打印出SQL语句
[环境参数] JDK:jdk1.8.0_25 IDE:Eclipse Luna Servie Release 1 框架:Spring 4.1.5 + SpringMVC 4.1.5 + MyBatis ...
- ExtJS ComboBox同时加载远程和本地数据
ExtJS ComboBox同时加载远程和本地数据 原文:http://gblog.hbcf.net/index.php/archives/233 ComboBox比较特殊需求,将远程数据和本地数据同 ...
- 添加引用方式抛出和捕获干净的WebService异常
转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...