# | Problem | Pass Rate (passed user / total user) |
---|---|---|
11945 | Treasure Hunting |
|
12819 | 15 Puzzle |
|
Description
Niflheimr is playing Minecraft recently. He occupies a lot of towns, and there are many diamonds inside some of those towns.
Whenever he runs out of diamond, he has to figure out the nearest town with diamonds from his current location.
Doing this task every time waste a lot of time. He asks you to help him for the calculation. For every town, find the shortest distance to get diamonds from a nearby town which has diamonds. If the town itself has diamonds, the distance would be 0.
Hint: Use BFS (Breadth First Search) and std::queue.
If you are not familiar with BFS, you should take a look at the following article:
http://alrightchiu.github.io/SecondRound/graph-breadth-first-searchbfsguang-du-you-xian-sou-xun.html
[IMPORTANT] For those almost run out of time, maybe this note can help you figure out BFS and solve this problem faster!
You are provided with the following code:
#include <queue> #include <iostream> #include <vector> #include <algorithm> using namespace std; // G[i] is the neighbor towns of town i vector<int> diamondTowns; vector<int> G[100005]; int Dist[100005]; struct node { int id; int dist; node(int id, int l) { this->id = id; this->dist = l; } }; int main() { int N, M, K; cin >> N >> M >> K; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < K; i++) { int x; cin >> x; diamondTowns.push_back(x); } fill(Dist, Dist+100005, -1); queue<node> Q; // [TODO] complete the task! // Output for (int i = 1; i <= N; i++) { cout << Dist[i] << '\n'; } return 0; }
You may take it as reference. You are not required to use this code, though.
Input
First line of input contains three integer N, M, K, denoting the number of towns, the amount of roads between towns and the number of towns which has diamond.
All the towns are numbered from 1 to N.
For the next M lines, each of them contains two integer a, b, meaning there is a road between a and b. All the roads are bidirectional, and the length of these roads are all 1.
The last line of input contains K distinct integers, denoting the towns which has diamonds.
It is guaranteed that:
- 1 <= K <= N <= 105
- 1 <= M <= min(106, N*(N+1)/2)
- All the towns can reach each other, i.e. the graph is connected.
Output
Output consists of N lines.
For i-th line, output the shortest distance Niflheimr has to travel from town i to get a diamond.
Sample Input Download
Sample Output Download
Discuss
Description
The 15-puzzle is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing. The goal of this problem is to transform the arrangement of tiles from the initial arrangement to a goal arrangement.
The legal moves are the moves in which the tiles adjacent to the empty tile are moved to either left, right, up, or down.
Your goal is to find the minimum move to solve the problem.
ouo.
hint
Using BFS/DFS to find answer may exceed memory limit and time limit of some test cases, so we recommend using A* algorithm, which is more efficient. In A* algorithm, it determine the direction of searching by “steps until now + heuristic value”.
A* algorithm: https://en.wikipedia.org/wiki/A*_search_algorithm
To improve heuristic function, you can adopt manhattan distance with Linear Conflict.
Manhattan distance: https://en.wikipedia.org/wiki/Taxicab_geometry
Linear Conflict: The tile set(t1, t2) is linear conflict if they have same goal row(column), and they are now in that row(column), but they are blocked by each other. A set of lenear conflict will cause at least two extra steps.
Input
Input contains 4 lines, each line has 4 numbers.
The given puzzle is guaranteed to contain numbers from 0 ~ 15.
The zero denotes the empty tile.
It is guaranteed that the given puzzle is solvable.
Output
Output the minimum move to solve the puzzle.