1026:Ignatius and the Princess I

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

方法:bfs+记忆路径

思路:这是我刷题以来见过的最难的一道题,不但要搜索出最短时间,而且还要记录最短时间所走过的路径,而且还要按照那种诡异的方式进行输出。总之,这道题没有一个方面是好处理的。首先,由于是找最短路径,因此用bfs比较合适,另外,由于要输出路径,所以还要开辟一个数组来储存路径。还有一点就是本题需要用到优先队列,队列元素的权重就是步数,步数小的要占据高优先级,保证步数小的可以率先出队。

难点:全都是难点。

参考链接:http://www.tuicool.com/articles/uI7RBb

个人水平有限,只能按照以上范本照猫画虎。

#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
char maze[101][101];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
bool mark[101][101] = {0};
int n,m;
int ans = -1;
struct node
{
    int x,y;
    int step;
    friend bool operator < (node n1,node n2)
    {
        return n2.step < n1.step;
    }
};

struct Path
{
  int x,y,step;
}path[101][101];

bool check(int x,int y)
{
    if(x >= n||x < 0||y >= m||y < 0) return 1;
    if(mark[x][y] == 1||maze[x][y] == 'X') return 1;
    return 0;
}
void bfs()
{
    node p,t,next;
    memset(mark,0,sizeof(mark));
    p.x = 0;
    p.y = 0;
    p.step = 0;
    mark[0][0] = 1;
    priority_queue<node> Q;
    Q.push(p);
    while(!Q.empty())
    {
        t = Q.top();
        Q.pop();
        if(t.x == n-1&&t.y == m-1)
        {
            ans = t.step;
            return;
        }
        for(int i = 0;i < 4;i++)
        {
            next.x = t.x+dir[i][0];
            next.y = t.y+dir[i][1];
            if(check(next.x,next.y)) continue;
            if(maze[next.x][next.y] == '.')
            {
                path[next.x][next.y].x=t.x;
                path[next.x][next.y].y=t.y;
                path[next.x][next.y].step = 1;
                //v.push_back(t);
                next.step = t.step+1;
                mark[next.x][next.y] = 1;
                Q.push(next);
            }
            else
            {
                path[next.x][next.y].x=t.x;
                path[next.x][next.y].y=t.y;
                path[next.x][next.y].step=maze[next.x][next.y]-'0'+1;
                //v.push_back(t);
                next.step = t.step+maze[next.x][next.y]-'0'+1;
                mark[next.x][next.y] = 1;
                Q.push(next);
            }
        }
    }
}

void show(int a,int b,int t)
{
  if(t==1)	printf("%ds:(%d,%d)->(%d,%d)\n",t,path[a][b].x,path[a][b].y,a,b);
  else if(path[a][b].step==1)
  {
    show(path[a][b].x,path[a][b].y,t-1);
    printf("%ds:(%d,%d)->(%d,%d)\n",t,path[a][b].x,path[a][b].y,a,b);
  }
  else
  {
    --path[a][b].step;
    show(a,b,t-1);
    printf("%ds:FIGHT AT (%d,%d)\n",t,a,b);
  }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ans = -1;
        for(int i = 0;i < n;i++)
        {
            scanf("%s",&maze[i]);
        }
        bfs();
        if(ans == -1)
            printf("God please help our poor hero.\nFINISH\n");
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
            show(n-1,m-1,ans);
            printf("FINISH\n");
        }
    }
}


你可能感兴趣的:(1026:Ignatius and the Princess I)