#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. 点权生成树(gentree)

    点权生成树(gentree) 题目背景 Awson是某国际学校信竞组的一只菜鸡.终于弄明白边权最小生成树后,然而又被大神嘲笑了.大神深邃的眼光中透露了些睿智,说道:“你会求点权最小生成树么?”Awso ...

  2. JAVA基础之File类

    个人理解: File是个文件类,可以用其增加.删除.查找某种类型的文件或者文件夹,同时根据其成员变量的特点可以综合利用,避免出现跨系统的时候出现错误,并且查找时最好输入绝对路径,以免出现不存在的文件. ...

  3. Eclipse 主题(Theme)配置

    < 程序员大牛必备的装逼神器 > 一个牛逼的程序员,除了有牛逼的技术,还要有高逼格的风格,说白了,就和人一样,单是内在美还不行,必须外表也要美,就好比,一个乞丐,他内在美,但是全身臭气熏天 ...

  4. vue2.0:(五)、路由vue-router

    好的,接下来,我们来写路由.用的是vue2.0的路由. 步骤一:配置main.js import Vue from 'vue'; import App from './App'; import rou ...

  5. ABAP事件的简单用法

    1.1.事件: 用于捕获某类对象状态的改变来触发事件的方法,并进行处理 1.2.定义:可以在类或接口中进行声明 EVENTS|CLASS-EVENTS evt  EXPORTING … VALUE(p ...

  6. js的加密和解密

    最近在研究js的加密和解密的问题,上网上搜出来很多方法,不过不知道到底哪一个会比较管用.这里是今天找到的一些关于base64加密解密的js代码,已经经过试验,可以使用,不过网上很多加密解密的工具,这种 ...

  7. git remote add 用法

    前一阵子,对于git remote add 的内容一直调错,现在明确一下: 这里是gitStack的用法:git remote add gitServerName http://ip/name(这里没 ...

  8. iOS - KVO 简单应用

    KVO(键值监听)全称 Key Value Observing.使用KVO可以实现视图组件和数据模型的分离,视图作为监听器,当模型的属性值发生变化后,监听器可以做相应的处理.KVO的方法由NSKeyV ...

  9. Lucene-如何编写Lucene程序

    Lucene版本:7.1 使用Lucene的关键点 创建文档(Document),添加文件(Field),保存了原始数据信息: 把文档加入IndexWriter: 使用QueryParser.pars ...

  10. URAL 2027 URCAPL, Episode 1 (模拟)

    题意:给你一个HxW的矩阵,每个点是一个指令,根据指令进行一系列操作. 题解:模拟 #include<cstdio> #include<algorithm> using nam ...