HDOJ 1253 HDU 1253 胜利大逃亡 ACM 1253 IN HDU
MiYu原創, 轉帖請注明 : 轉載自?______________白白の屋????
?
?題目地址:
?? ? ?http://acm.hdu.edu.cn/showproblem.php?pid=1253?
題目描述:
勝利大逃亡
Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4785????Accepted Submission(s): 1454
Problem DescriptionIgnatius被魔王抓走了,有一天魔王出差去了,這可是Ignatius逃亡的好機會.
魔王住在一個城堡里,城堡是一個A*B*C的立方體,可以被表示成A個B*C的矩陣,剛開始Ignatius被關在(0,0,0)的位置,離開城堡的門在(A-1,B-1,C-1)的位置,現在知道魔王將在T分鐘后回到城堡,Ignatius每分鐘能從一個坐標走到相鄰的六個坐標中的其中一個.現在給你城堡的地圖,請你計算出Ignatius能否在魔王回來前離開城堡(只要走到出口就算離開城堡,如果走到出口的時候魔王剛好回來也算逃亡成功),如果可以請輸出需要多少分鐘才能離開,如果不能則輸出-1.
Input輸入數據的第一行是一個正整數K,表明測試數據的數量.每組測試數據的第一行是四個正整數A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它們分別代表城堡的大小和魔王回來的時間.然后是A塊輸入數據(先是第0塊,然后是第1塊,第2塊......),每塊輸入數據有B行,每行有C個正整數,代表迷宮的布局,其中0代表路,1代表墻.(如果對輸入描述不清楚,可以參考Sample Input中的迷宮描述,它表示的就是上圖中的迷宮)
特別注意:本題的測試數據非常大,請使用scanf輸入,我不能保證使用cin能不超時.在本OJ上請使用Visual C++提交.
Output對于每組測試數據,如果Ignatius能夠在魔王回來前離開城堡,那么請輸出他最少需要多少分鐘,否則輸出-1.
Sample Input1 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 0
Sample Output11
?
?題目分析:
?? ?題目是很簡單的一道BFS, 但是這破題竟然糾結了我一整天, 晚上11點的時候才A掉,YM. ?這題和 二維矩陣的不同點就是有6個方向, 前后左右上下. ?然后一直BFS就可以了,不需要剪枝都能過...........
代碼如下:
?/*
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
?? ? ? ? ?http://www.cnblog.com/MiYu
Author By : MiYu
Test ? ? ?: 1
Program ? : HDU1253
*/
#include <iostream>
#include <queue>
using namespace std;
int TLE[56][56][56];
const int d[6][3] = { { 0,0,1 },{ 0,0,-1 },{ 0,-1,0 },{ 0,1,0 },{ 1,0,0 },{ -1,0,0 } };
int A, B, C, T, m;
typedef struct pos{
?? ? ? pos(){ x=y=z=n=0; }?
?? ? ? void setPos ( int a,int b,int c, int count ){ x=a;y=b;z=c;n=count; }
?? ? ? bool isEnd () { if ( x==1&&y==1&&z==1 )return true;return false; }
?? ? ? int x,y,z; ?
?? ? ? int n;
}pos;
pos t,p;
#define CMP(A,B) (A.n < B.n)
typedef class Heap {
public:
?? ? ? ?pos h[70000 * 2];
?? ? ? ?int n, p, c;
?? ? ? ?Heap() {
?? ? ? ? ? ? ? ?n = 0;
?? ? ? ?}
?? ? ? ?void inline push(pos e) {
?? ? ? ? ? ? ? ?for (p = ++n; p > 1 && CMP(e,h[p>>1]); h[p] = h[p>>1], p >>= 1)
?? ? ? ? ? ? ? ? ? ? ? ?;
?? ? ? ? ? ? ? ?h[p] = e;
?? ? ? ?}
?? ? ? ?int inline pop(pos &e) {
?? ? ? ? ? ? ? ?if (!n)
?? ? ? ? ? ? ? ? ? ? ? ?return 0;
?? ? ? ? ? ? ? ?for (e = h[p = 1], c = 2; c < n
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?&& CMP(h[c += (CMP(h[c + 1],h[c]) && c < n - 1)], h[n]);
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?h[p] = h[c], p = c, c <<= 1)
?? ? ? ? ? ? ? ? ? ? ? ?;
?? ? ? ? ? ? ? ?h[p] = h[n--];
?? ? ? ? ? ? ? ?return 1;
?? ? ? ?}
}Heap;
Heap WA;
int RE ()
{
?? ?if ( A+B+C-2 > T || TLE[A][B][C] == 0 )
?? ? ? ? return -1;
?? ?t.setPos ( A,B,C,0 );
?? ?TLE[A][B][C] = 0;
?? ?WA.push ( t );
?? ?while ( WA.pop(t) ){?
?? ? ? ? ? if ( t.x+t.y+t.z-2 > T-t.n )
?? ? ? ? ? ? ? ?continue; ? ? ? ? ? ?
?? ? ? ? ? if ( t.isEnd() )
?? ? ? ? ? ? ? ?return t.n; ?
?? ? ? ? ? for ( int i = 0; i != 6; ++ i ){
?? ? ? ? ? ? ? ? int x = t.x+d[i][0], y=t.y+d[i][1], z=t.z+d[i][2];
?? ? ? ? ? ? ? ? if ( TLE[ x ][ y ][ z ] != 0 ){
?? ? ? ? ? ? ? ? ? ? ?TLE[ x ][ y ][ z ] = 0;
?? ? ? ? ? ? ? ? ? ? ?p.setPos ( x, y, z, t.n+1 );?
?? ? ? ? ? ? ? ? ? ? ?if ( p.isEnd() && p.n <= T )
?? ? ? ? ? ? ? ? ? ? ? ? ? return p.n; ?
?? ? ? ? ? ? ? ? ? ? ?if ( p.n <= T ) ? ?
?? ? ? ? ? ? ? ? ? ? ? ? ? WA.push ( p );
?? ? ? ? ? ? ? ? }
?? ? ? ? ? } ?
?? ?}
?? ?return -1;
}
inline bool scan_d(int &num)?
{
?? ? ? ?char in;bool IsN=false;
?? ? ? ?in=getchar();
?? ? ? ?if(in==EOF) return false;
?? ? ? ?while(in!='-'&&(in<'0'||in>'9')) in=getchar();
?? ? ? ?if(in=='-'){ IsN=true;num=0;}
?? ? ? ?else num=in-'0';
?? ? ? ?while(in=getchar(),in>='0'&&in<='9'){
?? ? ? ? ? ? ? ?num*=10,num+=in-'0';
?? ? ? ?}
?? ? ? ?if(IsN) num=-num;
?? ? ? ?return true;
}
int main ()
{
?? ?int K;
?? ?scan_d(K);
?? ?while ( K -- ){
?? ? ? ? ? while ( WA.pop(p) ) ;
?? ? ? ? ? memset ( TLE, 0 , sizeof ( TLE ) );
?? ? ? ? ? scan_d(A); scan_d(B); scan_d(C); scan_d(T);
?? ? ? ? ? for ( int i = 1; i <= A; ++ i ){
?? ? ? ? ? ? ? ? for ( int j = 1; j <= B; ++ j ){
?? ? ? ? ? ? ? ? ? ? ? for ( int k = 1; k <= C; ++ k ){
?? ? ? ? ? ? ? ? ? ? ? ? ? ? scan_d(m);
?? ? ? ? ? ? ? ? ? ? ? ? ? ? TLE[i][j][k] = m == 1 ? 0 : 1;?
?? ? ? ? ? ? ? ? ? ? ? }?
?? ? ? ? ? ? ? ? }?
?? ? ? ? ? }?
?? ? ? ? ? TLE[1][1][1] = 1;
?? ? ? ? ? cout << RE () << endl;
?? ?}?
?? ?//system( "pause" );
?? ?return 0;
}
?
?
轉載于:https://www.cnblogs.com/MiYu/archive/2010/08/20/1804162.html
總結
以上是生活随笔為你收集整理的HDOJ 1253 HDU 1253 胜利大逃亡 ACM 1253 IN HDU的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDOJ HDU 1106 排序 ACM
- 下一篇: eclipse rcp 多线程