acmdream1191 bfs+優先佇列
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
剛開始的時候沒想到用優先佇列,採用了普通的佇列直接進行了bfs後來才想到自己忘記了寫回溯,這樣一來先搜到的路如果不是最優解的話一旦標記就把後面的本來可行的路給封死了,無疑就得不到正解了,因此我們在搜尋的時候一定要保證先搜到的肯定是最優解,想到這裡就很容易想到優先佇列了,那麼題目就迎刃而解了
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct note
{
int x,y,num,sum;
note() {}
note(int _x,int _y,int _num,int _sum):x(_x),y(_y),num(_num),sum(_sum) {}
bool operator <(const note a)const
{
return (a.num<num||(a.num==num&&sum<a.sum));
}
};
int dx[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int T,n,m,x1,x2,y1,y2,a[150][150];
bool flag[150][150];
priority_queue<note>q;
bool judge(int x,int y)
{
if(x<n&&x>=0&&y<m&&y>=0&&a[x][y]!=-1)
return true;
return false;
}
bool bfs(note p)
{
memset(flag,0,sizeof(flag));
while(!q.empty())
q.pop();
q.push(p);
while(!q.empty())
{
note p=q.top();
int x=p.x;
int y=p.y;
int num=p.num;
int sum=p.sum;
if(x==x2&&y==y2)
{
printf("%d\n",sum);
return true;
}
q.pop();
for(int i=0; i<4; i++)
{
int xx=x+dx[i][0];
int yy=y+dx[i][1];
if(judge(xx,yy)&&flag[xx][yy]==0)
{
q.push(note(xx,yy,num+1,sum+a[xx][yy]));
flag[xx][yy]=1;
}
}
}
return false;
}
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
scanf("%d",&a[i][j]);
printf("Case #%d: ",++tt);
if(!bfs(note(x1,y1,0,a[x1][y1])))
printf("Mission Impossible.\n");
}
return 0;
}
相關文章
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- HDU 1026(優先佇列+BFS+前驅記錄)佇列
- PHP優先佇列PHP佇列
- 堆--優先佇列佇列
- 優先佇列 (轉)佇列
- 淺談優先佇列佇列
- STL 優先佇列 用法佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- 優先佇列和堆排序佇列排序
- 堆排序與優先佇列排序佇列
- Java優先佇列(PriorityQueue)示例Java佇列
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- Redis實現任務佇列、優先順序佇列Redis佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 二叉堆優先佇列佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- 堆——神奇的優先佇列(上)佇列
- 優先佇列的效能測試佇列
- hdu5040 優先佇列+bfs佇列
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 演算法面試(三) 優先佇列演算法面試佇列
- STL優先佇列最小堆最大堆佇列
- STL醜數(set+優先佇列)佇列
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- 1007(優先佇列)佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 二叉堆實現優先佇列佇列
- STL(二十)priority_queue優先佇列容器佇列