设为首页 加入收藏

TOP

ACdream区域赛指导赛之手速赛系列(7)(一)
2015-07-20 17:39:07 来源: 作者: 【 】 浏览:6
Tags:ACdream 区域 指导 系列

A - Dragon Maze

Time Limit: 2000/1000MS ( Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Status

题目连接: 传送门

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, an N×M grid 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 could 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). T test cases follow.

Each test case starts with a line containing two integers N and M(1 ≤ N, M ≤ 100), which give the size of Dragon Maze as described above.

The second line of each test case contains four integers enx, eny, exx, exy(0 ≤ enx, exx < N, 0 ≤ eny, exy < M), describing the position of entrance cell (enx, eny) and exit cell (exx, exy).

Then N lines follow and each line has M numbers, separated by spaces, describing the N×M cells 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", where x is the case number (starting from 1).

If it's possible for you to walk from the entrance to the exit, y should 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, y should be the string "Mission Impossible." (quotes for clarity).

Sample Input

2
2 3
0 2 1 0
2 -1 5
3 -1 6
4 4
0 2 3 2
-1 1 1 2
1 1 1 1
2 -1 -1 1
1 1 1 1

Sample Output

Case #1: Mission Impossible.
Case #2: 7

意解: 一个简单的BFS加优先队列,dfs也可以做,毕竟数据很小.

AC代码:
            
/*
*  this code is made by eagle
*  Problem: 1191
*  Verdict: Accepted
*  Submission Date: 2014-09-19 01:46:08
*  Time: 72MS
*  Memory: 1736KB
*/
#include 
   
    
#include 
    
      #include 
     
       #include 
      
        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
       
        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<
        
         = 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,
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OC 第七天 字典 lesson7 下一篇使用cecil 完成 code injection

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)