zoukankan      html  css  js  c++  java
  • HDU-1026 Ignatius and the Princess I (BFS)

    这道题还解决出,问题是数组装的路径是所有的路径,而不是那个最短路径,但是最短时间算对了,怎么样把数组装成那个最短的路径,是我一直要解决的问题,求大神帮我,or等我再有水平了,重新解决这个问题。。。。。

                                    Ignatius and the Princess I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9911    Accepted Submission(s): 2960 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
      1 #include<stdio.h>
      2 #include<queue>
      3 using namespace std;
      4 typedef struct
      5 {
      6   int x,y,time;
      7 }point;
      8 point start;
      9 point arr[1000000];
     10 int n,m,g;
     11 int dir[4][2]={1,0,-1,0,0,1,0,-1},mintime[100][100];
     12 char map[101][101];
     13 void bfs(point start)
     14 {
     15     int i;
     16     queue<point>q;
     17     point cur,next;
     18     start.time=0;
     19     arr[g]=start;
     20     if(start.x==n-1&&start.y==m-1)
     21     {
     22         return ;
     23     }
     24     map[start.x][start.y]='X';
     25     q.push(start);
     26     while(!q.empty())
     27     {
     28         cur=q.front();
     29         q.pop();
     30         for(i=0;i<4;i++)
     31         {
     32             next.x=cur.x+dir[i][0];
     33             next.y=cur.y+dir[i][1];
     34             if(next.x==n-1&&next.y==m-1)
     35             { 
     36                 if(map[next.x][next.y]=='.')
     37                 {
     38                     next.time=cur.time+1;
     39                     if(next.time<mintime[next.x][next.y])
     40                     {
     41                     mintime[next.x][next.y]=next.time;
     42                      arr[g].x=next.x;
     43                     arr[g].y=next.y;
     44                     arr[g].time=next.time;
     45                 //    printf("arr=%d%d%d
    ",arr[g].x,arr[g].y,arr[g].time);
     46                     return ;
     47                     }
     48                 }
     49                 if(map[next.x][next.y]>=49&&map[next.x][next.y]<=57)
     50                 {
     51                     next.time=cur.time+map[next.x][next.y]-'0'+1;
     52                 if(next.time<mintime[next.x][next.y])
     53                     {
     54                     mintime[next.x][next.y]=next.time;
     55                     arr[g].x=next.x;
     56                     arr[g].y=next.y;
     57                     arr[g].time=next.time;
     58                 //    printf("arr=%d%d%d
    ",arr[g].x,arr[g].y,arr[g].time);
     59                     return ;
     60                     }
     61                 }
     62             }
     63             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='X')
     64             {
     65                 if(map[next.x][next.y]>=49&&map[next.x][next.y]<=57)
     66                     {
     67                         map[next.x][next.y]=='X';
     68                         next.time=cur.time+map[next.x][next.y]-'0';
     69                     }
     70                     if(map[next.x][next.y]=='.')
     71                     {
     72                         map[next.x][next.y]=='X';
     73                         next.time=cur.time+1;
     74                     }
     75                    if(next.time<mintime[next.x][next.y])
     76                     {
     77                         mintime[next.x][next.y]=next.time;
     78                         q.push(next);
     79                       //  printf("next=%d%d%d
    ",next.x,next.y,next.time);
     80                         arr[g].x=next.x;
     81                         arr[g].y=next.y;
     82                         arr[g].time=next.time;
     83                     //    printf("arr=%d%d%d
    ",arr[g].x,arr[g].y,arr[g].time);
     84                         g++;
     85                     }
     86             }
     87         }
     88     }
     89     return ;
     90 }
     91     int main()
     92     {
     93         int i,j,x,y,p,time;
     94         while(~scanf("%d%d",&n,&m))
     95         {
     96             time=1;
     97             g=p=0;
     98         for ( i = 0 ;i < n ;i++)
     99           for ( j = 0 ;j < m ;j++)
    100           {
    101                scanf(" %c",&map[i][j]);
    102                mintime[i][j] = 100000;
    103           }
    104           
    105             if(map[n-1][m-1]=='X') 
    106             {
    107                 printf("God please help our poor hero.
    ");
    108                 continue;
    109             }
    110             start.x=0;start.y=0;
    111             bfs(start);
    112         //    printf("%d",mintime[n-1][m-1] );
    113             if (mintime[n-1][m-1] == 100000)
    114                 printf ("God please help our poor hero.
    ");
    115             else
    116             {
    117                 printf ("It takes %d seconds to reach the target position, let me show you the way.
    ",mintime[n-1][m-1]);
    118                 while (p<=g)
    119                 {
    120                     x = arr[p].x ,y = arr[p].y;
    121                     if (map[x][y] != 'X' && map[x][y] != '.')
    122                       for(int i = 1 ;i <= map[x][y] - '0' ;i++)
    123                             printf("%ds:FIGHT AT (%d,%d)
    ",arr[p].time,x,y);
    124                     else
    125                         printf("%ds:(%d,%d)->(%d,%d)
    ",arr[p].time,x,y,arr[p+1].x,arr[p+1].y);
    126 
    127                     p++;
    128                 }
    129             }
    130             printf("FINISH
    ");
    131 
    132         }
    133         
    134     return 0;
    135 }
    136         
     
  • 相关阅读:
    monit安装配置
    php新加扩展模块
    centos6.5(64bit),python2.6.6安装MySQLdb模块
    esxi导出ovf模板注意事项
    gateone安装使用
    centos7上安装nagios及增加监控服务器
    zabbix自动发现主机并加入组绑定模板
    zabbix监控Windows-server
    zabbix设置中文并解决乱码问题
    centos7安装zabbix客户端并监控
  • 原文地址:https://www.cnblogs.com/cancangood/p/3278870.html
Copyright © 2011-2022 走看看