/*
这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态!
以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到!
每种钥匙有 k 种, 每一种有多个! 只要求找到每一种的其中一个就可以!
找钥匙的顺序按照 第1种, 第2种, 第3种 ....第k种!
找钥匙的时间是一步, 走到相邻空地的时间是一步, 打蛇的时间就是两步!
求找到师傅的最少步数! 这里说一下 state[N][N][10][35]表示的含义: ->state[x][y][i][j]
前两维就不用说了,就是地图上的坐标, 第三维表示的是当前找到第几把钥匙
第四维表示的沿着一条路径走到 (x, y)找到 第 i 把钥匙打掉了哪几条蛇!
将 j 拆分成 二进制, 从右往左数, 如果第 k 为是1, 表示第 k 条 蛇杀掉了!
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue> #define N 105
using namespace std; char mp[][]; bool state[N][N][][]; int bx, by; struct node{
int x, y;
int numk, snk;
int step;
node(){} node(int x, int y, int numk, int snk, int step){
this->x = x;
this->y = y;
this->numk = numk;
this->snk = snk;
this->step = step;
}
}; int n, m;
int dir[][] = {, , , , -, , , -};
bool operator < (node a, node b) {
return a.step > b.step;
} priority_queue<node>q; bool bfs(){
while(!q.empty()) q.pop();
memset(state, false, sizeof(state));
q.push( node(bx, by, , , ) );
state[bx][by][][] = true;
while( !q.empty() ) {
node cur = q.top();
q.pop();
for(int i=; i<; ++i){
int x = cur.x + dir[i][];
int y = cur.y + dir[i][];
if(x< || x>n || y< || y>n || mp[x][y]=='#') continue;
int numk = cur.numk, snk = cur.snk, step = cur.step;
if(mp[x][y] == '.')
step += ;
else if( mp[x][y] >= '' && mp[x][y] <= ''){
if( numk + == mp[x][y] - '' )
numk += ;
step += ;
}
else if( mp[x][y] >= 'A' && mp[x][y] <= 'E' ){//这一步是关键
int cnt = mp[x][y] - 'A' + ;
if( ( << (cnt-) ) & snk ) step += ;//如果这一条蛇已经被打过了,也就是一条路又折回到这一个点
else{//在这一条路上,这条蛇没有被打过,那就将它打掉,并标记这条蛇打过了!
step += ;
snk ^= ( << (cnt-) );
}
}
else if( mp[x][y] == 'T' && numk == m ){
printf("%d\n", step + );
return true;
}
else step += ; if( state[x][y][numk][snk] ) continue;
state[x][y][numk][snk] = true;
q.push( node(x, y, numk, snk, step) );
}
}
return false;
} int main(){
while( scanf("%d%d", &n, &m) && (n ||m) ) {
int cntS = ;
for(int i = ; i <= n; ++ i){
scanf("%s", mp[i] + );
for(int j = ; j <=n; ++ j)
if(mp[i][j] == 'K'){
bx = i;
by = j;
}
else if(mp[i][j] == 'S')
mp[i][j] = 'A' + cntS++;
}
if( !bfs() ) printf("impossible\n");
}
return ;
}

2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)的更多相关文章

  1. 2014 网选 广州赛区 hdu 5023 A Corrupt Mayor's Performance Art

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #d ...

  2. HDU 5025 Saving Tang Monk --BFS

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

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

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

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

  5. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

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

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

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

  8. HDU 5025 Saving Tang Monk

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

  9. 2014 网选 上海赛区 hdu 5047 Sawtooth

    题意:求n个'M'型的折线将一个平面分成的最多的面数! 思路:我们都知道n条直线将一个平面分成的最多平面数是 An = An-1 + n+1 也就是f(n) = (n*n + n +2)/2 对于一个 ...

随机推荐

  1. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  2. Liferay7 BPM门户开发之26: 集成Activiti到Liferay7

    开发顺序: 实战任务1,开发BPM管理后台(用于在Liferay管理中心管理Activiti模型上传) 一个熟悉Portlet操作的项目,为开发打好基础. http://www.cnblogs.com ...

  3. 正则表达式提取url中的参数,返回json字符串

    var urlstr = "www.baidu.com?a=1&b=xx&c"; var s = urlstr.split("?"); var ...

  4. Ubuntu “无法获得锁”解决方案(E: 无法获得锁 /var/cache/apt/archive

    Ubuntu “无法获得锁”解决方案(E: 无法获得锁 /var/cache/apt/archive   现象一: E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源 ...

  5. 用Wireshark提取WPA握手包

    进入正文前,先来看一张截图,如图1,使用“aircrack-ng wpa.cap -w password.lst”命令后,程序会提示输入待破解网络的序号,此时只要提供一个序号即可.注意:1:命令中不需 ...

  6. Asp.net WebApi + EF 单元测试架构 DbContext一站到底

    其实关于webapi和Ef service的单元测试我以前已经写过相关文章,大家可以参考: Asp.net WebAPI 单元测试 单元测试 mock EF 中DbContext 和DbSet Inc ...

  7. spring-boot 加载本地静态资源文件路径配置

    1.spring boot默认加载文件的路径是 /META-INF/resources/ /resources/ /static/ /public/ 这些目录下面, 当然我们也可以从spring bo ...

  8. Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法

    有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...

  9. 跨平台web调试代理工具---whistle

    whistle是基于Node实现的跨平台web调试代理工具,支持windows.mac.linux等所有安装了Node的操作系统,可以部署在本地机器.虚拟机或远程服务器,并通过本地网页查看或修改HTT ...

  10. 高大上技术之sql解析

    Question: 为何sql解析和高大上有关系?Answer:因为数据库永远都是系统的核心,CRUD如此深入码农的内心...如果能把CRUD改造成高大上技术,如此不是造福嘛... CRUD就是Cre ...