Acdream semi-finals guide series (7)

Source: Internet
Author: User
A-Dragon maze Time limit:2000/1000 ms (Java/Others) Memory limit:128000/64000 KB (Java/others) submit status

Question connection: Portal
Problem description

You are the prince of Dragon kingdom and your kingdom is in danger of running out of power. you must find power to save your kingdom and its people. an old legend states that power comes from a place known as Dragon maze. dragon maze appears randomly out of nowhere without notice and suddenly disappears without warning. you know where Dragon maze is now, so it is important you retrieve some power before it disappears.

Dragon maze is a rectangular maze,N × mGrid of cells. The top left corner cell of the maze is(0, 0)And the bottom right corner is(N-1, M-1). Each cell making up the maze can be either a dangerous place which you never escape after entering, or a safe place that contains a certain amount of power. the power in a safe cell is automatically gathered once you enter that cell, and can only be gathered once. starting from a cell, you can walk up/down/left/right to adjacent cells with a single step.

Now you know where the entrance and exit cells are, that they are different, and that they are both safe cells. In order to get out of Dragon maze before it disappears,You must walk from the entrance cell to the exit cell taking as few steps as possible.

If there are multiple choices for the path you cocould take, you must choose the one on which you collect as much power as possible in order to save your kingdom.

Input

The first line of the input gives the number of test cases,T (1 ≤ T ≤ 30).TTest Cases follow.

Each test case starts with a line containing two integersNAndM (1 ≤ n, m ≤100), Which give the size of Dragon maze as described abve.

The second line of each test case contains four IntegersENX, eny, Exx, exy (0 ≤ ENX, Exx <n, 0 ≤ eny, exy <m ),Describing the position of entrance Cell(ENX, eny)And exit Cell(Exx, exy).

ThenNLines follow and each line hasMNumbers, separated by spaces, describingN × mCells of Dragon maze from top to bottom.

Each number for a cell is either-1, Which indicates a cell is dangerous, or a positive integer, which indicates a safe cell containing a certain amount of power.

Output

For each test case, output one line containing "case # X: Y", whereXIs the case number (starting from 1 ).

If it's possible for you to walk from the entrance to the exit,YShocould be the maximum total amount of power you can collect by taking the fewest steps possible.

If you cannot walk from the entrance to the exit,YShocould be the string"Mission Impossible."(Quotes for clarity ).

Sample Input
22 30 2 1 02 -1 53 -1 64 40 2 3 2-1 1 1 21 1 1 12 -1 -1 11 1 1 1
Sample output
Case #1: Mission Impossible. Case #2: 7 meaning: A simple BFS plus priority queue can also be done by DFS. After all, the data is small. AC code:            
/**  this code is made by eagle*  Problem: 1191*  Verdict: Accepted*  Submission Date: 2014-09-19 01:46:08*  Time: 72MS*  Memory: 1736KB*/#include <iostream>#include <cstring>#include <cstdio>#include <queue> using namespace std;int N,M,ux,uy,nx,ny;int map[110][110];int d[][2] = {-1,0,1,0,0,1,0,-1}; struct Node{    int x,y,num,t;    //Node (int x, int y, int num) : x(x),y(y),num(num) {}    bool operator < (const Node &cnt) const    {        return cnt.t < t || (cnt.num > num && cnt.t == t);    }}; bool query(){    Node a;    priority_queue<Node>ms;    a.x = ux;    a.y = uy;    a.num = map[ux][uy];    a.t = 0;    ms.push(a);    while(!ms.empty())    {        a = ms.top();        ms.pop();//        cout<<a.x<<":"<<a.y<<endl;        if(a.x == nx && a.y == ny)        {            printf("%d\n",a.num);            return true;        }        for(int i = 0; i < 4; i++)        {            Node b;            b.x = a.x + d[i][0];            b.y = a.y + d[i][1];            if(b.x < 0 || b.y < 0 || b.x >= N || b.y >= M || map[b.x][b.y] == -1) continue;            b.num = a.num + map[b.x][b.y];            b.t = a.t + 1;            ms.push(b);            map[b.x][b.y] = -1;        }    }    return false;} int main(){    //freopen("in.txt","r",stdin);    int T,cnt = 0;    scanf("%d",&T);    while(T--)    {        printf("Case #%d: ",++cnt);        scanf("%d %d",&N,&M);        scanf("%d %d %d %d",&ux,&uy,&nx,&ny);        for(int i = 0; i < N; i++)            for(int j = 0; j < M; j++)                scanf("%d",&map[i][j]);        if(!query())            puts("Mission Impossible.");    }    return 0;}



Acdream semi-finals guide series (7)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.