poj 2049

来源:互联网 发布:淘宝摄影师 编辑:程序博客网 时间:2024/06/02 20:52
Finding Nemo
Time Limit: 2000MS Memory Limit: 30000KTotal 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. 

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 91 1 1 32 1 1 33 1 1 34 1 1 31 1 0 31 2 0 31 3 0 31 4 0 32 1 12 2 12 3 13 1 13 2 13 3 11 2 03 3 04 3 11.5 1.54 01 1 0 11 1 1 12 1 1 11 2 0 11.5 1.7
1 61 2 1 50 2 00 3 00 4 00 5 00 6 00 7 00.5 5.5
4 13 2 1 11 3 0 21 1 0 21 1 1 22 1 11.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 9999999struct 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;}


0 0
原创粉丝点击