• HDU 1026 Ignatius and the Princess I (bfs+存储路径)


     http://acm.hdu.edu.cn/showproblem.php?pid=1026

    Ignatius and the Princess I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 8381    Accepted Submission(s): 2490

    Special Judge

    Problem Description
    The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:
    1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1). 2.The array is marked with some characters and numbers. We define them like this: . : The place where Ignatius can walk on. X : The place is a trap, Ignatius should not walk on it. n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
    Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
     
    Input
    The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
     
    Output
    For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
     
    Sample Input
    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX.
    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX1
    5 6
    .XX...
    ..XX1.
    2...X.
    ...XX.
    XXXXX.
     
    Sample Output
    It takes 13 seconds to reach the target position, let me show you the way.
    1s:(0,0)->(1,0)
    2s:(1,0)->(1,1)
    3s:(1,1)->(2,1)
    4s:(2,1)->(2,2)
    5s:(2,2)->(2,3)
    6s:(2,3)->(1,3)
    7s:(1,3)->(1,4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1,4)->(1,5)
    11s:(1,5)->(2,5)
    12s:(2,5)->(3,5)
    13s:(3,5)->(4,5)
    FINISH
    It takes 14 seconds to reach the target position, let me show you the way.
    1s:(0,0)->(1,0)
    2s:(1,0)->(1,1)
    3s:(1,1)->(2,1)
    4s:(2,1)->(2,2)
    5s:(2,2)->(2,3)
    6s:(2,3)->(1,3)
    7s:(1,3)->(1,4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1,4)->(1,5)
    11s:(1,5)->(2,5)
    12s:(2,5)->(3,5)
    13s:(3,5)->(4,5)
    14s:FIGHT AT (4,5)
    FINISH
    God please help our poor hero.
    FINISH
     
    Author
    Ignatius.L
     

    思路:bfs搜索所有路径,并用一个二维数组存储经过对应的位置时的最小时间,如果搜索到某路径所用时间小于该位置的最小时间,则该路径进队列,否则跳过,当到达终点时,比较此时所耗的时间,选择最小值,最后输出路径。

    表示在指针上纠结了好久。。。之后又因为一个循环的问题,白送了几个WA。。。唉,水平还待提高啊

    125MS,17944K,我就不说啥了,还是贴下这龌龊的代码吧,以后有时间改进下......

    Code:

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<queue>
      5 #include<stack>
      6 using namespace std;
      7 #define MAX 999999
      8 char g[120][120];
      9 int dir[4][2] = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}};
     10 int step[120][120];
     11 int n, m, length, path;
     12 struct node
     13 {
     14     int x;
     15     int y;
     16     int flag;
     17     int step;
     18 };
     19 struct pri
     20 {
     21     node data;
     22     struct pri *pre;
     23 };
     24 void getd()
     25 {
     26     int i, j;
     27     char s[120];
     28     for(i = 0; i < n; i++)
     29     {
     30         scanf("%s", s);
     31         for(j = 0; j < strlen(s); j++)
     32             g[i][j] = s[j];
     33     }
     34     memset(step, MAX, sizeof(step));
     35     path = MAX;
     36 }
     37 int check(int x, int y)
     38 {
     39     if(x >= 0 && x < n && y >= 0 && y < m)
     40     {
     41         if(g[x][y] == '.')return 1;
     42         if(g[x][y] >= '0' && g[x][y] <= '9')return 2;
     43     }
     44     return 0;
     45 }
     46 void showpath(node p[], int n)
     47 {
     48     int i, j, k = 1;
     49     printf("It takes %d seconds to reach the target position, let me show you the way.\n", p[0].step);
     50     for(i = n; i > 0; i--)
     51     {
     52         if(p[i].flag == 0)printf("%ds:(%d,%d)->(%d,%d)\n", k++, p[i].x, p[i].y, p[i - 1].x, p[i - 1].y);
     53         else if(p[i].flag == 1)
     54         {
     55             for(j = 1; j <= g[p[i].x][p[i].y] - 48; j++)
     56                 printf("%ds:FIGHT AT (%d,%d)\n", k++, p[i].x, p[i].y);
     57             printf("%ds:(%d,%d)->(%d,%d)\n", k++, p[i].x, p[i].y, p[i - 1].x, p[i - 1].y);
     58         }
     59     }
     60     if(p[0].flag == 1)
     61     {
     62         for(j = 1; j <= g[p[0].x][p[0].y] - 48; j++)
     63             printf("%ds:FIGHT AT (%d,%d)\n", k++, p[0].x, p[0].y);
     64     }
     65 }
     66 void bfs()
     67 {
     68     int i, j, t;
     69     pri p, s, *temp;
     70     queue<pri>q;
     71     node st[20000];
     72     p.data.x = p.data.y = p.data.flag = p.data.step = 0;
     73     p.pre = NULL;
     74     step[0][0] = 0;
     75     temp = (pri *)malloc(sizeof(pri));
     76     q.push(p);
     77     while(!q.empty())
     78     {
     79         p = q.front();
     80         q.pop();
     81         s.pre = (pri *)malloc(sizeof(pri));
     82         if(p.data.x == n - 1 && p.data.y == m - 1 && path > p.data.step)
     83         {
     84             path = p.data.step;
     85             j = 0;
     86             temp = (pri *)malloc(sizeof(pri));
     87             *temp = p;
     88             while(temp != NULL)
     89             {
     90                 st[j++] = temp->data;
     91                 temp = temp->pre;
     92             }
     93             length = j - 1;
     94         }
     95         for(i = 0; i < 4; i++)
     96         {
     97             s.data.x = p.data.x + dir[i][0];
     98             s.data.y = p.data.y + dir[i][1];
     99             t = check(s.data.x, s.data.y);
    100             if(t == 0)continue;
    101             else
    102             {
    103                 if(t == 1)
    104                 {
    105                     s.data.step = p.data.step + 1;
    106                     s.data.flag = 0;
    107                 }
    108                 else if(t == 2)
    109                 {
    110                     s.data.step = p.data.step + g[s.data.x][s.data.y] - 48 + 1;
    111                     s.data.flag = 1;
    112                 }
    113                 if(step[s.data.x][s.data.y] <= s.data.step)continue;
    114                 step[s.data.x][s.data.y] = s.data.step;
    115                 *s.pre = p;
    116                 q.push(s);
    117             }
    118         }
    119     }
    120     if(path == MAX)printf("God please help our poor hero.\n");
    121     else showpath(st, length);
    122     printf("FINISH\n");
    123 }
    124 int main()
    125 {
    126     while(scanf("%d %d", &n, &m) != EOF)
    127     {
    128         getchar();
    129         getd();
    130         bfs();
    131     }
    132     return 0;
    133 }
  • 相关阅读:
    傻逼Eclipse笔记
    Less笔记
    [转]解决WebClient或HttpWebRequest首次连接缓慢问题
    Css3图标库
    Json.Net4.5 序列化问题
    async和await
    CLR、内存分配和垃圾回收
    C#7.0新语法
    C#6.0新语法
    C#泛型详解
  • 原文地址:https://www.cnblogs.com/lfeng/p/2979681.html
Copyright © 2020-2023  润新知