POJ 2049 Finding Nemo priority queue STL
The plot of the story "bottom story" is used, and the little ugly fish, Tom, is lost. His father is trying to help him.
The question shows a map like this, which assumes that the map is composed of walls and doors, ignoring the thickness of the wall. There is a door on the map, where there is no wall, you can move freely to ask how many doorways you can pass through to help you to reach nimo.
The data given by this question is an integer point at the point of intersection of the wall, but the fish father can move freely beyond the Wall.
Therefore, this question has two difficulties:
1. Save the map
2. How to traverse a map
Because the question is to give a point (x, y) to represent a wall
I use a pair of X and Y to represent the block space of the map.
A two-dimensional struct array is used for storage, and each struct is such a unit.
How to save it is solved.
As for traversal, BFS is decisive, but to ensure that the minimum number of doors is reached, there is a problem of which path to choose when traversing the next square.
In order to ensure that the number of doors passes is the least, priority queue is selected, instead of first-in-first-out queues, but the least-out queues.
Use the priority queue. Try STL for the first time.
priority_queue
,less
> que;
The third parameter has a problem and I don't know how to write it.
Baidu has said that it needs to re-write the overload, but does not understand how to write it. It seems that the overload inside and outside the function is hard to solve, STL should not let me do this (because it is understood that the error is to overload the priority_queue write operator)
After reading their code, I found that I was wrong. I should think of the struct defined by myself as a class and rewrite the operators for this class.
Then write it to the struct.
struct Tmp{ int x,y; int t; bool operator<(const Tmp & b) const { return t > b.t; }};
In the later debugging process, problems always occur. The biggest problem is that I ignore X, and Y should interchange positions. After all, the coordinates given by the question are different from those represented by two-dimensional arrays.
Last set of test data:
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
The answer is 2.
Instead of 4
The data we saw in the discussion showed me the big vulnerabilities that X and Y needed to swap, and then I got.
Code:
#include
#include
#include
#include
#include
#include
#define MAX 1000000using namespace std;struct node{ int eg[4];} MAP[300][300];bool vis[300][300];struct Tmp{ int x,y; int t; bool operator<(const Tmp & b) const { return t > b.t; }};int bfs (int x,int y,int fx,int fy){ Tmp tmp,now; priority_queue
,less
> que; memset(vis,0,sizeof(vis)); now.x = x; now.y = y; now.t = 0; que.push (now); while (!que.empty()) { now = que.top(); que.pop(); //cout << now.t << endl; if (now.x == fx && now.y == fy) return now.t; if (!vis[now.x][now.y]) { //cout << now.x << ' ' << now.y << '-' << now.t<< endl; vis[now.x][now.y] = 1; for (int i = 0;i < 4;i++) { if (MAP[now.x][now.y].eg[i] != 1) { tmp = now; if (i == 0) { tmp.x--; if (MAP[now.x][now.y].eg[i] == 2) tmp.t++; }else if (i == 1) { tmp.y++; if (MAP[now.x][now.y].eg[i] == 2) tmp.t++; }else if (i == 2) { tmp.x++; if (MAP[now.x][now.y].eg[i] == 2) tmp.t++; }else if (i == 3) { tmp.y--; if (MAP[now.x][now.y].eg[i] == 2) tmp.t++; } if (tmp.x>=0 && tmp.y >= 0 && tmp.x < 300 && tmp.y < 300 && !vis[tmp.x][tmp.y]) que.push(tmp); } } } } return -1;}int main (void){ int m,k; while (scanf (%d%d,&m,&k),m != -1 || k != -1) { memset(MAP,0,sizeof (MAP)); int x,y,d,t,i; for (i = 0;i < m;i++) { scanf (%d%d%d%d,&x,&y,&d,&t); int j; if (d) //y { for (j = 0;j < t;j++) { MAP[299 - y - j][x].eg[3] = 1; MAP[299 - y - j][x - 1].eg[1] = 1; } }else //x { for (j = 0;j < t;j++) { MAP[299 - y][x + j].eg[2] = 1; MAP[299 - y + 1][x + j].eg[0] = 1; } } } for (i = 0;i < k;i++) { scanf (%d%d%d,&x,&y,&d); int j; if (d) //y { MAP[299 - y][x].eg[3] = 2; MAP[299 - y][x - 1].eg[1] = 2; }else //x { MAP[299 - y][x].eg[2] = 2; MAP[299 - y + 1][x].eg[0] = 2; } } double fx,fy; scanf (%lf%lf,&fx,&fy); int ifx = int (fx); int ify = int (fy); if (k == 0 && m == 0) printf (0); else if (fx < 0 || fx > 199 || fy < 0 || fy > 199) printf(0); else { int ans = bfs (299,0,299 - ify,ifx); printf (%d,ans); } } return 0;}
??