日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Wappo BFS求最短路+路径记录

發布時間:2024/10/6 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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求最短路+路径记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人毛片100免费观看 | av网站在线免费观看 | 伊人网中文字幕 | 亚洲午夜小视频 | 99毛片| 精品国产伦一区二区三区 | 一本一道久久综合狠狠老精东影业 | 2025国产精品| 国产av一区不卡 | 久月婷婷 | 台湾佬美性中文娱乐网 | 中文字幕91在线 | 婷婷.com| 欧美一级全黄 | 欧美三级电影在线观看 | 好男人www | 美女又爽又黄视频毛茸茸 | 亚洲午夜久久久久久久久久久 | 日韩大片免费观看视频播放 | 在线免费观看视频a | 亚洲欧洲视频在线观看 | 两女双腿交缠激烈磨豆腐 | 亚洲资源在线 | 国产男男gay | 日韩高清不卡在线 | 蜜桃久久久久久久 | 天堂在线一区 | 国产精品乱码久久久久久久久 | 老汉av在线| 韩日一区二区三区 | 久久国产成人 | 欧美亚洲一级片 | 在线v | 欧美视频在线观看一区二区 | 久久视频在线 | 穿越异世荒淫h啪肉np文 | 蕾丝视频污 | 国产福利一区在线观看 | 久久神马影院 | 粉嫩一区 | 在线手机av | 日韩女优在线视频 | 日本极品喷水 | 99久久99久久 | 另类天堂av | 日韩五月 | 午夜精品小视频 | 91桃色视频在线观看 | 黑人毛片网站 | 国产精品色 | 男人激烈吮乳吃奶爽文 | 超碰黑人 | 国产又黄又粗又猛又爽视频 | 乱亲女h秽乱长久久久 | 日韩在线观看中文字幕 | 精品在线播放 | 已满十八岁免费观看 | 日本69少妇 | 色窝窝无码一区二区三区成人网站 | 日韩毛片基地 | 打开免费观看视频在线播放 | 爱情岛论坛永久入址测速 | 这里都是精品 | 天堂福利在线 | 在线中文字幕播放 | 玖玖热在线视频 | 久久亚洲精精品中文字幕早川悠里 | 三级成人网 | 精品国产一二区 | 一区二区三区视频在线观看免费 | 精品九九在线 | 韩日av在线 | 国产三级在线免费观看 | 91在线视频| 亚洲av成人精品一区二区三区 | 欧美日韩高清在线 | 90岁老太婆乱淫 | 日韩成人av一区 | 亚洲美女自拍 | 性色AV无码久久一区二区三 | 人人人干| 亚洲网站一区 | 精品福利电影 | 亚洲黄色在线观看视频 | 人妻互换一区二区三区四区五区 | 91成人毛片| 人人97| 免费看久久 | 久久99热这里只有精品 | 操极品美女 | 久久99精品国产麻豆91樱花 | 成人免费播放视频 | 日韩三级av在线 | 亚洲免费影视 | 九九热视 | 欧洲精品码一区二区三区免费看 | 欧美在线性爱视频 | 精品福利三区3d卡通动漫 | 亚洲网址在线观看 |