POJ 2049 Finding Nemo

这个题可以用BFS解决。

有以下几个注意事项:

1、使用优先队列可以缩短程序运行时间。

2、要注意墙和门的相对位置(我就在这卡了一天。。sad)。

3、注意只有门和墙的位置范围是[ 1 , 199 ],Nemo的位置不一定。

4、注意没有墙和门的情况。

5、注意数组越界。

下面贴代码:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

struct node
{
    int x,y,cut;
    bool operator < (const node &b) const
    {
        if(cut==b.cut)
        {
            if(x==b.x)
            {
                return b.y<y;
            }
            else
            {
                return b.x<x;
            }
        }
        else
        {
            return b.cut<cut;
        }
    }
};
const int M=405;
int main()
{
    int map1[M][M],map2[M][M],x,y,d,t,n,m;
    while(scanf("%d%d",&m,&n))
    {
        int i,j,flat,minx=1000,miny=1000,maxx=0,maxy=0;
        float f1,f2;
        priority_queue <struct node ,vector <struct node>,less<struct node> >qu;
        if(m==-1&&n==-1)
        {
            break;
        }
        memset(map1,0,sizeof(map1));
        for(j=0; j<m; j++)
        {
            scanf("%d%d%d%d",&x,&y,&d,&t);
            x=x*2-1;
            y=y*2-1;
            if(d)
            {
                for(i=0; i<t*2; i+=2)
                {
                    map1[y+i][x]=1;
                    map1[y+i+1][x]=1;
                }
                map1[y+2*t][x]=1;
                if(x<minx)
                    minx=x;
                if(y<miny)
                    miny=y;
                if(x>maxx)
                    maxx=x;
                if(y+2*t>maxy)
                    maxy=y+2*t;
            }
            else
            {
                for(i=0; i<t*2; i+=2)
                {
                    map1[y][x+i]=1;
                    map1[y][x+i+1]=1;
                }
                map1[y][x+2*t]=1;
                if(x<minx)
                    minx=x;
                if(y<miny)
                    miny=y;
                if(x+2*t>maxx)
                    maxx=x+2*t;
                if(y>maxy)
                    maxy=y;
            }

        }
        for(i=0; i<n; i++)
        {
            scanf("%d%d%d",&x,&y,&d);
            x=x*2-1;
            y=y*2-1;
            if(d)
            {
                map1[y+1][x]=2;
            }
            else
            {
                map1[y][x+1]=2;
            }
        }
        cin >>f1>>f2;
         if (n==0 && m==0)
         {
             printf ("0\n");
			continue;
         }
		else if (f1<1||f2<1||f1>199||f2>199)
        {
            printf ("0\n");
            continue;
        }
        x=(int )f1*2;
        y=(int)f2*2;
        struct node du,dc;
        du.x=x;
        du.y=y;
        du.cut=0;
        qu.push(du);
        map1[y][x]=3;
        flat=0;
        while(!qu.empty())
        {
            du=qu.top();
            qu.pop();
            if(du.x<minx||du.y<miny||du.x>=maxx||du.y>=maxy)
            {
                flat=1;
                break;
            }
            if(du.x-1>=0)
            {
                if(map1[du.y][du.x-1]==2||map1[du.y][du.x-1]==0)
                {
                    if(map1[du.y][du.x-1]==2)
                    {
                        dc.cut=du.cut+1;
                    }
                    else
                    {
                        dc.cut=du.cut;
                    }
                    map1[du.y][du.x-1]=3;
                    dc.x=du.x-1;
                    dc.y=du.y;
                    qu.push(dc);
                }
            }
            if(du.x+1<=404)
            {
                if(map1[du.y][du.x+1]==2||map1[du.y][du.x+1]==0)
                {
                    if(map1[du.y][du.x+1]==2)
                    {
                        dc.cut=du.cut+1;
                    }
                    else
                    {
                        dc.cut=du.cut;
                    }
                    map1[du.y][du.x+1]=3;
                    dc.x=du.x+1;
                    dc.y=du.y;
                    qu.push(dc);
                }
            }
            if(du.y-1>=0)
            {
                if(map1[du.y-1][du.x]==2||map1[du.y-1][du.x]==0)
                {
                    if(map1[du.y-1][du.x]==2)
                    {
                        dc.cut=du.cut+1;
                    }
                    else
                    {
                        dc.cut=du.cut;
                    }
                    map1[du.y-1][du.x]=3;
                    dc.x=du.x;
                    dc.y=du.y-1;
                    qu.push(dc);
                }
            }
            if(du.y+1<=404)
            {
                if(map1[du.y+1][du.x]==2||map1[du.y+1][du.x]==0)
                {
                    if(map1[du.y+1][du.x]==2)
                    {
                        dc.cut=du.cut+1;
                    }
                    else
                    {
                        dc.cut=du.cut;
                    }
                    map1[du.y+1][du.x]=3;
                    dc.x=du.x;
                    dc.y=du.y+1;
                    qu.push(dc);
                }
            }
        }
        if(flat)
        {
            printf("%d\n",du.cut);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


转载于:https://www.cnblogs.com/lin375691011/p/3996828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值