zoukankan      html  css  js  c++  java
  • [恢]hdu 1242

    2011-12-25 09:54:46

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

    题意:angel被困在迷宫里,他的朋友们去救。n*m的迷宫,'.'代表路,'#'代表墙,'x'代表守卫,'r'代表朋友,'a'代表ANGEL。求最少时间。

    mark:BFS。注意有多个“朋友”,所以从a开始搜。

    代码:

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


    int n, m, sx, sy ;
    char graph[210][210] ;
    int dp[210][210] ;
    int q[40010] ;


    int bfs()
    {
    int front = 0, rear = 0 ;
    int i, x, y, xx, yy, next ;
    int tab[4][2] = {0, 1, 0, -1, 1, 0, -1, 0} ;
    memset (dp, -1, sizeof(dp)) ;
    dp[sx][sy] = 0 ;
    q[rear++] = sx*m+sy ;
    while (front != rear)
    {
    x = q[front] / m ;
    y = q[front++] % m ;
    if (graph[x][y] == 'r') return dp[x][y] ;
    for (i = 0 ; i < 4 ; i++)
    {
    xx = x + tab[i][0] ;
    yy = y + tab[i][1] ;
    if (xx < 0 || xx >= n || yy < 0 || yy >= m)
    continue ;
    if (graph[xx][yy] == '#') continue ;
    if (graph[xx][yy] == 'x') next = 2 ;
    else next =1 ;
    next += dp[x][y] ;
    if (dp[xx][yy]!= -1 &&dp[xx][yy]<=next) continue ;
    dp[xx][yy] = next ;
    q[rear++] = xx * m + yy ;
    }
    }
    return -1 ;
    }


    int min(int a, int b){return a<b?a:b;}


    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] == 'a')
    sx = i, sy = j ;
    }
    ans = bfs () ;
    if (ans == -1)
    puts ("Poor ANGEL has to stay in the prison all his life.") ;
    else printf ("%d\n", ans) ;
    }
    return 0 ;
    }



  • 相关阅读:
    调用微信上传图片的接口
    jqgrid取消列排序
    jqGrid动态添加列
    jqgrid多次调用合并表头出现重叠的处理
    echarts3.0版本断点连线的处理
    JAVA数据转换常用方法
    Java面试常见各种概念区别比较
    Python从零开始(1)新手常问
    记录一下11月份的面试
    Centos7 下安装 mysql8
  • 原文地址:https://www.cnblogs.com/lzsz1212/p/2315344.html
Copyright © 2011-2022 走看看