(BFS) acdream 1191

Dragon Maze

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

Submit Statistic Next Problem

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

Source

codejam

题意:

起点到终点的最短步数的最大价值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define INF 100000000
using namespace std;
int mp[105][105],n,m,sx,sy,ex,ey,step[105][105],val[105][105];
bool vis[105][105];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int minstep;
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||mp[x][y]==-1)
        return false;
    return true;
}
void bfs()
{
    memset(step,0,sizeof(step));
    memset(val,0,sizeof(val));
    memset(vis,0,sizeof(vis));
    queue<int> q;
    q.push(sx),q.push(sy);
    vis[sx][sy]=1;
    val[sx][sy]=mp[sx][sy];
    while(!q.empty())
    {
        int fx,fy;
        fx=q.front(),q.pop();
        fy=q.front(),q.pop();
        //cout<<fx<<" "<<fy<<endl;
        //system("pause");
        if(step[fx][fy]>minstep) continue;
        if(fx==ex&&fy==ey)
        {
            minstep=step[fx][fy];
        }
        for(int i=0;i<4;i++)
        {
            int xx,yy;
            xx=fx+dx[i],yy=fy+dy[i];
            if(!check(xx,yy))
                continue;
            if(step[xx][yy]==step[fx][fy]+1)
            {
                val[xx][yy]=max(val[xx][yy],val[fx][fy]+mp[xx][yy]);
            }
            if(vis[xx][yy])
                continue;
            vis[xx][yy]=1;
            step[xx][yy]=step[fx][fy]+1;
            val[xx][yy]=val[fx][fy]+mp[xx][yy];
            q.push(xx),q.push(yy);
        }
    }
}
int main()
{
    int tt,cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
        minstep=INF;
        scanf("%d%d",&n,&m);
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                scanf("%d",&mp[i][j]);
        }
        bfs();
        if(minstep==INF)
            printf("Case #%d: Mission Impossible.\n",cas++);
        else
            printf("Case #%d: %d\n",cas++,val[ex][ey]);
    }
    return 0;
}

  

时间: 2024-07-30 13:43:32

(BFS) acdream 1191的相关文章

ACdream 1191(广搜)

题目链接:http://acdream.info/problem?pid=1191 Dragon Maze Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description You are the prince of Dragon Kingdom and your kingdom is in danger o

ACdream 1025 bfs

Transform Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One can transform xinto x+d  if dis divisor of x. Find out the minimum number of steps to transform a into b.

acdream 1681 跳远女王(BFS)

Problem Description 娜娜觉得钢琴很无趣了,就抛弃了钢琴,继续往前走,前面是一片湖,娜娜想到湖的对岸,可惜娜娜找了好久都没找到小桥和小船,娜娜也发现自己不是神仙,不能像八仙过海一样.正当娜娜发愁的时候,娜娜发现湖上面有一些石头!娜娜灵机一动,发现可以沿着石头跳吖跳吖,这样一直跳下去,或许能跳到对岸! 娜娜把所有石头的位置都告诉你,然后娜娜能跳的最远距离也是知道的~请聪明的你告诉娜娜,她能够顺利到达对岸吗? 为了能够顺利的表达每个石头的位置,假设娜娜正在x轴上,表示湖的一岸,湖的

nefu 1191 平行宇宙 (bfs)

Description 小k是时空贸易者,他经常在两个平行宇宙之间往来经商,现在他要从S点到达E点,问最少需要多长时间.(已知小k在同一个宇宙中只能向上下左右四个方向移动,每次移动需要1个单位时间,且不能在危险小行星带'#'中移动,遇到黑洞'O'时,他会被瞬间吸入另一个宇宙的对应的同一位置,比如从一个宇宙的黑洞处(2,2)必须且只能移动到另一个宇宙的(2,2)位置) Input 多组输入数据,每组数据第一行包含两个整数n,m(2<=n,m<=1000),表示两个宇宙的大小. 接下来n行表示第一

ACdream HUT新生摸底训练赛 B - 娜娜梦游仙境系列——跳远女王 bfs

题意:bfs 解题思路:建图bfs,要特判一下能否直接跳. 解题思路: 1 // File Name: b.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月12日 星期日 20时03分02秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include

acdream 1681(bfs)

题意:有一条河,河中有n个石头,给出了河的宽度和石头的位置,一个人要渡河,给出了这个人最远跳多远.问最少跳几下跳到对岸,或者如果跳不到那么就输出可以离对岸最近的距离. 题解:bfs,直接暴力把所有可能跳的情况都过一遍就可以找到解了. #include <cstdio> #include <cmath> #include <algorithm> #include <queue> using namespace std; const int N = 1005;

acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体.里面流躺物质. 而且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题).同一时候最小不能低于Li. 比如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3

【最短路】ACdream 1198 - Transformers&#39; Mission

Problem Description A group of transformers whose leader is Optimus Prime(擎天柱) were assigned a mission: to destroy all Decepticon's(霸天虎) bases. The bases are connected by roads. They must visit each base and place a bomb there. They start their missi

acdream 1211 Reactor Cooling 【上下界网络流 + 输出流量】

题目:acdream 1211 Reactor Cooling 分类:无源无汇的有上下界网络流. 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 例如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3