码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1026 Ignatius and the Princess I

时间:2014-09-19 10:13:15      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:bfs

简单bfs问题。

迷宫左上角是入口,右下角是出口,迷宫中有战斗力为1~9的守卫,打败他们需要1~9的时间。


遇到怪物判一下就可以了。步骤倒序输出即可。

由于需要记录步骤,不适用STL的queue。


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define debug puts("==fuck==")
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 1000+10
using namespace std;

int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};
int n,m;
char g[101][101];
struct lx
{
    int x,y,lv,path;
    void init(int xx,int yy,int llv,int p)
    {
        x=xx,y=yy,lv=llv,path=p;
    }
};
lx q[10000001];
void bfs()
{
    bool vis[101][101];
    CLR(vis,0);
    vis[0][0]=1;
    lx tmp;
    tmp.init(0,0,0,-1);
    int h=0,e=0;
    q[h++]=tmp;
    bool flag=0;
    while(e<h)
    {
        tmp=q[e++];
        lx now;
//        printf("%d %d ==%d\n",tmp.x,tmp.y,tmp.lv);
//        system("pause");
        if(tmp.x==n-1&&tmp.y==m-1&&(g[tmp.x][tmp.y]<'1'||g[tmp.x][tmp.y]>'9'))
        {
            flag=1;
            break;
        }
        if(g[tmp.x][tmp.y]>='1'&&g[tmp.x][tmp.y]<='9')
        {
            now.init(tmp.x,tmp.y,tmp.lv+1,e-1);
            g[tmp.x][tmp.y]--;
            vis[tmp.x][tmp.y]=1;
            q[h++]=now;
        }
        else
        FOR(k,0,4)
        {
            int x=tmp.x+xx[k];
            int y=tmp.y+yy[k];
            if(x<0||y<0||x>=n||y>=m||vis[x][y]||g[x][y]=='X')
                continue;
            now.init(x,y,tmp.lv+1,e-1);
            vis[x][y]=1;
            q[h++]=now;
        }
    }
    if(flag)
    {
        stack<lx>out;
        out.push(tmp);
        int path=tmp.path;
        int t=tmp.lv;
        while(path!=-1)
        {
            out.push(q[path]);
            path=q[path].path;
        }
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",t);
        while(!out.empty())
        {
            lx now=out.top();out.pop();
            if(now.lv==0)
            {
                tmp=now;
                continue;
            }
            if(now.x==tmp.x&&now.y==tmp.y)
                printf("%ds:FIGHT AT (%d,%d)\n",now.lv,now.x,now.y);
            else
                printf("%ds:(%d,%d)->(%d,%d)\n",now.lv,tmp.x,tmp.y,now.x,now.y);
            tmp=now;

        }
    }
    else
        puts("God please help our poor hero.");
    puts("FINISH");
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        char str[101];
        FOR(i,0,n)
        {
            scanf("%s",str);
            FOR(j,0,m)
            g[i][j]=str[j];
        }
        bfs();
    }
}


HDU 1026 Ignatius and the Princess I

标签:bfs

原文地址:http://blog.csdn.net/dongshimou/article/details/39393673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!