BFS + 状态压缩 险过 这个并不是最好的算法 但是写起来比较简单 , 可以AC,但是耗时比较多
下面是代码 就不多说了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a>b?a:b)
#define maxn 100 int m, n, time, k, sorce[];
bool vis[<<][][];
char map[][]; typedef struct
{
int step;
int x, y;
int sorce;
int stau;
}Point; int bfs(Point P); int main()
{
int i, j, T, ans, count = ;
Point P;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&time,&k); for(i=; i<k; i++)
scanf("%d",&sorce[i]); for(i=; i<m; i++)
{
scanf("%s",map[i]); for(j=; j < n; j++)
{
if(map[i][j] == '@')
P.x = i, P.y = j, P.step = P.sorce = P.stau = ;
}
} ans = bfs(P); printf("Case %d:\n",count++);
if(ans == -)
printf("Impossible\n");
else
printf("The best score is %d.\n",ans);
if(T)
printf("\n");
}
return ;
} int bfs(Point P)
{
int i, max = -, key;
int dir[][] = {,,,,-,,,-};
Point Pn;
queue <Point> q;
q.push(P);
memset(vis,,sizeof(vis));
vis[P.stau][P.x][P.y] = ;
while( !q.empty() )
{
P = q.front();
q.pop();
for(i=; i<; i++)
{
Pn.x = P.x + dir[i][];
Pn.y = P.y + dir[i][];
Pn.step = P.step + ;
Pn.sorce = P.sorce;
Pn.stau = P.stau;
if(Pn.step > time)
break;
if(Pn.x>= && Pn.x<m && Pn.y>= && Pn.y<n && map[Pn.x][Pn.y] != '*')
{
if(map[Pn.x][Pn.y] == '<')
{
max = Max(max,Pn.sorce);
if(Pn.stau == ((<<k)-) )
return max;
}
else if(map[Pn.x][Pn.y] >= 'A' && map[Pn.x][Pn.y] <= 'J')
{
key = map[Pn.x][Pn.y] - 'A';
if( ((Pn.stau)&(<<key)) == )
{
Pn.sorce += sorce[key];
Pn.stau += <<key;
}
}
if( !vis[Pn.stau][Pn.x][Pn.y])
{
vis[Pn.stau][Pn.x][Pn.y] = ;
q.push(Pn);
} }
}
}
return max;
}