贼变态,wa了半天结果是因为bfs少写了一个return -1
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;
int n,m,k;
char map[25][25];
int vis[25][25][1200];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step,key;
};
bool check(int x, int y,int k)
{
if(x<0 || x>=n || y<0 || y>=m || map[x][y] == '*')
{
return false;
}
return true ;
}
int bfs(int x,int y)
{
queue<node> Q;
node a;
a.x = x;
a.y = y;
a.step = 0;
a.key = 0;
vis[a.x][a.y][a.key] = 1;
Q.push(a);
while(!Q.empty())
{
node tmp = Q.front();
Q.pop();
if(tmp.step >= k)
{
return -1;
}
if(map[tmp.x][tmp.y] == '^')
{
if (tmp.step < k) return tmp.step;
}
for(int i=0;i<4;i++)
{
//printf("%d *** %d\n",tmp.x+dir[i][0],tmp.y+dir[i][1]);
if(!check(tmp.x+dir[i][0],tmp.y+dir[i][1],tmp.key))
{
continue;
}
node gg;
gg.x = tmp.x + dir[i][0];
gg.y = tmp.y + dir[i][1];
gg.step = tmp.step + 1;
gg.key = tmp.key;
if( map[gg.x][gg.y] >= 'a' && map[gg.x][gg.y] <= 'j')
{
int num = map[ gg.x ][ gg.y ] - 'a';
int bur = 1<<num;
int hh = tmp.key|bur;
if(!vis[gg.x][gg.y][hh])
{
vis[gg.x][gg.y][hh] = 1;
gg.key = hh;
Q.push(gg);
}
}
else if(map[gg.x][gg.y] >= 'A' && map[gg.x][gg.y] <= 'J')
{
int num = map[gg.x][gg.y] - 'A';
int bur = 1<<num;
if(tmp.key&bur && !vis[gg.x][gg.y][gg.key])
{
vis[gg.x][gg.y][gg.key] = 1;
Q.push(gg);
}
}
else
{
if(!vis[gg.x][gg.y][gg.key])
{
vis[gg.x][gg.y][gg.key] = 1;
Q.push(gg);
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
int ans;
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j] == '@')
{
ans = bfs(i,j);
}
}
}
printf("%d\n",ans);
}
}