2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)
時間限制:1000ms
單點時限:1000ms
記憶體限制:256MB
描述
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.
During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.
Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace, and he wanted to reach Tang Monk and rescue him.
The palace can be described as a matrix of characters. Different characters stand for different rooms as below:
'S' : The original position of Sun Wukong
'T' : The location of Tang Monk
'.' : An empty room
'#' : A deadly gas room.
'B' : A room with unlimited number of oxygen bottles. Every time Sun Wukong entered a 'B' room from other rooms, he would get an oxygen bottle. But staying there would not get Sun Wukong more oxygen bottles. Sun Wukong could carry at most 5 oxygen bottles at the same time.
'P' : A room with unlimited number of speed-up pills. Every time Sun Wukong entered a 'P' room from other rooms, he would get a speed-up pill. But staying there would not get Sun Wukong more speed-up pills. Sun Wukong could bring unlimited number of speed-up pills with him.
Sun Wukong could move in the palace. For each move, Sun Wukong might go to the adjacent rooms in 4 directions(north, west,south and east). But Sun Wukong couldn't get into a '#' room(deadly gas room) without an oxygen bottle. Entering a '#' room each time would cost Sun Wukong one oxygen bottle.
Each move took Sun Wukong one minute. But if Sun Wukong ate a speed-up pill, he could make next move without spending any time. In other words, each speed-up pill could save Sun Wukong one minute. And if Sun Wukong went into a '#' room, he had to stay there for one extra minute to recover his health.
Since Sun Wukong was an impatient monkey, he wanted to save Tang Monk as soon as possible. Please figure out the minimum time Sun Wukong needed to reach Tang Monk.
輸入
There are no more than 25 test cases.
For each case, the first line includes two integers N and M(0 < N,M ≤ 100), meaning that the palace is a N × M matrix.
Then the N×M matrix follows.
The input ends with N = 0 and M = 0.
輸出
For each test case, print the minimum time (in minute) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print -1
樣例輸入
2 2 S# #T 2 5 SB### ##P#T 4 7 SP..... P#..... ......# B...##T 0 0
樣例輸出
-1 8 11
題目大意:S為起點,T為終點,.為空地,#為毒氣,B為氧氣室,P為加速。每走一格時間花費1,入一個#花費一個氧氣瓶,無氧氣不能入,入B能獲得一個氧氣,身上最多能攜帶5個氧氣,入P不花費時間,求起點到終點的最小花費時間。
思路:呵呵,想多了。典型的搜尋題嘛,之前的想法是搜尋每條路,統計路上的#與B,判斷在B額外獲得1個氧氣的花費,最後減去相除取時間,再取最值。感覺應該能行吧?但是死磕在BB與BP的情況,效率不同可能涉及到組合問題,這就很傷腦筋了.........
然而....大佬們告訴我直接搜就可以了.....
參考了網上大佬的做法:www.bubuko.com/infodetail-2777883.html
因為最多能帶5個氧氣,所以每次入毒都只有那幾種可能,所以可將二維圖擴充套件為三維的狀態。每個地方相同的狀態進入結果是相同的,所以標記的vis陣列擴充套件為3維即可。
程式碼如下:
#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define swap(a,b) {int t=a;a=b;b=t}
using namespace std;
//using namespace __gnu_cxx;
char mp[205][205];
int vis[205][205][10];
struct node{
int x;
int y;
int t;
int b;
inline bool operator<(const node &a) const{
return t>a.t;
}
}p,p2;
int n,m,x1,y1;
int f[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node>q;
q.push(node{x1,y1,0,0});
vis[x1][y1][0]=1;
while(!q.empty())
{
p=q.top();
q.pop();
if(mp[p.x][p.y]=='T') return p.t;
per(i,0,3)
{
p2=p;
int nx=p.x+f[i][0];
int ny=p.y+f[i][1];
p2.x=nx;
p2.y=ny;
if(nx<0||nx>=n||ny<0||ny>=m) continue;
if(mp[nx][ny]=='B')
{
p2.t++;
p2.b=min(p2.b+1,5);
if(vis[nx][ny][p2.b]==0)
{
vis[nx][ny][p2.b]=1;
q.push(p2);
}
}
else if(mp[nx][ny]=='P')
{
if(vis[nx][ny][p2.b]==0)
{
vis[nx][ny][p2.b]=1;
q.push(p2);
}
}
else if(mp[nx][ny]=='#')
{
if(p2.b==0) continue;
p2.b--;
p2.t+=2;
if(vis[nx][ny][p2.b]==0)
{
vis[nx][ny][p2.b]=1;
q.push(p2);
}
}
else {
p2.t++;
if(vis[nx][ny][p2.b]==0)
{
vis[nx][ny][p2.b]=1;
q.push(p2);
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&n!=0&m!=0)
{
getchar();
per(i,0,n-1)
{
per(j,0,m-1)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S')
{
x1=i;
y1=j;
}
}
getchar();
}
printf("%d\n",bfs());
}
return 0;
}
相關文章
- ACM-ICPC 2018 南京賽區網路預賽 __G Lpl and Energy-saving Lamps【線段樹+模擬】ACMLAMP
- ACM-ICPC 2018 徐州賽區網路預賽ACM
- ACM-ICPC 2018 南京賽區網路預賽__B The writing on the wall【列舉】ACM
- ACM-ICPC 2018 瀋陽賽區網路預賽ACM
- ACM-ICPC 2018 徐州賽區網路預賽 F. Features TrackACM
- 聯賽模擬測試18 A. 施工 單調佇列(棧)優化DP佇列優化
- 2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)Git
- ACM-ICPC 2018 徐州賽區網路預賽 I. Characters with Hash【簽到題】ACM
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- 2024 ICPC 網路預選賽 第 2 場
- PHP優先佇列PHP佇列
- ACM-ICPC 2018 南京賽區網路預賽__K The Great Nim Game【博弈論+費馬小定理+DP】ACMGAM
- ACM-ICPC 2018 南京賽區網路預賽__J. Sum【尤拉篩法+質因子分解+思維】ACM
- STL 優先佇列 用法佇列
- 淺談優先佇列佇列
- 堆與優先佇列佇列
- 新生賽及預選賽 10
- ACM-ICPC 2018 南京賽區網路預賽__L. Magical Girl Haze 【Dijkstra演算法+分層圖思想】ACM演算法
- 2018 徐州網路賽 G 題解
- 優先佇列和堆排序佇列排序
- 01揹包優先佇列優化佇列優化
- 2018華為網路技術大賽
- 2018ACM-ICPC北京賽區 - A:Jin Yong’s Wukong Ranking List(DFS)ACM
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 棧,佇列,優先順序佇列簡單介面使用佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 動感地帶5G校園先鋒賽河北賽區線上海選賽燃戰開啟
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- STL優先佇列最小堆最大堆佇列
- leetcode621——優先佇列的思路LeetCode佇列
- 2024中國工業網際網路安全大賽智慧家電行業賽道選拔賽行業
- 2016中國“網際網路+”創業創新大賽(西北+山西)賽區決賽成功舉辦 優秀專案將會師海口...創業
- 2016中國“網際網路+”創業創新大賽(西北+山西)賽區決賽成功舉辦優秀專案將會師海口創業
- 2018icpc 南京網路賽L Magical Girl Haze
- 演算法面試(三) 優先佇列演算法面試佇列
- 二叉堆實現優先佇列佇列