POJ 2049 Finding Nemo

Finding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 6463 Accepted: 1473

Description

Nemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help.
After checking the map, Marlin found that the sea is like a labyrinth with walls and doors. All the walls are parallel to the X-axis or to the Y-axis. The thickness of the walls are assumed to be zero.
All the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent medusas near the doors), Marlin wants to go through as few doors as he could to find Nemo.
Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.

We assume Marlin's initial position is at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum number of doors Marlin has to go through in order to reach Nemo.

Input

The input consists of several test cases. Each test case is started by two non-negative integers M and N. M represents the number of walls in the labyrinth and N represents the number of doors.
Then follow M lines, each containing four integers that describe a wall in the following format:
x y d t
(x, y) indicates the lower-left point of the wall, d is the direction of the wall -- 0 means it's parallel to the X-axis and 1 means that it's parallel to the Y-axis, and t gives the length of the wall.
The coordinates of two ends of any wall will be in the range of [1,199].
Then there are N lines that give the description of the doors:
x y d
x, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted.
The last line of each case contains two positive float numbers:
f1 f2
(f1, f2) gives the position of Nemo. And it will not lie within any wall or door.
A test case of M = -1 and N = -1 indicates the end of input, and should not be processed.

Output

For each test case, in a separate line, please output the minimum number of doors Marlin has to go through in order to rescue his son. If he can't reach Nemo, output -1.

Sample Input

8 9
1 1 1 3
2 1 1 3
3 1 1 3
4 1 1 3
1 1 0 3
1 2 0 3
1 3 0 3
1 4 0 3
2 1 1
2 2 1
2 3 1
3 1 1
3 2 1
3 3 1
1 2 0
3 3 0
4 3 1
1.5 1.5
4 0
1 1 0 1
1 1 1 1
2 1 1 1
1 2 0 1
1.5 1.7
-1 -1

Sample Output

5
-1

Source

     昨天晚上看了看这个题目,但是没有思路,以前做的题都是与点有关的,突然来了处理边的很不适应,想了想要用什么样的数据结构来存储呢?思路很是混乱,怎么确定一个点在一片空间中呢?太多的问题困扰着我。所以就搜了一下解题报告,看了看他们是用什么样的数据结构来存储图的,看了之后也不懂为什么那样存储图形,回宿舍的时候就想着这个问题,然后突然有一想法,可以把这个区域分成1 X 1的许多图形,他们用那样的数据结构来存储图我开始慢慢明白,同时我也有了一丝思路,想着一定能做出来,今天中午放学后就敲了敲,可是代码不知道怎么的越写越多,很多时候就写不下去了,到了最后的时候看着我的代码,我都变得慢慢的模糊了,自己都不知道这代码是怎么写出来的,刚开始写出来的时候,调试样例的时候怎么都不对,只能一步步的分析,到最后发现在存储的结构体数组给弄错了,改过来调试正确了,但是交上去确实WA,这下子不知道怎么办了,这种数据自己构造太麻烦了,然后就看了看discuss上的数据,发现有一组确实错了,但我却 不知道是为什么,然后在那看啊看啊,最后发现是建图的时候处理的不够精确,每次都多标记了一组,改过来之后终于A了。最后A的程序有330多行了,我都有些想笑了,其实里面有很多部分处理都是差不多的,写好一段其他的直接粘贴就好了,然后在改一改。我比较高兴的是虽然有很多小错误,但是函数的主体程序没有错,最重要的是思路没有错。
   其实我们在平时处理题目的时候,都是做与点有关的题目,遇到边的题目的时候,我们可以尝试着把他转换成点。或许你就会明白并且能够更好的处理。  
  最后把我300多行的代码给大家展示一下吧!伴随着这个题,训练1的计划终于完成了,明天就行训练2的计划.微笑
 
#include <iostream>
#include <string.h>
using namespace std;
class num
{
    public:
    int x[4],y[4];
    int pt[4];
}c[5];
class queue
{
    public:
    int x,y,status;
    int k;
}b[1000000];
int map[2][300][300];
int maxx,maxy,minx,miny;
int INF=0x7ffffff;
int main()
{
    int deal(int x,int y);
    int i,j,n,m,s,t,k;
    int x,y,d,xend,yend;
    double u,v;
    (c[1].x)[1]=0; (c[1].y)[1]=0; (c[1].x)[2]=1; (c[1].y)[2]=0; (c[1].x)[3]=0;(c[1].y)[3]=1;
    (c[1].pt)[1]=3; (c[1].pt)[2]=4; (c[1].pt)[3]=1;
    (c[2].x)[1]=0; (c[2].y)[1]=-1; (c[2].x)[2]=1; (c[2].y)[2]=-1; (c[2].x)[3]=0;(c[2].y)[3]=-1;
    (c[2].pt)[1]=3; (c[2].pt)[2]=4; (c[2].pt)[3]=2;
    (c[3].x)[1]=-1; (c[3].y)[1]=0; (c[3].x)[2]=-1; (c[3].y)[2]=0; (c[3].x)[3]=-1;(c[3].y)[3]=1;
    (c[3].pt)[1]=3; (c[3].pt)[2]=2; (c[3].pt)[3]=1;
    (c[4].x)[1]=1; (c[4].y)[1]=0; (c[4].x)[2]=0; (c[4].y)[2]=1; (c[4].x)[3]=0;(c[4].y)[3]=0;
    (c[4].pt)[1]=4; (c[4].pt)[2]=1; (c[4].pt)[3]=2;
    while(cin>>n>>m)
    {
        if(n==-1&&m==-1)
        {
            break;
        }
        for(i=1;i<=299;i++)
        {
            for(j=1;j<=299;j++)
            {
                map[0][i][j]=0;
                map[1][i][j]=0;
            }
        }
        maxx=maxy=0;minx=miny=INF;
        for(i=1;i<=n;i++)
        {
            cin>>x>>y>>d>>t;
            if(d==0)
            {
                if(y>maxy)
                {
                    maxy=y;
                }
                if(y<miny)
                {
                    miny=y;
                }
                if((x+t)>maxx)
                {
                    maxx=x+t;
                }
                if((x)<minx)
                {
                    minx=x;
                }
            }else
            {
                if((y+t)>maxy)
                {
                    maxy=y+t;
                }
                if(y<miny)
                {
                    miny=y;
                }
                if((x)>maxx)
                {
                    maxx=x;
                }
                if((x)<minx)
                {
                    minx=x;
                }

            }
            for(j=0;j<=t-1;j++)
            {
                if(d==0)
                {
                    map[d][x+j][y]=1;
                }else
                {
                    map[d][x][y+j]=1;
                }
            }
        }
        for(i=1;i<=m;i++)
        {
            cin>>x>>y>>d;
            if(d==0)
            {
                map[d][x][y]=2;
                if(x<minx)
                {
                    minx=x;
                }
                if((x+1)>maxx)
                {
                    maxx=x+1;
                }
                if(y>maxy)
                {
                    maxy=y;
                }
                if(y<miny)
                {
                    miny=y;
                }
            }else
            {
                map[d][x][y]=2;
                if(x<minx)
                {
                    minx=x;
                }
                if((x)>maxx)
                {
                    maxx=x;
                }
                if((y+1)>maxy)
                {
                    maxy=y+1;
                }
                if(y<miny)
                {
                    miny=y;
                }
            }
        }
        cin>>u>>v;
        xend=(int)u; yend=(int)v;
        if(yend<miny||yend>=maxy||xend<minx||xend>=maxx)
        {
            cout<<0<<endl;
            continue;
        }
        k=deal(xend,yend);
        if(k==INF)
        {
            cout<<-1<<endl;
        }else
        {
            cout<<k<<endl;
        }
    }
    return 0;
}
int deal(int x,int y)
{
    int i,j,base,top,res;
    int xend,yend,key,k2;
    int status[2][250][250];
    int sum[2][250][250];
    int vex[]={0,0,0,1};
    int vey[]={1,0,0,0};
    memset(status,0,sizeof(status));
    for(i=1;i<=249;i++)
    {
        for(j=1;j<=249;j++)
        {
            sum[0][i][j]=INF;
            sum[1][i][j]=INF;
        }
    }
    base=top=0;
    res=INF;
    for(i=0;i<=3;i++)
    {
        xend=x+vex[i]; yend=y+vey[i];
        if(xend>=minx&&xend<=maxx&¥d>=miny&¥d<=maxy)
        {
            if(i<=1)
            {
                if(map[0][xend][yend]==2&&!status[0][xend][yend])
                {
                    sum[0][xend][yend]=1;
                    status[0][xend][yend]=1;
                    (b[top].x)=xend; (b[top].y)=yend; b[top].k=0;(b[top++].status)=i+1;
                    if(yend==miny||yend==maxy)
                    {
                        if(sum[0][xend][yend]<res)
                        {
                            res=sum[0][xend][yend];
                        }
                    }
                }else if(map[0][xend][yend]==0&&!status[0][xend][yend])
                {
                    sum[0][xend][yend]=0;
                    status[0][xend][yend]=1;
                    (b[top].x)=xend; (b[top].y)=yend; b[top].k=0;(b[top++].status)=i+1;
                    if(yend==miny||yend==maxy)
                    {
                        if(sum[0][xend][yend]<res)
                        {
                            res=sum[0][xend][yend];
                        }
                    }
                }
            }else
            {
                if(map[1][xend][yend]==2&&!status[1][xend][yend])
                {
                    sum[1][xend][yend]=1;
                    status[1][xend][yend]=1;
                    (b[top].x)=xend; (b[top].y)=yend; b[top].k=1;(b[top++].status)=i+1;
                    if(xend==minx||xend==maxx)
                    {

                        if(sum[1][xend][yend]<res)
                        {
                            res=sum[1][xend][yend];
                        }
                    }
                }else if(map[1][xend][yend]==0&&!status[1][xend][yend])
                {
                    sum[1][xend][yend]=0;
                    status[1][xend][yend]=1;
                    (b[top].x)=xend; (b[top].y)=yend; b[top].k=1;(b[top++].status)=i+1;
                    if(xend==minx||xend==maxx)
                    {
                        if(sum[1][xend][yend]<res)
                        {
                            res=sum[1][xend][yend];
                        }
                    }
                }
            }
        }
    }
    while(base<top)
    {
        x=b[base].x; y=b[base].y; key=b[base].status; k2=b[base++].k;
        status[k2][x][y]=0;
        for(i=1;i<=3;i++)
        {
            xend=x+(c[key].x)[i]; yend=y+(c[key].y)[i];
            if(xend>=minx&&xend<=maxx&¥d>=miny&¥d<=maxy)
            {
                if((c[key].pt)[i]<=2)
                {
                    if(map[0][xend][yend]==2)
                    {
                        if(sum[0][xend][yend]>sum[k2][x][y]+1)
                        {
                            sum[0][xend][yend]=sum[k2][x][y]+1;
                            if(yend==miny||yend==maxy)
                            {
                                if(sum[0][xend][yend]<res)
                                {
                                    res=sum[0][xend][yend];
                                }
                            }
                            if(!status[0][xend][yend])
                            {
                                status[0][xend][yend]=1;
                                (b[top].x)=xend; (b[top].y)=yend; b[top].k=0;(b[top++].status)=(c[key].pt)[i];
                            }
                        }
                    }else if(map[0][xend][yend]==0)
                    {
                        if(sum[0][xend][yend]>sum[k2][x][y])
                        {
                            sum[0][xend][yend]=sum[k2][x][y];
                            if(yend==miny||yend==maxy)
                            {
                                if(sum[0][xend][yend]<res)
                                {
                                    res=sum[0][xend][yend];
                                }
                            }
                            if(!status[0][xend][yend])
                            {
                                status[0][xend][yend]=1;
                                (b[top].x)=xend; (b[top].y)=yend; b[top].k=0;(b[top++].status)=(c[key].pt)[i];
                            }
                        }
                    }
                }else
                {
                    if(map[1][xend][yend]==2)
                    {
                        if(sum[1][xend][yend]>sum[k2][x][y]+1)
                        {
                            sum[1][xend][yend]=sum[k2][x][y]+1;
                            if(xend==minx||xend==maxx)
                            {
                                if(sum[1][xend][yend]<res)
                                {
                                    res=sum[1][xend][yend];
                                }
                            }
                            if(!status[1][xend][yend])
                            {
                                status[1][xend][yend]=1;
                                (b[top].x)=xend; (b[top].y)=yend; b[top].k=1;(b[top++].status)=(c[key].pt)[i];
                            }
                        }
                    }else if(map[1][xend][yend]==0)
                    {
                        if(sum[1][xend][yend]>sum[k2][x][y])
                        {
                            sum[1][xend][yend]=sum[k2][x][y];
                            if(xend==minx||xend==maxx)
                            {
                                if(sum[1][xend][yend]<res)
                                {
                                    res=sum[1][xend][yend];
                                }
                            }
                            if(!status[1][xend][yend])
                            {
                                status[1][xend][yend]=1;
                                (b[top].x)=xend; (b[top].y)=yend; b[top].k=1;(b[top++].status)=(c[key].pt)[i];
                            }
                        }
                    }
                }
             }
        }
    }
    return res;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值