#include<bits/stdc++.h>
using namespace std;
const int maxN = ;
const int inf = 1e9 + ;
char G[maxN][maxN], snake[maxN][maxN];
int times[maxN][maxN][];
int n, m, sx, sy, ex, ey, ans;
int dir[][] = {{,},{,},{,-},{-,}}; struct node
{
int x, y, t, key, killed; //坐标, 步数, 已经拿到的钥匙, 有没有杀蛇
bool operator < (const node& p) const
{
return t > p.t;
}
node(int x, int y, int t, int key, int killed):x(x),y(y), t(t), key(key), killed(killed) {};
};
void bfs()
{
node t = (node)
{
sx, sy, , ,
};
times[sx][sy][] = ;
queue<node> q;
q.push(t);
while(!q.empty())
{
node u = q.front();
q.pop();
int x = u.x, y = u.y, key = u.key, t = u.t, killed = u.killed;
if(x == ex && y == ey && key == m)
{
ans = min(ans, t);
continue;
}
for(int d = ; d < ; d++)
{
int tx = x + dir[d][];
int ty = y + dir[d][];
if(tx < || tx >= n || ty < || ty >= n || G[tx][ty] == '#') //越界
continue; if(G[tx][ty] >= '' && G[tx][ty] <= '') //如果是钥匙
{
int num = G[tx][ty] - '';
if(key + == num && (times[tx][ty][key + ] > t + )) //可以拿
{
q.push(node(tx,ty,t+,key+,killed));
times[tx][ty][key + ] = t + ;
}
else if(((key + ) != num) && (times[tx][ty][key] > t + )) //不可以拿
{
q.push(node(tx,ty,t+,key,killed));
times[tx][ty][key] = t + ;
}
}
else if(G[tx][ty] >= 'A' && G[tx][ty] <= 'E') //蛇
{
int cnt = G[tx][ty] - 'A'; //压位判断 if((killed & (<<cnt)) == ) //未杀
{
if(times[tx][ty][key] > t + )
{
q.push(node(tx,ty,t+,key,(killed | (<<cnt))));
times[tx][ty][key] = t + ;
}
}
else //已经杀了
{
if(times[tx][ty][key] > t + )
{
q.push(node(tx,ty,t+,key,killed));
times[tx][ty][key] = t + ;
}
}
}
else if((G[tx][ty] == '.' || G[tx][ty] == 'T' || G[tx][ty] == 'K') && times[tx][ty][key] > t + )
{
q.push(node(tx,ty,t+,key,killed));
times[tx][ty][key] = t + ;
}
}
}
}
int main()
{
// freopen("1.txt","r", stdin);
while(~scanf("%d %d", &n, &m))
{
int cnt = ;
if(n == )
break;
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
for(int k = ; k < ; k++)
times[i][j][k] = inf;
for(int i = ; i < n; i++)
{
scanf("%s", G[i]);
}
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
{
if(G[i][j] == 'K')
sx = i, sy = j;
if(G[i][j] == 'T')
ex = i, ey = j;
if(G[i][j] == 'S')
{
G[i][j] = (cnt++ + 'A');
}
}
} ans = inf;
bfs();
if(ans == inf)
{
printf("impossible\n");
}
else
{
printf("%d\n", ans);
}
}
return ;
}

HDU 5025 Saving Tang Monk(状态转移, 广搜)的更多相关文章

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

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

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

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

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

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

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

  5. HDU 5025 Saving Tang Monk

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

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

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

  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 + 状压)

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

随机推荐

  1. GPIO的翻转操作方法

    STM32在进行IO翻转操作的时候可以使用以下方法:以PE.5为例 GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(1-(GPIO_ReadOutputDataB ...

  2. SQL生成日期维度(到小时)

    #建表语句: CREATE TABLE [dbo].[Dim_日期3]( ) NOT NULL, [年] [int] NULL, ) NULL, ) NULL, ) NULL, ) NULL, ) N ...

  3. Spark Mllib里如何将数据取消暂存在内存中(图文详解)

    不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集

  4. csrf攻击实例

    CSRF 攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执行在权限保护之下的操作.比如说,受害者 Bob 在银行有一笔存款,通过对银行的网站发送请求 ht ...

  5. .md文件插图片,不建议使用绝对地址。

    一 主要有两种方法,一种绝对地址,一种相对地址. 而百度的话,全都是说绝对地址的. 但是,有很大的弊端啊 orz 二 先说绝对地址 ![image](https://github.com/AngelS ...

  6. Spring Bean的一生

    Spring Bean的一生 When you work directly in Java, you can do anything you like with your objects and do ...

  7. JavaScript 函数(方法)

    1 定义 1.1 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 语法: 函数就是包裹在大括号中的代码块,前面使用了关键词 function function 方法名(参数列表){ 代码 ...

  8. Android ORM对象关系映射之GreenDAO建立多表关联

    https://blog.csdn.net/u010687392/article/details/48496299 利用GreenDAO可以非常方便的建立多张表之间的关联 一对一关联 通常我们在操作数 ...

  9. Logback文档(1)

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL030VCK2svQZU2rRyDR05KMh5

  10. MyLinkedList

    /** * 节点类 * @author JP * */ class Node { Object value;//节点元素值 Node pre;//上一个节点 Node next;//下一个节点 pub ...