/*
这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态!
以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到!
每种钥匙有 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. 虚拟机下samba简单安装配置

    系统是Win7 虚拟机是CenterOS6.5 1.关闭防火墙以及关闭SELINUX的强制模式(重要): service iptables stop//关闭防火墙 setenforce 0 //关闭S ...

  2. Comet技术详解:基于HTTP长连接的Web端实时通信技术

    前言 一般来说,Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Ser ...

  3. gearman mysql udf

    gearman安装 apt-get install gearman gearman-server libgearman-dev 配置bindip /etc/defalut/gearman-job-se ...

  4. 使用Git Bash for Windows

    本篇体验Git Bash在Windows操作系统上的用法. 什么是Bash? 是一个Shell环境,Bourne Again Shell的缩写. 安装git for windows → http:// ...

  5. CoreOS实践(2)—在coreos上安装Kubernetes

    下载kubernetes sudo mkdir -p /opt/bin sudo wget https://storage.googleapis.com/kubernetes/binaries.tar ...

  6. CoreOS实践(1)—CoreOS初体验

    CoreOS主要包含以下一些东西: (1)最小的OS:kernel+systemd (2)使用Docker运行应用 (3)使用fleet管理集群 (4)使用etcd实现服务发现:一个分布式的K/V存储 ...

  7. android CheckBox的运用

    CheckBox定义一个同意协议的按钮,只要同意button才可以点击 XML代码 <CheckBox android:id="@+id/checkbox1" android ...

  8. C#数据库绑定

    .Net对数据库的绑定 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  9. quick -- 添加按钮

    cc.ui.UIPushButton.new({ normal = "comm_btnGreenBackBack.png", pressed = "comm_btnGre ...

  10. PMBOK/CMM/CMMI/OPM3

    1968年为了解决大型软件项目的软件危机,北大西洋公约组织(NATO)提出了“软件工程”这一术语,以改进软件开发设计过程. 1969年美国项目管理协会(PMI)组织成立,从1981年起经过30年的努力 ...