MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1253
题目描述:
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 0
11
题目分析:
代码如下:
/*
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;
}