POJ2049——Finding Nemo

Finding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 9176 Accepted: 2156

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      //n和m  n个墙  m个门(门在墙上)
1 1 1 3     //x,y,d,t  墙的坐标x,y   d表示方向 1 竖着,0 横着,  t为墙的长度
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     //x,y,d   x,y为门的坐标, d表示方向 1 竖着,0 横着  长度题目规定为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       //若到达不了人的位置,输出-1

Source



#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int inf = 0x3f3f3f3f;     //一个很大的数(16进制)
const int N = 250;
int xa[N][N], ya[N][N];      //xa数组存放上墙,ya存放右墙
int xmx, ymx;
int dir[5][2] = {{-1, 0},{1, 0},{0, -1}, {0, 1}};   //方向数组

int out(int x, int y)     //判断出界
{
    if( x<=0||x>xmx||y<=0||y>ymx )
        return 0;
    return 1;
}

int getstep(int x, int y, int i)    //返回某个方向的数
{
    if( i==0 )
        return ya[x-1][y];
    else if( i==1 )
        return ya[x][y];
    else if( i==2 )
        return xa[x][y-1];
    else
        return xa[x][y];
}

int bfs(int xx, int yy)      //广搜
{
    int dis[N][N];
    for( int i = 0; i <= xmx; i++ )
        for( int j = 0; j <= ymx; j++ )
            dis[i][j] = inf;

    dis[1][1] = 0;

    queue<int> q;           //辅助队列
    while(!q.empty())
        q.pop();

    q.push(1);
    q.push(1);

    int dx, dy, x, y, step;
    while(!q.empty())
    {
        x = q.front();
        q.pop();
        y = q.front();
        q.pop();

        for( int i = 0; i < 4; i++ )
        {
            dx = x + dir[i][0];
            dy = y + dir[i][1];
            step = getstep( x, y, i );
            if( out(dx, dy)&&dis[dx][dy]>dis[x][y]+step )
            {
                dis[dx][dy] = dis[x][y]+step;
                q.push(dx);
                q.push(dy);
            }
        }
    }
    return (dis[xx][yy]==inf?-1:dis[xx][yy]);
}

int main()
{
    int n,m;
    int x, y, d, t;
    while(cin>>n>>m)
    {
        if( n==-1&&m==-1 ) break;

        memset(xa,0,sizeof(xa));     //墙的初始化,若为墙  则为inf  ,若为门  则为1,  否则  为初始化的0
        memset(ya,0,sizeof(ya));

        xmx = ymx = -1;
        for( int i = 0; i < n; i++ )
        {
            cin>>x>>y>>d>>t;
            if( d )
            {
                for( int j = 0; j < t; j++ )
                    ya[x][y+j+1] = inf;
                ymx = max(ymx,y+t+1 );
                xmx = max(xmx, x+1);
            }
            else
            {
                for( int j = 0; j < t; j++ )
                    xa[x+j+1][y] = inf;
                xmx = max(xmx, x+t+1);
                ymx = max( ymx, y+1 );
            }
        }

        for( int i = 0; i < m; i++ )
        {
            cin>>x>>y>>d;
            if( d )
                ya[x][y+1] = 1;
            else
                xa[x+1][y] = 1;
        }

        double f1, f2;
        cin>>f1;
        cin>>f2;
        if( !(f1>=1&&f1<= 199&&f2>=1&&f2<=199) )    //若在  外面  则不需要经过门
            cout<<0<<endl;
        else
            cout<<bfs( (int)f1+1,(int)f2+1 )<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值