Enterprising Escape |
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB |
Total submit users: 28, Accepted users: 18 |
Problem 12921 : No special judgement |
Problem description |
The Enterprise is surrounded by Klingons! Find the escape route that has the quickest exit time, and print that time. |
Input |
The first line will contain T, the number of cases; 2 ≤ T ≤ 100. Each case will start with line containing three numbers k, w, and h. The value for k is the number of different Klingon classes and will be between 1 and 25, inclusive. The value for w is the width of the grid and will be between 1 and 1000, inclusive. The value for h is the height of the grid and will be between 1 and 1000, inclusive. |
Output |
Your output should be a single integer value indicating the time required for the Enterprise to escape. |
Sample Input |
2 6 3 3 A 1 B 2 C 3 D 4 F 5 G 6 ABC FEC DBG 2 6 3 A 100 B 1000 BBBBBB AAAAEB BBBBBB |
Sample Output |
2 400 |
题目没看 就是知道大概的意思 就是会有K个字母 每个字母对应相应的值 在一个地图中 从E字母开始出发 直到最外面记录走过的点的值 求走到外面值的最小值
第一反映就是优先队列+bfs 写起来还算简单 因为只有大写字母 直接用数组存储点的值就好 遍历过的点要标记
代码如下:
<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOV(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REV(i,a,b) for(int i=a-1;i>=b;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
int num[26];
bool vis[1005][1005];
char map[1005][1005];
int sx,sy;
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int k,w,h;
struct node
{
int x,y;
int sum;
bool operator <(const node p)const
{
return sum>p.sum;
}
};
node p,q;
void bfs()
{
priority_queue<node> Q;
q.x=sx; q.y=sy;
q.sum=0;
vis[q.x][q.y]=1;
Q.push(q);
while(!Q.empty())
{
q=Q.top(); Q.pop();
// cout<<q.x<<" "<<q.y<<" "<<q.sum<<endl;
if(q.x<0||q.x>=h||q.y<0||q.y>=w)
{
printf("%d\n",q.sum);
// cout<<"ww"<<w<<"hh"<<h<<endl;
// cout<<"11111111111"<<endl;
return;
}
for(int i=0;i<4;i++)
{
p.x=q.x+dir[i][0]; p.y=q.y+dir[i][1];
if(p.x<0||p.x>=h||p.y<0||p.y>=w)
{
printf("%d\n",q.sum);
return;
}
if(!vis[p.x][p.y])
{
int tt=map[p.x][p.y]-'A';
vis[p.x][p.y]=1;
p.sum=q.sum+num[tt];
Q.push(p);
}
}
}
}
int main()
{
//freopen("ceshi.txt","r",stdin);
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d%d%d\n",&k,&w,&h);
MEM(vis,0);
for(int i=0;i<k;i++)
{
char c; int n;
scanf("%c %d\n",&c,&n);
num[c-'A']=n;
}
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);
for(int j=0;j<w;j++)
if(map[i][j]=='E')
{
sx=i; sy=j;
}
}
bfs();
}
return 0;
}
</span>