标签:状态 def type solution using 优先 bool \n har
‘S‘ : 起点
‘T‘ : 终点
‘#‘ : 毒气室
‘B‘ :氧气
‘P‘:不消耗步数
每次经过毒气室需要一瓶氧气,氧气可以重复获得,但只能带五瓶氧气,问最少步数
HINT:多维状态判重,多一维携带氧气瓶数量
没带氧气瓶的时候不能走毒气室#
携带超过5个跳过氧气B
相似题目:UVA816 Abbott‘s Revenge这题多一维方向
1 #include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<cstring> 8 #define fi first 9 #define se second 10 #define mp make_pair 11 #define pb push_back 12 #define pw(x) (1ll << (x)) 13 #define sz(x) ((int)(x).size()) 14 #define all(x) (x).begin(),(x).end() 15 #define rep(i,l,r) for(int i=(l);i<(r);++i) 16 #define per(i,l,r) for(int i=(r)-1;i>=(l);--i) 17 #define maxn 500005 18 #define eps 1e-9 19 #define PIE acos(-1) 20 #define dd(x) cout << #x << " = " << (x) << ", " 21 #define de(x) cout << #x << " = " << (x) << "\n" 22 #define endl "\n" 23 #define INF 0x3f3f3f3f 24 using namespace std; 25 typedef double db; 26 typedef long long LL; 27 typedef vector<int> vi; 28 typedef pair<int, int> pii; 29 //---------------------- 30 int n,m; 31 char pic[105][105]; 32 bool vis[105][105][6]; 33 const int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; 34 struct Node{ 35 int x; 36 int y; 37 int d=0; 38 int cnt=0; 39 bool operator<(const Node& a)const{ 40 return d>a.d; 41 } 42 }start,u,v; 43 int cnt; 44 bool check(Node v) 45 { 46 return v.x>=0&&v.x<n&&v.y>=0&&v.y<m; 47 } 48 int bfs(int x,int y)//把#当路障,找氧气瓶 49 { 50 memset(vis,0,sizeof(vis)); 51 priority_queue<Node>q; 52 vis[x][y][0]=1; 53 start.x=x;start.y=y;start.d=0;start.cnt=0; 54 q.push(start); 55 while(!q.empty()) 56 { 57 u=q.top();q.pop(); 58 if(pic[u.x][u.y]==‘T‘){return u.d;} 59 rep(i,0,4){ 60 v=u; 61 v.x+=dir[i][0]; 62 v.y+=dir[i][1]; 63 if(!check(v))continue; 64 if(pic[v.x][v.y]==‘#‘){ 65 if(v.cnt>=1)v.cnt--,v.d++; 66 else continue; 67 } 68 else if(pic[v.x][v.y]==‘B‘){ 69 if(v.cnt>=5)continue; 70 else v.cnt++; 71 } 72 else if(pic[v.x][v.y]==‘P‘)v.d--; 73 v.d++; 74 if(vis[v.x][v.y][v.cnt])continue; 75 vis[v.x][v.y][v.cnt]=1; 76 // dd(v.x+1);dd(v.y+1);dd(pic[v.x][v.y]);dd(v.cnt);de(v.d); 77 q.push(v); 78 } 79 } 80 return INF; 81 } 82 int main() 83 { 84 // ifstream cin("in.txt"); 85 while(cin>>n>>m,n+m){ 86 int ans=INF; 87 int ok=0; 88 rep(i,0,n)rep(j,0,m){ 89 cin>>pic[i][j]; 90 } 91 rep(i,0,n)rep(j,0,m)if(pic[i][j]==‘S‘) 92 { 93 ans=bfs(i,j); 94 goto here; 95 } 96 here:; 97 if(ans!=INF)printf("%d\n",ans); 98 else puts("-1"); 99 } 100 return 0; 101 }
标签:状态 def type solution using 优先 bool \n har
原文地址:https://www.cnblogs.com/klaycf/p/9692333.html