zoukankan      html  css  js  c++  java
  • 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 ;

    } 

  • 相关阅读:
    redis之哨兵配置-2
    redis之主从配置-1
    MYSQL 在当前时间加上或减去一个时间段
    【转】将long数字序列化为json时,转换为字符串
    从技术到产品,从职场到创业,我这7年的痕迹
    可能是东半球第二好用的软件工具全部在这里(update in 2020.10.09)
    Java架构师面试题答案2020备忘录
    学习方法
    《精力管理》管理精力,而非时间
    《道德经》全文
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2264653.html
Copyright © 2011-2022 走看看