3 1 K.S ##1 1#T 3 1 K#T .S# 1#. 3 2 K#T .S. 21. 0 0
5 impossible 8
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
const int maxn=100+10;
int n,m;
char mp[maxn][maxn];
struct Node
{
    int x,y;
    int key,time,snake;
    friend bool operator<(Node n1,Node n2)
    {
        return n2.time<n1.time;
    }
};
priority_queue<Node>Q;
Node st,en;
bool vis[maxn][maxn][11];
int snake[maxn][maxn];
bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=n&&mp[x][y]!='#')
        return true;
    return false;
}
int judge(Node p,int dd)
{
    if(p.snake&(1<<dd))
        return 0;
    return 1;
}
int bfs()
{
   int xx,yy;
   while(!Q.empty())  Q.pop();
   Node cur,next;
   memset(vis,false,sizeof(vis));
   vis[st.x][st.y][0]=true;
   Q.push(st);
   while(!Q.empty())
   {
       cur=Q.top();
       Q.pop();
       if(cur.x==en.x&&cur.y==en.y&&cur.key==m)
            return cur.time;
       for(int i=0;i<4;i++)
       {
           xx=cur.x+dx[i];
           yy=cur.y+dy[i];
           if(check(xx,yy)&&!vis[xx][yy][cur.key])
           {
                if(mp[xx][yy]==cur.key+1+'0')
                {
                    vis[xx][yy][cur.key+1]=true;//񈬀
                    next.x=xx,next.y=yy;
                    next.snake=cur.snake;
                    next.key=cur.key+1;
                    next.time=cur.time+1;
                    Q.push(next);
                }
                else if(mp[xx][yy]=='S')//Óöµ½Éß
                {
                   if(!(cur.snake&(1<<snake[xx][yy])))
                   {
                     next.x=xx,next.y=yy;
                     next.key=cur.key;
                     next.snake=cur.snake|(1<<snake[xx][yy]);
                     next.time=cur.time+2;
                     vis[xx][yy][next.key]=true;
                     Q.push(next);
                   }
                   else
                    {
                        next.x=xx,next.y=yy;
                        next.key=cur.key;
                        next.snake=cur.snake;
                        next.time=cur.time+1;
                        vis[xx][yy][next.key]=true;
                        Q.push(next);
                    }
                }
               else
               {
                   next.x=xx,next.y=yy;
                   next.key=cur.key;
                   next.snake=cur.snake;
                   next.time=cur.time+1;
                   vis[xx][yy][next.key]=true;
                   Q.push(next);
               }
           }
       }
   }
   return -1;
}
void Read_Graph()
{
    char str[maxn];
    memset(snake,-1,sizeof(snake));
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str+1);
        for(int j=1;j<=n;j++)
        {
            mp[i][j]=str[j];
            if(mp[i][j]=='K')
            {
                st.x=i;
                st.y=j;
                st.key=0;
                st.time=0;
                st.snake=0;
            }
            else if(mp[i][j]=='T')
            {
                en.x=i;
                en.y=j;
                en.key=m;
            }
            else if(mp[i][j]=='S')
              snake[i][j]=cnt++;
        }
    }
}
int main()
{
     while(~scanf("%d%d",&n,&m))
     {
        if(n==0&&m==0)  return 0;
        Read_Graph();
        int ans=bfs();
        if(ans==-1)
            printf("impossible\n");
        else
            printf("%d\n",ans);
     }
    return 0;
}
/*
3
3 2
K#T
.S.
21.
*/
hdu5025Saving Tang Monk(bfs+优先队列+状态压缩)
原文:http://blog.csdn.net/u014303647/article/details/39479351