hdu 1253(bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1253

   题目不难,简单的BFS加一点剪枝,不过让人郁闷的是用G++提交一直CE,用C++一次AC。最后还是XSY发现的问题,将我保存时间的变量time改成了T然后就A掉了...

   这下是记住了,以后再也不用time这名了!

 

 

#include<cstdio>
#include<queue>
using  namespace std ;
int a, b, c, T;
int map[ 51][ 51][ 51] ;
int tur[ 6][ 3] = {- 1001000, - 1001000100, - 1} ;
struct node{
     int x ;
     int y ;
     int z ;
     int curtime ;
};
void bfs(){
    queue<node> q ;
    node begin ;
    begin.x =  0 ;
    begin.y =  0 ;
    begin.z =  0 ;
    begin.curtime =  0 ;
    map[ 0][ 0][ 0] =  1 ;
    q.push(begin) ;
     while(!q.empty()){
        node p = q.front() ;
        q.pop() ;
         for( int i= 0; i< 6; i++){
            node temp = p ;
            temp.x += tur[i][ 0] ;
            temp.y += tur[i][ 1] ;
            temp.z += tur[i][ 2] ;
             if(temp.x==a- 1&&temp.y==b- 1&&temp.z==c- 1&&temp.curtime<=T){
                printf( " %d\n ", temp.curtime+ 1) ;
                 return ;
            }

             if(temp.x>= 0&&temp.x<a&&temp.y>= 0&&temp.y<b&&temp.z>= 0&&temp.z<c&&!map[temp.x][temp.y][temp.z]){
                map[temp.x][temp.y][temp.z] =  1 ;
                temp.curtime ++ ;
                q.push(temp) ;
            }
        }
    }
    printf( " -1\n ") ;
     return ;
}
int main(){
     int t ;
    scanf( " %d ", &t) ;
     while(t--){
        scanf( " %d%d%d%d ", &a, &b, &c, &T) ;
         for( int i= 0; i<a; i++)
             for( int j= 0; j<b; j++)
                 for( int k= 0; k<c; k++)
                    scanf( " %d ", &map[i][j][k]) ;
         if(map[a- 1][b- 1][c- 1]||a+b+c- 3>T){ //剪枝
            printf( " -1\n ") ;
             continue ;
        }
         if(a== 1&&b== 1&&c== 1){
            printf( " 0\n ") ;
             continue ;
        }
        bfs() ;
    }
     return  0 ;
}

 

你可能感兴趣的:(HDU)