#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n,m,t,vis[25][25][1024];
char mp[25][25];
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};
struct node
{
int x,y,t,k;
} p1,p2;
int bfs(int sx,int sy,int ex,int ey)
{
int i,j,flag=0;
queue<node>q;
p1.x=sx;
p1.y=sy;
p1.t=0;
p1.k=0;
q.push(p1);
while(!q.empty())
{
p1=q.front();
q.pop();
//printf("%d %d
",p1.x,p1.y);
if(p1.x==ex&&p1.y==ey)
{
flag=p1.t;
break;
}
for(i=0; i<4; i++)
{
p2.x=p1.x+dx[i];
p2.y=p1.y+dy[i];
p2.t=p1.t+1;
p2.k=p1.k;
if(p2.x<0||p2.x>=n||p2.y<0||p2.y>=m) continue;
if(mp[p2.x][p2.y]=='*') continue;
if(mp[p2.x][p2.y]<='J'&&mp[p2.x][p2.y]>='A')
{
int k=p2.k&(1<<mp[p2.x][p2.y]-'A');
if(!k) continue;
}
if(mp[p2.x][p2.y]<='j'&&mp[p2.x][p2.y]>='a')
{
p2.k=p2.k|(1<<(mp[p2.x][p2.y]-'a'));
}
while(vis[p2.x][p2.y][p2.k]==0)
{
vis[p2.x][p2.y][p2.k]=1;
q.push(p2);
}
}
}
if(flag>=t||flag==0) flag=-1;
return flag;
}
int main()
{
int i,j,sx,sy,ex,ey,flag;
while(~scanf("%d%d%d",&n,&m,&t))
{
memset(vis,0,sizeof(vis));
for(i=0; i<n; i++)
scanf("%s",mp[i]);
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
if(mp[i][j]=='@')
{
sx=i;
sy=j;
mp[i][j]='.';
}
if(mp[i][j]=='^')
{
ex=i;
ey=j;
}
}
printf("%d
",bfs(sx,sy,ex,ey));
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/