标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5800 Accepted Submission(s): 2025
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
char s[24][24];
int sx,sy,ex,ey,n,m,t,ans;
int vis[24][24][1560];
bool flag;
int dic[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y,key;
};
void bfs()
{
flag=0;
node temp;
int x,y,key;
memset(vis,0,sizeof(vis));
queue<node> q;
temp.x=sx,temp.y=sy,temp.key=0;
q.push(temp);
while(!q.empty())
{
temp=q.front(),q.pop();
sx=temp.x,sy=temp.y,key=temp.key;
if(sx==ex&&sy==ey)
{
flag=1;
ans=vis[sx][sy][key];
break;
}
for(int i=0;i<4;i++)
{
x=sx+dic[i][0],y=sy+dic[i][1];
if(x<0||y<0||x>=n||y>=m||vis[x][y][key]||s[x][y]==‘*‘)
continue;
if(s[x][y]==‘.‘)
{
vis[x][y][key]=vis[sx][sy][key]+1;
temp.x=x;
temp.y=y;
temp.key=key;
q.push(temp);
}
if(s[x][y]>=‘a‘&&s[x][y]<=‘j‘)
{
int key1=(key|(1<<(s[x][y]-‘a‘)));
vis[x][y][key1]=vis[sx][sy][key]+1;
temp.x=x;
temp.y=y;
temp.key=key1;
q.push(temp);
}
if(s[x][y]>=‘A‘&&s[x][y]<=‘J‘)
{
if(key&(1<<(s[x][y]-‘A‘)))
{
vis[x][y][key]=vis[sx][sy][key]+1;
temp.x=x;
temp.y=y;
temp.key=key;
q.push(temp);
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
for(int j=0;j<m;j++)
{
if(s[i][j]==‘@‘)
sx=i,sy=j,s[i][j]=‘.‘;
else if(s[i][j]==‘^‘)
ex=i,ey=j,s[i][j]=‘.‘;
}
}
bfs();
if(!flag||ans>=t)
printf("-1\n");
else
printf("%d\n",ans);
}
}
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4354394.html