hdu 1072(bfs)

一开始看着题目太长,不想读了,就问了下XSY题意。可惜交流了半天也没弄清楚具体细节问题...无奈又回过去重新读了遍题。看来读题还是得自己做啊...

   做的BFS题不多,队列的性质还没用熟练。

#include<cstdio>
#include<queue>
#include<cstring>
using  namespace std ;
int tur[ 4][ 2] = {{ 10}, {- 10}, { 01}, { 0, - 1}} ;
struct Point{
     int x, y, time, step ;
} ;
int map[ 10][ 10] ;
int mark[ 10][ 10] ;
int n, m, si, sj ;
void bfs(){
    Point begin ;
    begin.x = si ;
    begin.y = sj ;
    begin.time =  6 ;
    begin.step =  0 ;
    mark[si][sj] =  6 ;
    queue<Point> q ;
    q.push(begin) ;
     while(!q.empty()){
        Point p = q.front() ;
        q.pop() ;
         for( int k= 0; k< 4; k++){
            Point temp = p ;
            temp.x += tur[k][ 0] ;
            temp.y += tur[k][ 1] ;
             if(temp.x< 0||temp.x>=n||temp.y< 0||temp.y>=m||map[temp.x][temp.y]== 0)
                 continue ;
            temp.step ++ ;
            temp.time -- ;
             if(map[temp.x][temp.y]== 3){
                printf( " %d\n ", temp.step) ;
                 return ;
            }
             if(map[temp.x][temp.y]== 4){
                temp.time =  6 ;
            }
             if(temp.time> 1&&mark[temp.x][temp.y]<temp.time){
                mark[temp.x][temp.y] = temp.time;
                q.push(temp) ;
            }
        }
    }
    printf( " -1\n ") ;
     return ;
}
int main(){
     int i, j, t ;
    scanf( " %d ", &t) ;
     while(t--){
        scanf( " %d%d ", &n, &m) ;
        memset(mark,  0sizeof(mark)) ;
         for(i= 0; i<n; i++)
             for(j= 0; j<m; j++){
                scanf( " %d ", &map[i][j]) ;
                 if(map[i][j]== 2){
                    si = i ;
                    sj = j ;
                }
            }
        bfs() ;
    }
     return  0 ;

} 

你可能感兴趣的:(HDU)