POJ-2251 Dungeon Master
這道題呢其實就是一個迷宮問題,只不過從二維變成了三維而已
首先呢這道題是用寬搜做的,可以從其資料範圍看出
我本人一開始用深搜做,然後一直超時,看了資料範圍以後才用的寬搜(其實最短路時是最好用寬搜的)
#include<iostream>
#include<queue>
using namespace std;
queue<int>p1;//寬搜的佇列
queue<int>p2;
queue<int>p3;
int n,m,x,end1,end2,end3,a[31][31][31]={0},st1,st2,st3;
int b[3][6]= {{1,-1,0,0,0,0},{0,0,1,-1,0,0},{0,0,0,0,1,-1}};//6個方向
char s[31][31][31];
int search(){
for(int i=1;i<=30;i++){//初始化
for(int j=1;j<=30;j++){
for(int k=1;k<=30;k++){
a[i][j][k]=11111111;
}
}
}
a[st1][st2][st3]=0;//起點是0
p1.push(st1);
p2.push(st2);
p3.push(st3);
while(p1.size()&&p2.size()&&p3.size()){//當佇列不為空時就一直執行
int s1=p1.front();p1.pop();
int s2=p2.front();p2.pop();
int s3=p3.front();p3.pop();
if(s1==end1&&s2==end2&&s3==end3)break;
for(int i=0;i<=5;i++){
int t1=s1+b[0][i],t2=s2+b[1][i],t3=s3+b[2][i];
if(a[s1][s2][s3]+1<a[t1][t2][t3]&&t1<=n&&t1>=1&&t2<=m&&t2>=1&&t3<=x&&t3>=1&&s[t1][t2][t3]!='#'){
a[t1][t2][t3]=a[s1][s2][s3]+1;
p1.push(t1);p2.push(t2);p3.push(t3);
}
}
}
return a[end1][end2][end3];
}
int main() {
while(cin>>n>>m>>x&&n&&m&&x){
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
for(int k=1; k<=x; k++) {
cin>>s[i][j][k];
if(s[i][j][k]=='E') {//記錄終點
end1=i;
end2=j;
end3=k;
}
if(s[i][j][k]=='S') {//記錄開始點
st1=i;
st2=j;
st3=k;
}
}
}
}
int h=search();
if(h!=11111111)
cout<<"Escaped in "<<h<<" minute(s).";
else cout<<"Trapped!";
cout<<endl;
}
}
相關文章
- Dungeon Master(POJ-2251)AST
- 1248:Dungeon MasterAST
- POJ2251 Dungeon MasterAST
- POJ 2251-Dungeon Master(BFS-三維迷宮)AST
- 視訊遊戲之 Shattered Pixel Dungeon遊戲
- MySQL Master/Slave Master/MasterMySqlAST
- SGU 110 Dungeon(立體幾何)
- ! [rejected] master -> master (fetch first)AST
- Mysql5.6 Master+MasterMySqlAST
- 文字冒險遊戲AI Dungeon 故事由AI寫就遊戲AI
- Mongodb的master-slave模式與master-master模式實驗MongoDBAST模式
- 《AI Dungeon》開發者獲得投資,構建AI遊戲平臺AI遊戲
- change master 未指定master_log_fileAST
- Git branching: master vs. origin/master vs. remotes/origin/masterGitASTREM
- show master logs 和 show master status 區別AST
- 【HBase】start master 與 start master --backup 的區別AST
- git rebase masterGitAST
- Spark master standalongSparkAST
- Redis master and slaveRedisAST
- MySQL master/slaveMySqlAST
- Scrum Master JobGPTScrumASTGPT
- MySQL5.7 Master-Master主主搭建for Centos7MySqlASTCentOS
- 2 新增standby masterAST
- Master-Worker 模式AST模式
- Scrum Master 生存指南ScrumAST
- Master MVVM in Swift<1>ASTMVVMSwift
- Master Note for Tablespace IssuesAST
- Master-Worker模式AST模式
- 母版頁(Master Pages)AST
- MySql的Master/SlaveMySqlAST
- Master Data Management UpdateAST
- KSV master waitASTAI
- WPF master detail viewASTAIView
- C. Game MasterGAMAST
- DocumentDB 報錯“not master”AST
- git merge origin master git merge origin/master區別GitAST
- 對 Git 分支 master 和 origin/master 的一些認識GitAST
- 什麼是ITSM Master?AST