zoukankan      html  css  js  c++  java
  • HDU 2612 Find a Way

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2612

    题目大意:Y,M往@走,问走到最近的@耗时多少。上下左右移动,移动一次11分钟。

    解题思路:上来直接枚举@宽搜到Y,M距离记录最小值果断wa,果然搜索还是太耗时了。宽搜两次记录Y到每个@的距离和M到每个@的距离,然后枚举每个点,是@就计算,记录最小值。

    这道题最有趣的地方莫过于要脑补限制:

    M Y是不能通过的

    @是可以通过的

    代码:

     1 const int maxn = 500;
     2 const int iadd[] = {0, 1, 0, -1}, jadd[] = {1, 0, -1, 0};
     3 char maze[maxn][maxn];
     4 int vis[maxn][maxn], disy[maxn][maxn], dism[maxn][maxn];
     5 int n, m;
     6 struct node{
     7     int i, j, t;
     8 };
     9 
    10 void bfs1(int sti, int stj){
    11     memset(vis, 0, sizeof(vis));
    12     memset(disy, 0x3f, sizeof(disy));
    13     node u; u.i = sti; u.j = stj; u.t = 0;
    14     queue<node> q;
    15     q.push(u);
    16     vis[u.i][u.j] = 1; disy[u.i][u.j] = 0;
    17     while(!q.empty()){
    18         u = q.front(); q.pop();
    19         for(int i = 0; i < 4; i++){
    20             node v = u;
    21             v.i += iadd[i]; v.j += jadd[i]; v.t += 1;
    22             if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;
    23             if(vis[v.i][v.j]) continue; 
    24             if(maze[v.i][v.j] == '#') continue;
    25             if(maze[v.i][v.j] == 'M') continue;
    26             q.push(v);
    27             disy[v.i][v.j] = v.t;
    28             vis[v.i][v.j] = 1;
    29         }
    30     }
    31 }
    32 
    33 void bfs2(int sti, int stj){
    34     memset(vis, 0, sizeof(vis));
    35     memset(dism, 0x3f, sizeof(dism));
    36     node u; u.i = sti; u.j = stj; u.t = 0;
    37     queue<node> q;
    38     q.push(u);
    39     vis[u.i][u.j] = 1; dism[u.i][u.j] = 0;
    40     while(!q.empty()){
    41         u = q.front(); q.pop();
    42         for(int i = 0; i < 4; i++){
    43             node v = u;
    44             v.i += iadd[i]; v.j += jadd[i]; v.t += 1;
    45             if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;
    46             if(vis[v.i][v.j]) continue; 
    47             if(maze[v.i][v.j] == '#') continue;
    48             if(maze[v.i][v.j] == 'Y') continue;
    49             q.push(v);
    50             dism[v.i][v.j] = v.t;
    51             vis[v.i][v.j] = 1;
    52         }
    53     }
    54 }
    55 
    56 int main(){
    57     while(scanf("%d %d", &n, &m) != EOF){
    58         memset(vis, 0, sizeof(vis));
    59         int yi, yj, mi, mj;
    60         for(int i = 0; i < n; i++){
    61             for(int j = 0; j < m; j++){
    62                 scanf(" %c", &maze[i][j]);
    63                 if(maze[i][j] == 'Y') yi = i, yj = j;
    64                 if(maze[i][j] == 'M') mi = i, mj = j;
    65             }
    66         }
    67         bfs1(yi, yj);
    68         bfs2(mi, mj);
    69         int ans = inf;
    70         for(int i = 0; i < n; i++){
    71             for(int j = 0; j < m; j++){
    72                 if(maze[i][j] == '@'){
    73                     if(disy[i][j] != inf && dism[i][j] != inf)
    74                         ans = min(ans, disy[i][j] + dism[i][j]);
    75                 }
    76             }
    77         }
    78         printf("%d
    ", ans * 11);
    79     }
    80 }

    题目:

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 16695    Accepted Submission(s): 5358


    Problem Description
    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
    Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
    Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
     
    Input
    The input contains multiple test cases.
    Each test case include, first two integers n, m. (2<=n,m<=200). 
    Next n lines, each line included m character.
    ‘Y’ express yifenfei initial position.
    ‘M’    express Merceki initial position.
    ‘#’ forbid road;
    ‘.’ Road.
    ‘@’ KCF
     
    Output
    For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
     
    Sample Input
    4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
     
    Sample Output
    66 88 66
     
    Author
    yifenfei
     
    Source
  • 相关阅读:
    python中网络编程之线程
    python并发编程基础之守护进程、队列、锁
    python中并发编程基础1
    python中TCP粘包问题解决方案
    python中的异常处理常用方法
    python中面向对象元类的自定义用法
    python中类与对象之继承
    python中的re模块——正则表达式
    【2020090401】排名 rank over的用法
    【2020090301】mysql中 having 的用法
  • 原文地址:https://www.cnblogs.com/bolderic/p/7342511.html
Copyright © 2011-2022 走看看