点击打开链接

题意:从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的更多相关文章

  1. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  2. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  3. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  4. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

  5. hdu 5025 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...

  6. 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 ...

  7. 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

    /* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...

  8. HDU 5025 Saving Tang Monk --BFS

    题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...

  9. hdu 5025 Saving Tang Monk(bfs+状态压缩)

    Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...

随机推荐

  1. BNUOJ 52503 Disdain Chain

    暴力,结论. 本打算写一发暴力,然后直接交答案,之后发现无论$n$等于多少,每种图都存在长度为$n$的路径,中间还一直以为自己暴力写错了. #include<bits/stdc++.h> ...

  2. Django 和 html

    下面是对应的形式,自定义的forms

  3. Python - 软件设计规范

    为什么要设计好目录结构? "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同学认为,这种个人风 ...

  4. 洛谷——P2071 座位安排 seat.cpp/c/pas

    P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...

  5. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  6. SpringBoot学习(四)

    spring boot 默认端口是 8080,如果想要进行更改的话,只需要修改 application.properties 文件,在配置文件中加入: 1. server.port=9090 其他常用 ...

  7. I/O 多路复用之select、poll、epoll详解

    select,poll,epoll都是IO多路复用的机制.I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但s ...

  8. 【尺取法好题】POJ2566-Bound Found

    [题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...

  9. 腾讯通消息webSDK踩坑

    1.腾讯通提供一个通过http协议的接口,可用于发送消息,公告等功能,要使用其功能首先要开启RTX_HTTPServer服务. 2.阅读文档http://rtx.tencent.com/sdk/,为了 ...

  10. python string和dict转换

    字典(dict)转为字符串(string) 我们可以比较容易的将字典(dict)类型转为字符串(string)类型. 通过遍历dict中的所有元素就可以实现字典到字符串的转换: for key, va ...