Wappo BFS求最短路+路径记录
點擊打開鏈接
描述
Einstein最近迷上了一款手機益智游戲Wappo,但是由于IQ和RP等諸多原因,他一直無法通關,他希望你編一個程序來玩這個游戲。
Wappo的游戲規則是這樣的:在一張m*n的地圖上,你可以控制勇士每秒向上下左右任意方向移動一格,之后怪獸會朝著你的方向移動至多兩格。
注意,怪獸會優先橫向走,勇士和怪獸都不會穿墻。
勇士的目的地就是橋頭,但是千萬不能被怪獸吃掉。
陷阱是很有用的東西(一格),勇士是不能進入陷阱的,但是如果你的IQ足夠高,就可以把怪獸引入陷阱,在接下來你的三次移動中,怪獸將無法移動,三秒后恢復正常。
現在給你地圖的信息,請你用最少的步數走到橋上。
格式
輸入格式
第一行是兩個正整數m,n(m,n<=20),表示地圖的大小為m*n.
接下來是n行,每行m個整數,表示一個格子周圍墻的信息。其中
1-上方有墻
2-左方有墻
4-右方有墻
8-下方有墻
例如,一個格子的左、上、右都有墻,那么代表這個格子的整數是2+1+4=7。
接下來是n行,每行m個字符,表示一個格子里的其他信息,其中
.-nothing
S-勇士的初始位置
W-陷阱
M-怪獸的初始位置
E-目的地,即橋頭
其中S,M,E均保證只出現一次。
輸出格式
輸出包含若干行,前r行為最少步數走到橋頭的走法,每行為up,down,left,right中的一個,表示勇士的走法。最后一行輸出最少步數r,格式見樣例。
若存在多解,按照上左右下的優先順序行走。
若無法走到橋頭,只輸出一行impossible。
樣例1
樣例輸入1
6 6 0 8 8 8 8 0 4 3 1 1 5 2 4 2 0 4 6 2 4 2 0 4 6 2 4 10 8 8 12 2 0 1 1 1 1 0 ...... .S..W. ....M. ...... ...E.. ...... Copy樣例輸出1
right right down down down total steps: 5 Copy提示
友情提示:怪獸掉進陷阱以后,如果沒有走出來,不會繼續掉進去。
分析:
(1)怪獸被困陷阱里到時間了以后如果沒有移動不算再掉進去(那樣就永遠出不來了);
(2)在人走之后要判斷是否到終點(如果已到終點則不用考慮怪獸了,只不過有的Wappo版本里即使人走到終點也不算過關,需要怪獸進行最后一次操作,若沒有抓到人才算過關)以及是否直接碰到怪獸(那不是自殺?)
(3)怪獸在不能橫著走的情況下會豎著走,如果不能豎著走的話就不動
(怪獸有點SB,如果人在它左上角則它只會向左或向上)
關于怪獸的行走邏輯,分兩個階段:
走第一步,如果怪獸與你的水平方向不一致,那么怪獸會企圖往豎直方向走動使得他與你的水平方向一致
這時侯可能豎直方向有墻(沒有墻就走過去進入第二階段)
那么怪獸就會往水平方向企圖走,如果沒有墻就走,否則原地不動
然后不再走第二步。
走第二步,和第一步類似,需要考慮優先級和可行性
#include <bits/stdc++.h> using namespace std; #define FOR(i,n) for (int i=1;i<=n;i++) #define REP(i,a,b) for (int i=a;i<=b;i++) #define pb push_back #define mp make_pair #define ll long long #define pos(x,y) (x+(y)*n) const int N=100000+10; const int inf=0x3f3f3f3f; const ll mod=1000000007; const double eps=1e-8;int n,m; int a[25][25]; int b[25][25]; struct node { int x,y,x2,y2,t; int pre; int d; int dir; // t為陷入陷阱后還剩幾秒鐘出來,t=0表示沒陷入陷阱 void operator=(node a) {x=a.x,y=a.y,x2=a.x2,y2=a.y2,t=a.t;pre=a.pre;d=a.d;dir=a.dir; } }; bool vis[25][25][25][25][5];//狀態 node q[400*400*3+10]; // x,y,x2,y2,t int head,tail; int X,Y,X2,Y2,EX,EY; bool ok(int x,int y) { return 1<=x&&x<=n&&1<=y&&y<=m&&!b[x][y]; } void print(node a) { vector<int> v; int steps=a.d; while (1) { if (a.pre==0) break; v.pb(a.dir); a=q[a.pre]; //cout<<a.x<<" "<<a.y<<endl; } while (v.size()) { if (v.back()==1) cout<<"up"<<endl; if (v.back()==2) cout<<"left"<<endl; if (v.back()==3) cout<<"right"<<endl; if (v.back()==4) cout<<"down"<<endl; v.pop_back(); } cout<<"total steps: "<<steps<<endl; } void work(int &x,int &y,int &x2,int &y2,node &tmp) { if (tmp.t) tmp.t--; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } } } } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; else { if (y2<y) { if (!(a[x2][y2]&4)) { y2++; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (y2>y) { if (!(a[x2][y2]&2)) { y2--; if (b[x2][y2]) tmp.t=3; } else { if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } else if (x2>x) { if (!(a[x2][y2]&1)) { x2--; if (b[x2][y2]) tmp.t=3; } } else if (x2<x) { if (!(a[x2][y2]&8)) { x2++; if (b[x2][y2]) tmp.t=3; } } } } } } if (!(x==EX&&y==EY)&&x==x2&&y==y2) return; if (!vis[tmp.x][tmp.y][tmp.x2][tmp.y2][tmp.t]) { vis[tmp.x][tmp.y][tmp.x2][tmp.y2][tmp.t]=1; q[++tail]=tmp; } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>m>>n; FOR(i,n) FOR(j,m) cin>>a[i][j]; FOR(i,n) FOR(j,m) { char t='\n'; while (t=='\n') cin>>t; if (t=='S') X=i,Y=j; else if (t=='W') b[i][j]=1; else if (t=='M') X2=i,Y2=j; else if (t=='E') EX=i,EY=j; } head=tail=1; q[1]=node{X,Y,X2,Y2,0,0,0,0}; vis[X][Y][X2][Y2][0]=1; while (head<=tail) { node u=q[head++]; if (u.x==EX&&u.y==EY) { print(u); return 0; } node tmp; tmp=u; tmp.d++; tmp.pre=head-1; if (!(a[tmp.x][tmp.y]&1)&&ok(tmp.x-1,tmp.y)&&!(tmp.x2==tmp.x-1&&tmp.y2==tmp.y)) { tmp.x--,tmp.dir=1; work(tmp.x,tmp.y,tmp.x2,tmp.y2,tmp); } tmp=u; tmp.d++; tmp.pre=head-1; if (!(a[tmp.x][tmp.y]&2)&&ok(tmp.x,tmp.y-1)&&!(tmp.x2==tmp.x&&tmp.y2==tmp.y-1)) { tmp.y--,tmp.dir=2; work(tmp.x,tmp.y,tmp.x2,tmp.y2,tmp); } tmp=u; tmp.d++; tmp.pre=head-1; if (!(a[tmp.x][tmp.y]&4)&&ok(tmp.x,tmp.y+1)&&!(tmp.x2==tmp.x&&tmp.y2==tmp.y+1)) { tmp.y++,tmp.dir=3; work(tmp.x,tmp.y,tmp.x2,tmp.y2,tmp); } tmp=u; tmp.d++; tmp.pre=head-1; if (!(a[tmp.x][tmp.y]&8)&&ok(tmp.x+1,tmp.y)&&!(tmp.x2==tmp.x+1&&tmp.y2==tmp.y)) { tmp.x++,tmp.dir=4; work(tmp.x,tmp.y,tmp.x2,tmp.y2,tmp); } } cout<<"impossible\n"<<endl; return 0; }
總結
以上是生活随笔為你收集整理的Wappo BFS求最短路+路径记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1001 Easy h-index
- 下一篇: 路痴的单身小菡 BFS求最短路径+DFS