题意:拿钥匙,救唐僧,必须全部拿完才行,然后就是 他那第n把钥匙的时候必须n-1全配齐把才可以,然后#不能走,S是蛇,第一次见到要打一次,.可以走,问你最短距离是多少,
思路:就是状压蛇的状态,然后标记一下钥匙就好了,,,其中有个小bug我找了一天,,结果晚上了找了工作室的各种学长 ,改了一晚上 终于特么找到了 - - 真是菜 。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
int n,m;
int vis[111][111][10][1<<5+1];
int sx,sy,ex,ey,cnt,ans;
char ch[111][111];
struct node
{
int x,y;
int stept;//步数
int state;//蛇的状态
int cnt;//拿了第几把要是,钥匙的编号
friend bool operator < (const node &a,const node &b)
{
return a.stept>b.stept;
}
int prex,prey;
}edg[20];
//node book[11][11][11][11];
priority_queue<node>que;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int S_judge(int x,int y)
{
for(int i = 0 ; i < cnt ; i++)
{
if(edg[i].x == x && edg[i].y == y)
{
return i;
}
}
return -1;
}
int judge(int x,int y)
{
if(x<0||x>=n||y<0||y>=n)
{
return 0;
}
if(ch[x][y] == '#' ) return 0;
return 1;
}
void bfs(int sx,int sy)
{
while(!que.empty()) que.pop();
node now,nex;
now.x = sx , now.y = sy;
now.cnt = 0,now.state = 0,now.stept = 0;
que.push(now);
vis[sx][sy][now.cnt][now.state] = 1;
while(!que.empty())
{
now = que.top();
que.pop();
if(now.x == ex && now.y == ey && now.cnt >= m )
{
ans = min(ans,now.stept);
}
// nex = now; 我把这个写外面了 ,,,卧槽
for(int i = 0 ; i < 4 ; i++)
{
nex = now;
nex.x = now.x +dir[i][0];
nex.y = now.y +dir[i][1];
if(judge(nex.x,nex.y))
{
if(ch[nex.x][nex.y]>='1'&&ch[nex.x][nex.y]<='9')//碰到钥匙
{
if(nex.cnt == ch[nex.x][nex.y] - '1')//现在手里的钥匙和现在碰到的钥匙
{
nex.cnt = ch[nex.x][nex.y] - '0';
}
nex.stept = now.stept + 1;
}
if(ch[nex.x][nex.y] == 'S')//碰到蛇
{
int te = S_judge(nex.x,nex.y);
if(te!=-1)//表示当前遇到的是第几个蛇
{
if(((1<<te)&nex.state) == 0)//如果是0 表示还没有遇到过
{
nex.state = nex.state|(1<<te);//变成1
nex.stept = nex.stept + 2;
}
else
{
nex.stept = nex.stept + 1;
}
}
}
if(ch[nex.x][nex.y] == '.') nex.stept = nex.stept+1;
if(vis[nex.x][nex.y][nex.cnt][nex.state])
{
continue;
}
vis[nex.x][nex.y][nex.cnt][nex.state] = 1;
/* book[nex.x][nex.y][nex.cnt][nex.state].x = nex.x;
book[nex.x][nex.y][nex.cnt][nex.state].y = now.y;
book[nex.x][nex.y][nex.cnt][nex.state].cnt = now.cnt;
book[nex.x][nex.y][nex.cnt][nex.state].state = now.state;
*/ que.push(nex);
}
}
}
if(ans == 99999)
puts("impossible");
else
{
printf("%d\n",ans);
}
}
/*
void print(int x,int y,int cnt, int state)
{
if(x == sx && y == sy &&cnt ==0 &&state ==0)
{
printf("(%d - %d)\n",x,y);
return ;
}
print(book[x][y][cnt][state].x,book[x][y][cnt][state].y,book[x][y][cnt][state].cnt,book[x][y][cnt][state].state);
printf("(%d - %d)\n",x,y);
}
*/
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ans = 99999;
if(n == 0&&m==0) break;
cnt = 0 ;
memset(vis,0,sizeof(vis));
//memset(book,0,sizeof(book));
memset(edg,0,sizeof(edg));
memset(ch,0,sizeof(ch));
for(int i = 0 ; i < n ;i++)
{
cin>>ch[i];
for(int j = 0 ; j < n ;j++)
{
if(ch[i][j] == 'K') sx = i,sy = j,ch[i][j] = '.';
if(ch[i][j] == 'T') ex = i,ey = j,ch[i][j] = '.';
if(ch[i][j] == 'S') edg[cnt].x = i,edg[cnt].y = j,cnt++;
}
}
bfs(sx,sy);
// print(ex,ey,m,(1<<m)-1);
// puts("-----");
}
}