/*
这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态!
以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到!
每种钥匙有 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. 通过rinetd实现端口转发来访问内网的服务

    通过rinetd实现端口转发来访问内网的服务 一.   问题描述 通过外网来访问内网的服务 二.   环境要求 需要有一台能够外网访问的机器做端口映射,通过数据包转发来实现外部访问阿里云的内网服务 三 ...

  2. adb uninstall

    adb shell pm list packages adb uninstall com.pa.pfac

  3. ngnix编译遇到的问题.

    总结:先后遇到libz库文件没有正确的链接和pcre库文件没有正确的链接 1./configure后提示需要zlib 2.locate zlib,系统中没有zlib的共享库so文件,但是有一些头文件, ...

  4. Hadoop学习篇1 快速入门

    Hadoop是Apache Lucene创始人Doug Cutting创建的,Hadoop起源于Apache Nutch,一个开源的网络搜索引擎.最先引起注意是2003年google的一篇论文,该论文 ...

  5. TNSNAMES.ORA 配置

    上面的sqlnet.ora文件说明:SQLNET.AUTHENTICATION_SERVICES= (NTS)——这个表示采用os认证,在数据库服务器上,可以利用sqlplus “/ as sysdb ...

  6. JS图片切换效果

    源地址:http://www.codefans.net/jscss/code/4699.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  7. 菜鸟译文(三)——JDK6和JDK7中substring()方法的对比

    substring(int beginIndex, int endIndex)方法在JDK6和JDK7中是不同的.了解他们的区别可以让我们更好的使用这个方法.方便起见,以下用substring() 代 ...

  8. java代写

    Computer Science, Claremont McKenna CollegeCS51.2 - Introduction to Computer Science, Fall 2014Probl ...

  9. Cocos2dx使用wxsqlite开源加密SQLite3数据库

    最近使用wxsqlite加密sqlite3数据库,刚开始折腾好几天,在xcode上一直编译不通过,后来在sqlite3.c找到配置,编译顺利通过,太激动了,哈哈,废话少说!总结一下android和io ...

  10. iOS杂谈-我为什么不用Interface builder

    在互联网上关于Interface Builder的争吵每天都在发生,用和不用大家都有一大堆的理由.最近看了这篇文章,很多地方和作者有共鸣,结合自己的一些经历,就有了你现在所看到的东西,你可以把它当成前 ...