HDU - 1253 胜利大逃亡(搜索)
Ignatius被魔王抓走了,有一天魔王出差去了,這可是Ignatius逃亡的好機會.?
魔王住在一個城堡里,城堡是一個A*B*C的立方體,可以被表示成A個B*C的矩陣,剛開始Ignatius被關(guān)在(0,0,0)的位置,離開城堡的門在(A-1,B-1,C-1)的位置,現(xiàn)在知道魔王將在T分鐘后回到城堡,Ignatius每分鐘能從一個坐標走到相鄰的六個坐標中的其中一個.現(xiàn)在給你城堡的地圖,請你計算出Ignatius能否在魔王回來前離開城堡(只要走到出口就算離開城堡,如果走到出口的時候魔王剛好回來也算逃亡成功),如果可以請輸出需要多少分鐘才能離開,如果不能則輸出-1.?
?
Input
輸入數(shù)據(jù)的第一行是一個正整數(shù)K,表明測試數(shù)據(jù)的數(shù)量.每組測試數(shù)據(jù)的第一行是四個正整數(shù)A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它們分別代表城堡的大小和魔王回來的時間.然后是A塊輸入數(shù)據(jù)(先是第0塊,然后是第1塊,第2塊......),每塊輸入數(shù)據(jù)有B行,每行有C個正整數(shù),代表迷宮的布局,其中0代表路,1代表墻.(如果對輸入描述不清楚,可以參考Sample Input中的迷宮描述,它表示的就是上圖中的迷宮)?
特別注意:本題的測試數(shù)據(jù)非常大,請使用scanf輸入,我不能保證使用cin能不超時.在本OJ上請使用Visual C++提交.?
Output
對于每組測試數(shù)據(jù),如果Ignatius能夠在魔王回來前離開城堡,那么請輸出他最少需要多少分鐘,否則輸出-1.?
Sample Input
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0Sample Output
11三維搜索,數(shù)據(jù)量不大,利用不超過T的時間剪枝即可,優(yōu)化時間。
因為是多組輸入所以慎用return 0,wa了7遍。
#include<iostream> #include<queue> #include<algorithm> #include<set> #include<cmath> #include<vector> #include<map> #include<stack> #include<bitset> #include<cstdio> #define Swap(a,b) a^=b^=a^=b #define cini(n) scanf("%d",&n) #define cinl(n) scanf("%lld",&n) #define cinc(n) scanf("%c",&n) #define coui(n) printf("%d",n) #define couc(n) printf("%c",n) #define coul(n) printf("%lld",n) #define speed ios_base::sync_with_stdio(0); #define Max(a,b) a>b?a:b #define Min(a,b) a<b?a:b using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=1e6+10; const double esp=1e-9; int m,n,s,x,y,z,A,B,C; int ob[51][51][51]; struct lo {int x,y,z,cnt;lo(int a,int b,int c,int cnt ):x(a),y(b),z(c),cnt(cnt){} }; int fx[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}}; int main() {int T;cini(T);while(T--){cini(A);cini(B);cini(C);cini(s);for(int i=0;i<A;i++)for(int j=0;j<B;j++)for(int k=0;k<C;k++)cini(ob[i][j][k]);//while(cin>>x>>y>>z) cout<<ob[x][y][z]<<endl;queue<lo> q;while(!q.empty()) q.pop();q.push(lo(0,0,0,0));ob[0][0][0]=1;while(!q.empty()){lo tem=q.front();int a=tem.x;int b=tem.y;int c=tem.z;int cnt=tem.cnt;if(a==A-1&&b==B-1&&c==C-1){printf("%d\n",cnt);goto loop;}q.pop();for(int i=0;i<6;i++){if(a+fx[i][0]<0||a+fx[i][0]>=A||b+fx[i][1]<0||b+fx[i][1]>=B||c+fx[i][2]>=C||c+fx[i][2]<0) continue;if(ob[a+fx[i][0]][b+fx[i][1]][c+fx[i][2]]==0&&cnt+1<=s){// cout<<a+fx[i][0]<<' '<<b+fx[i][1]<<' '<<c+fx[i][2]<<endl;ob[a+fx[i][0]][b+fx[i][1]][c+fx[i][2]]=1;q.push(lo(a+fx[i][0],b+fx[i][1],c+fx[i][2],cnt+1));}}}printf("-1\n");loop:continue;}}?
總結(jié)
以上是生活随笔為你收集整理的HDU - 1253 胜利大逃亡(搜索)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电动化业务等推动,现代摩比斯 2022
- 下一篇: 疯子的算法总结(八) 最短路算法+模板