Enterprising Escape

</pre><table width="100%" bgcolor="#ffffff"><tbody><tr><td align="center"><strong>Time Limit: </strong>20000ms, <strong>Special Time Limit:</strong>50000ms, <strong>Memory Limit:</strong>65536KB</td></tr><tr><td align="center"><strong>Total submit users:</strong> 28, <strong>Accepted users: </strong>18</td></tr><tr><td align="center"><strong>Problem 12921 : </strong><span style="color:#0000ff;">No special judgement</span></td></tr><tr><td><span style="font-size:14px;color:#0000aa;"><strong>Problem description</strong></span></td></tr><tr><td class="title"><p>The Enterprise is surrounded by Klingons! Find the escape route that has the quickest exit time, and print that time.Input is a rectangular grid; each grid square either has the Enterprise or some class of a Klingon warship. Associated with each class of Klingon warship is a time that it takes for the Enterprise to defeat that Klingon. To escape, the Enterprise must defeat each Klingon on some path to the perimeter. Squares are connected by their edges, not by corners (thus, four neighbors). </p></td></tr><tr><td><span style="font-size:14px;color:#0000aa;"><strong>Input</strong></span></td></tr><tr><td><p>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.Following that will be k lines. Each will consist of a capital letter used to label the class of Klingon ships followed by the duration required to defeat that class of Klingon. The label will not be “E”. The duration is in minutes and will be between 0 and 100,000, inclusive. Each label will be distinct.Following that will be h lines. Each will consist of w capital letters (with no spaces between them). There will be exactly one “E” across all h lines, denoting the location of the Enterprise; all other capital letters will be one of the k labels given above, denoting the class of Klingon warship in the square. </p></td></tr><tr><td><span style="font-size:14px;color:#0000aa;"><strong>Output</strong></span></td></tr><tr><td><p>Your output should be a single integer value indicating the time required for the Enterprise to escape. </p></td></tr><tr><td><span style="font-size:14px;color:#0000aa;"><strong>Sample Input</strong></span></td></tr><tr><td><pre>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
#include<iostream>//E题迷宫。。。不过呀。。。喵 
#include<cstring>//s用优先队列写的,应该还有其他方法吧,迷宫题型bfs之类的 
#include<cstdlib>//原理不明,还不能收进去 
#include<queue>//输入很巧 
#include<vector>//最后的memset初始化有问题 
#define maxn 1010
#include<algorithm>
#define INF 999999999
#define ll int
using namespace std;
int strx,stry;
char s[maxn][maxn];
char str[5];
int num[30];
int k,w,h;
//bool vis[1000010];
int numdp[1010*1010];
bool vis[maxn][maxn];
int v[4]={-1,1,0,0};
int e[4]={0,0,1,-1};
int result;

  	
struct cmp
{
    bool operator()(int x, int y)
    {
        return numdp[x] > numdp[y];
    }
};


int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        
        scanf("%d%d%d", &k, &w, &h);
        memset(num, 0, sizeof(num));
        for(int i = 0; i < k; i++)
        {
        	int x;
            scanf("%s%d", str, &x);//一输输一行取始个 
            num[str[0] - 'A'] = x;//超巧 
        }
        for(int i = 0; i < h; i++)
            scanf("%s", s[i]);//学习中 
            strx = stry = -1;
        
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(s[i][j]=='E')
                {
                strx=i;
                stry=j;
                break;
                }
            }
        }
        memset(vis, 0, h*1010);
        memset(numdp, 0, h*1010*sizeof(int));
        vis[strx][stry] = true;//学习了 
        priority_queue<int, vector<int>, cmp>q;
	 int z=(strx)*w+stry;
     numdp[z] = 0;
	 q.push(z);
	 while(!q.empty())
	 {
	 int tep=q.top();
	 q.pop();
	 int x=tep/w;
	 int y=tep%w;
	 if(x==0 || x==(h-1) || y==0 || y==(w-1))
	 {
	 	result=numdp[tep];
	 	break;
	 }
	 for(int i=0;i<4;i++)
	 {
	 	int newx=x+v[i];int newy=y+e[i];
	if(!vis[newx][newy] && newx>-1 && newx<h && newy>-1 && newy<w )
		{
		    int newz=newx*w+newy;
			numdp[newz]=numdp[tep]+num[s[newx][newy] - 'A'];
			q.push(newz);
			vis[newx][newy]=true;
			
		} 
	 }
	 
	 }
	 cout<<result<<endl; 
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值