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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU1429胜利大逃亡(续)HDU 1885 Key Task BFS+状态压缩+水

發(fā)布時間:2023/12/10 编程问答 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU1429胜利大逃亡(续)HDU 1885 Key Task BFS+状态压缩+水 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

HDU1429

只有10把鑰匙 1<<10足夠了

標(biāo)記用三維數(shù)組

用Linux好不習(xí)慣 繼續(xù)克服~

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAXN 11111 #include<queue> const int INF = 999999; char mp[33][33]; bool vis[33][33][1026]; int n,m; struct node {int x,y,step;int key; }; queue<node>q; int xx[4]= {1,0,-1,0}; int yy[4]= {0,1,0,-1}; int bfs(int x,int y) {while(!q.empty())q.pop();memset(vis,false,sizeof(vis));node front,rear;front.x=x,front.y=y;front.key=0;front.step=0;vis[x][y][0]=true;q.push(front);while(!q.empty()){front=q.front();q.pop();for(int i=0; i<4; i++){int dx=front.x+xx[i],dy=front.y+yy[i];if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='*'&&!vis[dx][dy][front.key]){if(mp[dx][dy]=='^')//到終點{return front.step+1;}else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿到鑰匙{vis[dx][dy][front.key]=true;//標(biāo)記rear.key =(front.key )| ( 1<<(mp[dx][dy]-'a'));//添加鑰匙rear.x=dx,rear.y=dy,rear.step=front.step+1;q.push(rear);}else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//遇到門{if(front.key&(1<<(mp[dx][dy]-'A')))//如果有對應(yīng)的鑰匙{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}else{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}}}return 0; } int main() {int t;// freopen("in.txt","r",stdin);while(scanf("%d%d%d",&n,&m,&t)!=EOF){node s;for(int i=0; i<n; i++)scanf("%s",mp[i]);for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(mp[i][j]=='@')s.x=i,s.y=j,s.step=0;}}int ans=bfs(s.x,s.y);if(ans==0||ans>=t)printf("-1\n");else printf("%d\n",ans);}return 0; }

HDU1885

這個就4把鑰匙

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAXN 11111 #include<queue> const int INF = 999999; char mp[133][133]; bool vis[133][133][33]; int n,m; int ke[28]; struct node {int x,y,step;int key; }; queue<node>q; int xx[4]= {1,0,-1,0}; int yy[4]= {0,1,0,-1}; int bfs(int x,int y) {while(!q.empty())q.pop();memset(vis,false,sizeof(vis));node front,rear;front.x=x,front.y=y;front.key=0;front.step=0;vis[x][y][0]=true;q.push(front);while(!q.empty()){front=q.front();q.pop();for(int i=0; i<4; i++){int dx=front.x+xx[i],dy=front.y+yy[i];if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='#'&&!vis[dx][dy][front.key]){if(mp[dx][dy]=='X')//到終點{return front.step+1;}else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿鑰匙{vis[dx][dy][front.key]=true;rear.key =(front.key )| ( 1<<(ke[mp[dx][dy]-'a']));rear.x=dx,rear.y=dy,rear.step=front.step+1;q.push(rear);}else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//開門{if(front.key&(1<<(ke[mp[dx][dy]-'A']))){rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}else{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}}}return 0; } int main() {//freopen("in.txt","r",stdin);ke['a'-'a']=0,ke['b'-'a']=1,ke['r'-'a']=2,ke['g'-'a']=3;while(scanf("%d%d",&n,&m),n+m){node s;for(int i=0; i<n; i++)scanf("%s",mp[i]);for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(mp[i][j]=='*')s.x=i,s.y=j,s.step=0;}}int ans=bfs(s.x,s.y);if(ans==0)printf("The poor student is trapped!\n");else printf("Escape possible in %d steps.\n",ans);}return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/kewowlo/p/4002588.html

總結(jié)

以上是生活随笔為你收集整理的HDU1429胜利大逃亡(续)HDU 1885 Key Task BFS+状态压缩+水的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。