POJ2049Finding Nemo(bfs + 构图)

Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 8456   Accepted: 1975

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

题意:先给出n个墙,以x,y为左下角的点d == 1,与
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <queue>
  4 #include <cstring>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 const int MAX = 250;
  9 const int INF = 0x3f3f3f3f;
 10 int maxx,maxy,sx,sy;
 11 int h[MAX][MAX],l[MAX][MAX],dis[MAX][MAX];
 12 double f1,f2;
 13 struct node
 14 {
 15     int x,y;
 16     int door;
 17     bool operator<(const node &a) const
 18     {
 19         return a.door < door;
 20     }
 21 };
 22 
 23 void bfs()
 24 {
 25     priority_queue<node> q;
 26     while(q.empty() == 0)
 27         q.pop();
 28     node cur;
 29     cur.x = 0;
 30     cur.y = 0;
 31     cur.door = 0;
 32     for(int i = 0; i <= maxx; i++)
 33     {
 34         for(int j = 0; j <= maxy; j++)
 35         {
 36             dis[i][j] = INF;
 37         }
 38     }
 39     dis[0][0] = 0;
 40     q.push(cur);
 41     while(q.empty() == 0)
 42     {
 43         cur = q.top();
 44         q.pop();
 45         int x = cur.x;
 46         int y = cur.y;
 47         if(x == sx && y == sy)
 48             return;
 49         if(y + 1 <= maxy && dis[x][y + 1] > dis[x][y] + h[x][y + 1])
 50         {
 51             dis[x][y + 1] = dis[x][y] + h[x][y + 1];
 52             cur.x = x;
 53             cur.y = y + 1;
 54             cur.door = dis[x][y + 1];
 55             q.push(cur);
 56         }
 57         if(y -1 >= 0 && dis[x][y - 1] > dis[x][y] + h[x][y])
 58         {
 59             dis[x][y - 1] = dis[x][y] + h[x][y];
 60             cur.x = x;
 61             cur.y = y - 1;
 62             cur.door = dis[x][y - 1];
 63             q.push(cur);
 64         }
 65         if(x + 1 <= maxx && dis[x + 1][y] > dis[x][y] + l[x + 1][y])
 66         {
 67             dis[x + 1][y] = dis[x][y] + l[x + 1][y];
 68             cur.x = x + 1;
 69             cur.y = y;
 70             cur.door = dis[x + 1][y];
 71             q.push(cur);
 72         }
 73         if(x - 1 >= 0 && dis[x - 1][y] > dis[x][y] + l[x][y])
 74         {
 75             dis[x - 1][y] = dis[x][y] + l[x][y];
 76             cur.x = x  - 1;
 77             cur.y = y;
 78             cur.door = dis[x - 1][y];
 79             q.push(cur);
 80         }
 81     }
 82     dis[sx][sy] = -1;
 83 }
 84 int main()
 85 {
 86     int n,m,x,y,d,t;
 87     while(scanf("%d%d",&n,&m) != EOF)
 88     {
 89         if(m == -1 && n == -1)
 90             break;
 91         maxx = maxy = -1;
 92         memset(h,0,sizeof(h));
 93         memset(l,0,sizeof(l));
 94         for(int i = 0; i < n; i++)
 95         {
 96             scanf("%d%d%d%d",&x,&y,&d,&t);
 97             if(d == 0)
 98             {
 99                 for(int j = 0; j < t; j++)
100                 {
101                     h[x + j][y] = INF;
102                 }
103                 maxx = max(maxx,x + t);
104                 maxy = max(maxy,y);
105             }
106             else
107             {
108                 for(int j = 0; j < t; j++)
109                     l[x][y + j] = INF;
110                 maxx = max(maxx,x);
111                 maxy = max(maxy,y + t);
112             }
113         }
114         for(int i = 0; i < m; i++)
115         {
116             scanf("%d%d%d", &x, &y,&d);
117             if(d == 0)
118             {
119                 h[x][y] = 1;
120             }
121             else
122                 l[x][y] = 1;
123         }
124         scanf("%lf%lf",&f1,&f2);
125         if(f1 > maxx || f2 > maxy)
126         {
127             printf("0
");
128             continue;
129         }
130         sx = (int) f1;
131         sy = (int) f2;
132         bfs();
133         printf("%d
",dis[sx][sy]);
134     }
135     return 0;
136 }
View Code
原文地址:https://www.cnblogs.com/zhaopAC/p/5008835.html