poj 2049 Finding Nemo(bfs+dij 建图难)

来源:互联网 发布:阿里云空间 编辑:程序博客网 时间:2024/05/25 08:12

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 -1
Sample Output
5-1


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <queue>#include <vector>using namespace std;typedef long long LL;const int N = 1010;int  dis[N][N], xa[N][N], ya[N][N];const LL mod = 1e9+7;const int inf = 0x3f3f3f3f;int xn, ym;struct node{    int x, y, step;;};int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};int bfs(int sx,int sy){    queue<node>q;    node e;    e.x=1, e.y=1;    q.push(e);    for(int i=1; i<=ym; i++)    {        for(int j=1; j<=xn; j++)            dis[i][j]=inf;    }    dis[1][1]=0;    while(!q.empty())    {        node u=q.front();q.pop();        for(int i=0; i<4; i++)        {            int a1=u.x+dir[i][0], b1=u.y+dir[i][1];            int h;            if(i==0) h=ya[u.x-1][u.y];            else if(i==1) h=ya[u.x][u.y];            else if(i==2) h=xa[u.x][u.y-1];            else h=xa[u.x][u.y];            if(a1<0||a1>xn+1||b1<0||b1>ym+1) continue;            if(dis[a1][b1]>dis[u.x][u.y]+h)            {                dis[a1][b1]=dis[u.x][u.y]+h;                node tmp=u;                tmp.x=a1, tmp.y=b1;                q.push(tmp);            }        }    }    return dis[sx][sy]>=inf?-1:dis[sx][sy];}int main(){    int n, m;    while(scanf("%d %d", &n, &m),m!=1&&n!=-1)    {        memset(xa,0,sizeof(xa));        memset(ya,0,sizeof(ya));        xn=ym=0;        for(int i=0; i<n; i++)        {            int x, y, d, t;            scanf("%d %d %d %d", &x, &y, &d, &t);            if(d)            {                for(int j=y+1; j<=y+t; j++) ya[x][j]=inf;                xn=max(xn,x+1);                ym=max(ym,y+t+1);            }            else            {                for(int j=x+1; j<=x+t; j++) xa[j][y]=inf;                xn=max(xn,x+t+1);                ym=max(ym,y+1);            }        }        for(int i=0; i<m; i++)        {            int x, y, d;            scanf("%d %d %d", &x, &y, &d);            if(d) ya[x][y+1]=1;            else xa[x+1][y]=1;        }        double sx, sy;        scanf("%lf %lf", &sx, &sy);        if(!(sx>=1&&sx<=199&&sy>=1&&sy<=199)) printf("0\n");        else printf("%d\n",bfs((int)sx+1, (int)sy+1));    }    return 0;}





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚怀孕就有霉菌怎么办 车牌选错了想换怎么办 足癣传染到身上怎么办 金鱼生病了立鳞怎么办 脸上有闭口痘痘怎么办 泰迪得了皮肤癣怎么办 qq截图发不出去怎么办 半夜2点肚子饿怎么办 半夜两三点饿了怎么办 科目三预约不上怎么办 心脏支架又堵了怎么办 车辆验车过期了怎么办 车辆年审过期2天怎么办 违停单子丢了怎么办 沙发弹簧包坏了怎么办 bt种子被和谐了怎么办 苹果手机id密码忘了怎么办 墙漆颜色太深了怎么办 油漆颜色太深了怎么办 墙面漆颜色太深怎么办 古代打仗牙掉了怎么办 乐视电视没声音怎么办 老公疑心病很重怎么办啊 被安装了尿道锁怎么办 狼青小狗腿罗圈怎么办 备孕期间有霉菌怎么办 虫子进皮肤里了怎么办 生完孩子肚子越来越大怎么办 怀孕8个月肚子小怎么办 孕晚期胎儿不长怎么办 肚子上肉特别多怎么办 奶堵了有硬块怎么办 便秘5天肚子胀怎么办 上火大便拉不出来怎么办 大便拉不出来肚子痛怎么办 戒奶奶涨有硬块怎么办 忌奶的时候涨奶怎么办 娃儿隔奶,,奶涨怎么办 狗肚子很大很鼓怎么办 注册不了的二建怎么办 专升本没考过怎么办