HDU 5025 Saving Tang Monk --BFS
题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐僧,集齐m个钥匙之前可以经过唐僧,集齐x个钥匙以前可以经过x+1,x+2..个钥匙,问最少多少步救到唐僧。
解法:BFS,每个节点维护四个值:
x,y : 当前坐标
key :已经集齐了key个钥匙
step:已经走了多少步
S : 蛇的访问状态 (2^5的数表示,某位为1表示已经杀过了)
然后把唐僧看做钥匙m+1,再加点优化:
为了避免超时,用一个全局的dis[x][y][key][S] 表示到(x,y),已经集齐到key个钥匙,蛇的访问状态为S时的最小步数,如果BFS扩展的时候,当前状态的步数>=dis[当前状态],那么就不再扩展下去了。
BFS中的逻辑就很简单了,看代码吧。
最后,枚举蛇的状态S,取dis[x][y][m+1][S]的最小值即为最小步数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
#define N 1000007 int dis[][][][],Stot,M;
struct node
{
int x,y,key,step,S;
};
int dx[] = {,,,-};
int dy[] = {,-,,};
map<pair<int,int>,int> snake;
char ss[][];
int n,m; bool OK(int nx,int ny)
{
if(nx < n && nx >= && ny < n && ny >= && ss[nx][ny] != '#')
return true;
return false;
} void bfs(node s)
{
queue<node> que;
que.push(s);
while(!que.empty())
{
node now = que.front();
que.pop();
int nx = now.x, ny = now.y;
int key = now.key, step = now.step;
int S = now.S;
node tmp;
for(int k=;k<;k++)
{
int kx = nx + dx[k];
int ky = ny + dy[k];
if(!OK(kx,ky)) continue;
tmp.x = kx,tmp.y = ky;
if(ss[kx][ky] == 'S') //蛇
{
int ind = snake[make_pair(kx,ky)]; //是第几条蛇
tmp.key = key;
if(S & (<<(ind-))) //如果已经杀死
{
tmp.S = S;
tmp.step = step+;
}
else //否则要杀
{
tmp.S = S|(<<(ind-));
tmp.step = step+;
}
if(tmp.step < dis[kx][ky][tmp.key][tmp.S])
{
dis[kx][ky][tmp.key][tmp.S] = tmp.step;
que.push(tmp);
}
}
else if(ss[kx][ky] >= '' && ss[kx][ky] <= '') //钥匙点
{
int num = ss[kx][ky] - '';
tmp.step = step+;
tmp.S = S;
if(num == key+) //正好是要拿的那个
tmp.key = key+;
else
tmp.key = key;
if(tmp.step < dis[kx][ky][tmp.key][tmp.S])
{
dis[kx][ky][tmp.key][tmp.S] = tmp.step;
que.push(tmp);
}
}
else if(ss[kx][ky] == '$') //唐僧这个点
{
tmp.key = key;
tmp.S = S;
tmp.step = step+;
if(M == key+) //已经集齐了所有钥匙,不再扩展,更新dis即可
dis[kx][ky][M][S] = min(dis[kx][ky][M][S],step+);
else //没有集齐,继续走
que.push(tmp);
}
else if(ss[kx][ky] == '.')
{
tmp.key = key;
tmp.S = S;
tmp.step = step+;
if(tmp.step < dis[kx][ky][tmp.key][tmp.S])
{
dis[kx][ky][tmp.key][tmp.S] = tmp.step;
que.push(tmp);
}
}
}
}
} int main()
{
int Sx,Ex,Sy,Ey;
int i,j;
while(scanf("%d%d",&n,&m)!=EOF && n+m)
{
if(n == )
{
puts("impossible");
continue;
}
snake.clear();
Stot = ;
M = m+;
for(i=;i<n;i++)
{
scanf("%s",ss[i]);
for(j=;j<n;j++)
{
if(ss[i][j] == 'K')
Sx = i,Sy = j, ss[i][j] = '.';
else if(ss[i][j] == 'T')
Ex = i,Ey = j, ss[i][j] = '$';
else if(ss[i][j] == 'S')
snake[make_pair(i,j)] = ++Stot;
}
}
node tmp;
tmp.x = Sx,tmp.y = Sy,tmp.key = ,tmp.step = ,tmp.S = ;
memset(dis,INF,sizeof(dis));
dis[Sx][Sy][][] = ;
bfs(tmp);
int mini = INF;
for(i=;i<(<<Stot);i++)
mini = min(mini,dis[Ex][Ey][M][i]);
if(mini == INF)
puts("impossible");
else
printf("%d\n",mini);
}
return ;
}
HDU 5025 Saving Tang Monk --BFS的更多相关文章
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- 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 ...
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5025 Saving Tang Monk(bfs+状态压缩)
Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- HDU 5025 Saving Tang Monk
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- HDU 5025 Saving Tang Monk(状态转移, 广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
随机推荐
- window10 mysql5.7 解压版 安装
1. 解压mysql-5.7.11-winx64.zip 到某文件夹, 如C:\DevelopCommon\mysql-5.7.11-winx64. 2. 配置环境变量 变量名 : MYSQL_HOM ...
- jdbcTemplate queryForObject 查询 结果集 数量
1.组织sql语句, 查询参数 数组, 设置返回类型 public int countByCondtion(String title, int mediaType, String currentSta ...
- Java中的继承
我们在以前的学习中,我们会了C#中的继承,今天我们来了解了解Java中的继承,其实都大同小异啦! 1.语法 修饰符 SubClass extends SuperClass(){ //类定义部分 } e ...
- Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]
前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...
- 常用SQL语句优化技巧
除了建立索引之外,保持良好的SQL语句编写习惯将会降低SQL性能问题发生. ①通过变量的方式来设置参数 好:stringsql = "select * from people p where ...
- 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法
几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...
- andriod 获得drawable下所有图片
package com.example.yanlei.my1; import android.app.AlertDialog; import android.content.Context; impo ...
- 滴滴DSRC抢楼大赛,十一快车券飞起来
十一马上就要到了 不管你想要去哪里 总少不了滴滴打车 DSRC为你准备了30份快车券 现在开始参与抢楼,就有机会得到 活动时间: 本帖发布之时起-9月30日12:00 参与方式: 关注微信公众号“滴滴 ...
- python多线程ssh爆破
python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...
- Burp Suite使用
Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登陆表单,执行会话令牌等多种的随机性检查.本文将做一个Bu ...