• hdu 1180


    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1180

    题意:中文。

    mark:题目没说明楼梯改变情况。sample中的路径应该是先向上走,再向右走,通过楼梯后走到头再向上,这里wa了1次。

    bfs+优先队列。优先队列用手写堆实现。数据规模很小。

    代码:

    # include <stdio.h>
    # include <string.h>


    char graph[25][25] ;
    int dr[4][2] = {0,1,0,-1,1,0,-1,0} ;
    int sx, sy, ex, ey ;
    int vis[25][25] ;
    int q[25*25*10][3], qcnt ;
    int n, m ;


    void swap(int a[3], int b[3])
    {
    int i, t ;
    for( i = 0 ; i < 3 ; i++)
    t = a[i], a[i] = b[i], b[i] = t ;
    }


    void qpush(int x, int y, int t)
    {
    int idx = qcnt++ ;
    q[idx][0] = x, q[idx][1] = y, q[idx][2] = t ;
    while (idx != 1)
    {
    if (q[idx][2] < q[idx/2][2])
    swap(q[idx], q[idx/2]) ;
    else break ;
    idx /= 2 ;
    }
    }


    void heapify(int idx)
    {
    int l = idx*2, r=idx*2+1, least = idx ;
    if (l<qcnt && q[l][2]<q[least][2])
    least = l ;
    if (r<qcnt && q[r][2]<q[least][2])
    least = r ;
    if (least == idx) return ;
    swap(q[least], q[idx]) ;
    heapify(least) ;
    }


    void qpop(int *px, int *py, int *pt)
    {
    *px = q[1][0], *py = q[1][1], *pt = q[1][2] ;
    swap(q[1], q[--qcnt]) ;
    heapify(1) ;
    }


    int bfs()
    {
    int i, front = 0, rear = 2 ;
    int x, y, t, xx, yy ;
    vis[sx][sy] = 1 ;
    qpush(sx, sy, 0) ;
    qpush(sx, sy, 1) ;
    while (qcnt != 1)
    {
    qpop(&x, &y, &t) ;
    printf ("%d %d %d\n", x, y, t) ;
    if (x == ex && y == ey) return t ;
    for(i = 0 ; i < n ;i++)
    {
    xx = x + dr[i][0], yy = y + dr[i][1] ;
    if (xx < 0 || xx >= n) continue ;
    if (yy < 0 || yy >= m) continue ;
    //楼梯改变坐标
    if (graph[xx][yy] == '-' || graph[xx][yy] == '|')
    {
    if (i>=2) //左右
    {
    if (((t%2 == 0) && (graph[xx][yy] == '|')) ||
    ((t%2 == 1) && (graph[xx][yy] == '-')))
    {
    xx += dr[i][0] ;
    yy += dr[i][1] ;
    }
    else continue ;
    }
    else //上下
    {
    if (((t%2 == 0) && (graph[xx][yy] == '-')) ||
    ((t%2 == 1) && (graph[xx][yy] == '|')))
    {
    xx += dr[i][0] ;
    yy += dr[i][1] ;
    }
    else continue ;
    }
    if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue ;
    }
    if (graph[xx][yy] == '*') continue ;
    if (vis[xx][yy]) continue ;
    vis[xx][yy] = 1 ;
    qpush(xx, yy, t+1) ;
    qpush(xx, yy, t+2) ;
    }
    }
    return -1 ;
    }


    int main ()
    {
    int i, j, ans ;
    while (~scanf ("%d %d%*c", &n, &m))
    {
    for (i = 0 ; i <n ; i++)
    scanf ("%s%*c", graph[i]) ;
    for(i = 0 ; i < n ; i++)
    for (j = 0 ; j < m ; j++)
    {
    if (graph[i][j] == 'S') sx = i, sy = j ;
    else if (graph[i][j] == 'T') ex = i, ey = j ;
    }
    memset (vis, 0, sizeof(vis)) ;
    qcnt = 1 ;
    ans = bfs() ;
    printf ("%d\n", ans) ;
    }
    return 0 ;
    }



  • 相关阅读:
    Python异常处理
    奇异值分解(SVD)详解及其应用
    上楼梯问题
    Python面向对象(特殊成员)
    Best Time to Buy and Sell Stock II
    String to Integer (atoi)
    Gas Station
    N-Queens II
    Letter Combinations of a Phone Number
    N-Queens
  • 原文地址:https://www.cnblogs.com/lzsz1212/p/2327721.html
Copyright © 2020-2023  润新知