ACdream oj 1191 Dragon Maze(手速賽 A)
題目連結:http://115.28.76.232/problem?pid=1191
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
求 從起點走到終點,經過最短的步數所能獲得的最大能量 ,其中標記為-1的不能走,BFS 來搜尋最短的路線 然後更新走的這個格子的時候能得到的最大能量
程式碼如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 110;
const int INF = 0xffffff;
int mp[maxn][maxn];
int vv[maxn][maxn];
int dep[maxn][maxn];
bool vis[maxn][maxn];
int sx,sy,ex,ey,n,m,mmax;
struct nod{
int x,y;
int step;
};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool judge(int x,int y)
{
if(x>=0&&y>=0&&x<n&&y<m)
return true;
return false;
}
void bfs()
{
nod s,tmp,now;
mmax = INF;
s.x=sx,s.y=sy;
s.step=0;
queue<nod> Q;
Q.push(s);
vis[sx][sy]=1;
vv[sx][sy]=mp[sx][sy];
while(!Q.empty()){
now=Q.front();
Q.pop();
if(now.step>mmax) continue;
if(now.x==ex&&now.y==ey) mmax=min(now.step,mmax);
for(int i=0;i<4;i++){
tmp.x=now.x+dx[i];
tmp.y=now.y+dy[i];
if(!vis[tmp.x][tmp.y]||tmp.step==now.step+1)//更新走到這裡所能取得最大值
vv[tmp.x][tmp.y]=max(vv[tmp.x][tmp.y],vv[now.x][now.y]+mp[tmp.x][tmp.y]);
if(vis[tmp.x][tmp.y]||mp[tmp.x][tmp.y]==-1||!judge(tmp.x,tmp.y)) continue;
vis[tmp.x][tmp.y]=1;
tmp.step=now.step+1;
Q.push(tmp);
}
}
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--){
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]);
memset(vis,0,sizeof(vis));
memset(vv,0,sizeof(vv));
bfs();
printf("Case #%d: ",cas++);
if(mmax==INF) puts("Mission Impossible.");
else printf("%d\n",vv[ex][ey]);
}
return 0;
}
相關文章
- ACdream OJ 1153 (k-GCD)GC
- acdream群賽(4)Bad Horse(種類並查集)並查集
- 東華OJ 陣列競賽 分糖果陣列
- 東華 OJ 一維陣列競賽 等差數列陣列
- Postpartum dragon power pills in londonGo
- 淺談競速(賽車)遊戲的賽道取景設計遊戲
- Dragon Tiger Cattle Cattle GameGoGAM
- 手機芯戰!麒麟與驍龍上演難分勝負的技術競速賽
- 賽車競速遊戲:拉力賽藝術art of rally Mac中文版遊戲Mac
- YTU-OJ-Problem E: AB程式設計題--世界盃小組賽程式設計
- Macos賽車競速遊戲:art of rally拉力賽車 for Mac 最新中文版Mac遊戲
- ACdream1139 Sum(推公式+逆元求解)公式
- Acdream 1205 Disappeared Block(模擬)APPBloC
- uva11292-Dragon of LoowaterGo
- acmdream1191 bfs+優先佇列ACM佇列
- 正版D&D角色聯動策略RPG,《代號:Dragon》即將開啟S0賽季測試Go
- FPV無人機競速掀起潮流,或成為體育比賽?無人機
- Macos賽車競速遊戲:art of rally拉力賽車 for Mac 1.5.0中文版 推薦Mac遊戲
- Tiptronic手自一體化變速箱
- 賽車競速遊戲:極限競速地平線2Horizon Chase 2中文原生版遊戲
- 蘋果電腦賽車競速遊戲:極限競速地平線2Horizon Chase 2推薦蘋果遊戲
- [SceneKit專題]22-3D平衡球遊戲Marble-Maze3D遊戲
- hihocoder 1191 小W與網格 (組合數)
- 駛向2030:汽車行業競速賽(附下載)行業
- 無人機也有競速賽?最高可以達到恐怖的161公里無人機
- Vue + Koa 搭建 ACM OJVueACM
- 分段函式 (sdut oj)函式
- 計算題 (sdut oj)
- “競速”品類將迎復興?3A級跨平臺擬真賽車新作《巔峰極速》首度曝光
- 遊戲瑰寶 mini:Dragon Buster,雙階跳的起源遊戲Go
- In advance of using any dragon poeer pills Enduros Male EnhancementGoROS
- 力扣oj-字串相乘力扣字串
- OJ題之氣泡排序排序
- Hydro OJ搭建全過程
- UVALive 6665 Dragon’s Cruller (BFS + 優先佇列+hash)Go佇列
- 解讀網易易盾新一代IoT安全編譯器Maze編譯
- 寧波國際賽車場圈速排行分析大屏製作教程
- ACdream 1112 Alice and Bob (博弈&&素數篩選優化)優化