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

poj 2049

时间:2014-06-03 03:04:50      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 7373   Accepted: 1715

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. 
bubuko.com,布布扣

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 6
1 2 1 5
0 2 0
0 3 0
0 4 0
0 5 0
0 6 0
0 7 0
0.5 5.5
4 1
3 2 1 1
1 3 0 2
1 1 0 2
1 1 1 2
2 1 1
1.5 1.5
-1 -1

Sample Output

5
-1
2
0
题目意思很清晰了,直接上AC代码了,里面解释很清楚
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define MM 210
#define MX 9999999
struct Node{
    int x,y,dis;
    friend bool operator <(const Node &a,const Node &b){
        return a.dis>b.dis;                                 //注意这里是>,因为优先队列是大顶堆结构 
    }
    Node(int X,int Y,int DIS){
        x=X; y=Y; dis=DIS;
    }
};
int xe[MM][MM],ye[MM][MM];             //xe表示横向边  ye表示纵向边 
int dis[MM][MM];                       //表示到 0 0的距离 
int f_x,f_y;
priority_queue <Node> q;               //优先队列 
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int M,N;
    while(scanf("%d%d",&M,&N)!=EOF&&(M!=-1 || N!=-1)){
        memset(xe,0,sizeof(xe));
        memset(ye,0,sizeof(ye));
        int x,y,d,t;
        int mx,my;
        mx=my=0;
        for(int i=0;i<M;i++){                    //初始化墙 
            scanf("%d%d%d%d",&x,&y,&d,&t);
            if(d==1){
                for(int j=0;j<t;j++)
                    ye[x][y+j]=MX;               //ye[x][y]表示 x y+1这条边 
                mx=max(mx,x);
                my=max(my,y+t);
            }
            else{
                for(int j=0;j<t;j++)
                    xe[x+j][y]=MX;
                mx=max(mx,x);
                my=max(my,y+t);
            }
        }
        
        for(int i=0;i<N;i++){                     //初始化门 
            scanf("%d%d%d",&x,&y,&d);
            if(d==1){
                ye[x][y]=1;
                mx=max(mx,x);
                my=max(my,y+1);
            }
            else{
                xe[x][y]=1;
                mx=max(mx,x+1);
                my=max(my,y);
            }
        }
        float xd,yd; scanf("%f%f",&xd,&yd);
        if(xd>mx || yd>my){
            printf("0\n");
            continue;
        }
        
        f_x=(int)xd; f_y=(int)yd;               
        for(int i=0;i<=mx;i++)
            for(int j=0;j<=my;j++)
                dis[i][j]=MX;
        dis[0][0]=0;
        int tmp_x,tmp_y;
        int flag=0;
        while(!q.empty())
            q.pop();
        q.push(Node(0,0,0));
        while(!q.empty()){                         //从0 0出发bfs直到到达目的地 
            tmp_x=q.top().x;  tmp_y=q.top().y;
            q.pop();
            if(tmp_x==f_x && tmp_y==f_y){          //到达目的地,退出 
                flag=1;
                break;
            }
            if(tmp_y+1<=my && dis[tmp_x][tmp_y+1]>dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y+1]){   //向上走 
                dis[tmp_x][tmp_y+1]=dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y+1];
                q.push(Node(tmp_x,tmp_y+1,dis[tmp_x][tmp_y+1]));
            }
            if(tmp_x+1<=mx && dis[tmp_x+1][tmp_y]>dis[tmp_x][tmp_y]+ye[tmp_x+1][tmp_y]){    //向右走 
                dis[tmp_x+1][tmp_y]=dis[tmp_x][tmp_y]+ye[tmp_x+1][tmp_y];
                q.push(Node(tmp_x+1,tmp_y,dis[tmp_x+1][tmp_y]));
            }
            if(tmp_y-1>=0 && dis[tmp_x][tmp_y-1]>dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y]){        //向下走  注意:好好体会这里的 +xe[tmp_x][tmp_y] 
                dis[tmp_x][tmp_y-1]=dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y];                                      //而不是 +xe[tmp_x][tmp_y-1]
                q.push(Node(tmp_x,tmp_y-1,dis[tmp_x][tmp_y-1]));
            }
            if(tmp_x-1>=0 && dis[tmp_x-1][tmp_y]>dis[tmp_x][tmp_y]+ye[tmp_x][tmp_y]){        //向左走  注意:好好体会这里的 +ye[tmp_x][tmp_y] 
                dis[tmp_x-1][tmp_y]=dis[tmp_x][tmp_y]+ye[tmp_x][tmp_y];                                      //而不是 +xe[tmp_x][tmp_y-1]
                q.push(Node(tmp_x-1,tmp_y,dis[tmp_x-1][tmp_y]));
            }
        }
        if(flag)
            printf("%d\n",dis[f_x][f_y]);
        else
            printf("-1\n");
    }
    return 0;
}


poj 2049,布布扣,bubuko.com

poj 2049

标签:des   c   style   class   blog   code   

原文地址:http://blog.csdn.net/my_acm/article/details/27565081

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