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 ;

    } 

  • 相关阅读:
    贪心法解活动安排问题
    求两个等长的已排序数组的中位数(《算法导论》P113习题9.3-8)
    0-1背包问题
    动态规划法解最长公共子序列问题
    动态规划法解矩阵链乘问题
    常见的算法设计策略
    A*算法与8数字谜题(参见《算法》P226习题2.5.32)
    keepalive+redis 主从高可用
    sharding-jdbc 读写分离+水平分表
    hash一致算法原理
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2264653.html
Copyright © 2011-2022 走看看