ACdream区域赛指导赛之手速赛系列(7)

A -
Dragon Maze

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

SubmitStatus

题目连接:    
传送门

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 <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;
}

时间: 2024-12-18 13:06:09

ACdream区域赛指导赛之手速赛系列(7)的相关文章

ACdream区域赛指导赛之手速赛系列(5) 题解

A - Problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are

ACdream区域赛指导赛之手速赛系列(6)

Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. Each 9x9 matrix is par

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)G——BFS——Cutting Figure

Description You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make i

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)D——数学——Triangles

Description 已知一个圆的圆周被  N 个点分成了 N 段等长圆弧,求任意取三个点,组成锐角三角形的个数. Input 多组数据. 每组数据一个N (N ≤  1000000). Output 对于每组数据,输出不同锐角三角形的个数. Sample Input 3 4 5 Sample Output 1 0 5大意:数学推导,分成奇数点偶数点讨论偶数时:只要两个相减就是答案奇数时同理:还有1ll*涨姿势用来变成long long 形式 #include<cstdio> #includ

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge

Description Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they do

快速切题 acdream手速赛(6)A-C

Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each colu

Acdream手速赛7

蛋疼啊,本次只做出了一道题目...渣爆了... 妈蛋,,卡题之夜..比赛结果是1道题,比赛完哗啦哗啦出4道题.. A acdream1191 Dragon Maze 题意: 给一个迷宫,给出入口坐标和出口坐标,要求从入口到出口的步数尽可能少,如果有多种方案,则要求获得的分数尽可能多,获得的分数为经过的方格的数字之和 思路: bfs求最小步数,每走一步更新一下走到这个格子的最大权值 #include <bits/stdc++.h> using namespace std; typedef lon

Java制作最难练手速游戏,Faker都坚持不了一分钟

想练手速,来啊,互相伤害啊 Java制作最难练手速游戏,目测Faker也坚持不了一分钟 制作思路:只靠Java实现.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java. 字母模型应该是整个游戏的主角,因为整个游戏过程中都涉及到它的运动,比如坠落,消失,产生等,首先应该考虑字母随即出现的位置,在游戏中不断下落,计算下落的高

猫和老鼠 蓝桥杯/手速/暴力练习赛(暴力搜索)

猫和老鼠 蓝桥杯/手速/暴力练习赛 [题目描述] 猫和老鼠在10*10 的方格中运动,例如: *...*..... ......*... ...*...*.. .......... ...*.C.... *.....*... ...*...... ..M......* ...*.*.... .*.*...... C=猫(CAT) M=老鼠(MOUSE) *=障碍物 .=空地 猫和老鼠每秒中走一格,如果在某一秒末他们在同一格中,我们称他们“相遇”. 注意,“对穿”是不算相遇的.猫和老鼠的移动方式相