HDU1026 bfs+优先队列

来源:互联网 发布:举报windows盗版软件 编辑:程序博客网 时间:2024/06/10 08:55

点击打开题目链接

题目的意思就是说要从左上角走到右下角  总过花了多少时间  如果当前点是数字n 则需要在在这个店停留n秒

这题并不难  难在后面的处理  细节问题  容易出错

特别注意

下面是我AC的代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int go[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
char fangxiang[4] = {'N','W','S','E'};
int n, m, flag = 0;
char map[100][100], vis[100][100];
struct point
{
    int x,y;
    int step;
    string f;
    friend bool operator < (point a,point b)
    {
        return a.step > b.step;
    }
}p;
void fac(string a)
{
    int x, x1,y, y1;
    int sum = 0;
    if(vis[0][0]>='0'&&vis[0][0]<='9')
        {
            for(int k = 1; k <= vis[0][0]-'0'; k++)
            {
                printf("%ds:FIGHT AT (0,0)\n",++sum);
            }
        }
    if(a[0]=='S')
        {
            printf("%ds:(0,0)->(1,0)\n",++sum);
            x = 1,y = 0;
            x1 = 1, y1 = 0;
        }
    else if(a[0]=='E')
        {
            printf("%ds:(0,0)->(0,1)\n",++sum);
            x = 0, y = 1;
            x1 = 0, y1 = 1;
        }
    for(int i = 1; a[i] != '\0'; i++)
    {
        if(a[i]=='N')
        {
            x = x - 1,y = y + 0;
        }
        else if(a[i]=='E')
        {
            x = x + 0,y = y + 1;
        }
        else if(a[i]=='S')
        {
            x = x + 1,y = y + 0;
        }
        else if(a[i]=='W')
        {
            x = x + 0,y = y - 1;
        }
        if(vis[x1][y1]>='1'&&vis[x1][y1]<='9')
        {
           for(int j = 1; j <= vis[x1][y1]-'0'; j++)
           {
               printf("%ds:FIGHT AT (%d,%d)\n",++sum,x1,y1);
           }
        }
        printf("%ds:(%d,%d)->(%d,%d)\n",++sum,x1,y1,x,y);
        x1 = x;
        y1 = y;
    }
    if(vis[n-1][m-1]>='1'&&vis[n-1][m-1]<='9')
    {
        for(int k = 1; k <= vis[n-1][m-1]-'0'; k++)
        {
            printf("%ds:FIGHT AT (%d,%d)\n",++sum,n-1,m-1);
        }
    }
    cout << "FINISH" << endl;
}
void bfs()
{
    flag = 0;
    p.x = 0,p.y = 0,p.step = 0,p.f = "";
    priority_queue<point>q;
    q.push(p);
    vis[p.x][p.y] = 1;
    point now, next;
    while(!q.empty())
    {
        now = q.top();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + go[i][0];
            next.y = now.y + go[i][1];
            next.step = now.step + 1;
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='X')
            {

                if(next.x==n-1&&next.y==m-1)
                {
                    if(map[next.x][next.y]>='1'&&map[next.x][next.y]<='9')
                        next.step+=map[next.x][next.y] - '0';
                    printf("It takes %d seconds to reach the target position, let me show you the way.\n",next.step);
                    flag = 1;
                    fac(now.f + fangxiang[i]);
                }
                if(map[next.x][next.y]>='1'&&map[next.x][next.y]<='9')
                {
                    next.step+=map[next.x][next.y] - '0';
                    next.f = now.f + fangxiang[i];
                    q.push(next);
                }
                next.f = now.f + fangxiang[i];
                map[next.x][next.y] = 'X';
                q.push(next);
            }
        }
    }

}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,'X',sizeof(map));
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
             {
                 cin >> map[i][j];
                 vis[i][j] = map[i][j];
             }
        }

        bfs();
        if(flag==0)
        {
            printf("God please help our poor hero.\n");
            printf("FINISH\n");
        }
    }
    return 0;
}


0 0
原创粉丝点击