Title Link: http://poj.org/problem?id=3170
Description
Bessie are in Camelot and have encountered a sticky situation:she needs to pass through the forest that's guarded by the K Nights of Ni. In order to pass through safely, the Knights has demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.
Bessie have a map of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel To the X and Y axes. The map is a W x H units in size (1 <= W <=; 1 <= H <= 1000).
The map shows where Bessie starts her quest, the single square where the Knights of Ni is, and the locations of all the S Hrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks is impassable because of swamps, cliffs, and Kille R rabbits). Bessie can not pass through the Knights of Ni Square without a shrubbery.
In order to make sure this she follows the map correctly, Bessie can only move in four Directions:north, East, south, or West (i.e., not diagonally). She requires a traversal from one grid block to a neighboring grid block.
It's guaranteed that Bessie'll be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest-a-to-do.
Input
Line 1:two space-separated integers:w and H.
Lines 2..: These Lines describe the map, row by row. The first line describes the very northwest part of the map; The last line describes the very southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and all input line contains no more than Space-separat Ed integers. If W <=, then each input line describes a complete row of the map. If W > B, then more than one line are used to describe a single row, all integers on each line except potentially the LA St One. No input line ever describes elements of more than one row.
The integers that describe the map come from this set:
0:square through which Bessie can travel
1:impassable Square that Bessie cannot traverse
2:bessie ' s starting location
3:location of the Knights of Ni
4:location of a shrubbery
Output
Line 1: D, the minimum number of days it would take Bessie to reach a shrubbery and bring it to the Knights of Ni.
Sample Input
8 44 1 0 0 0 0 1 00 0 0 1 0 1 0 00 2 1 1 3 0 4 00 0 0 4 1 1 1 0
Sample Output
11
Hint
Explanation of the sample:
Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.
Bessie can move in this pattern to get a shrubbery for the knights:n, W, N, S, E, E, N, E, E, S, S. She Gets the Shrubber Y in the northwest corner and then makes she away around the barriers to the east and then south to the Knights.
Source
Usaco 2005 December Silver
Ps:
Two times BFS,
The first time is to find the shortest distance from 2 to 4.
The second time is to find the shortest distance from 3 to 4.
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #define INF 0x3f3f3f3f #define MAXN 1000+47using namespace Std;int W, H;int S, E;int g[maxn][maxn];int DIS1[MAXN][MAXN], Dis2[maxn][maxn];int xx[ 4] = {0,0,-1,1};int Yy[4] = {1,-1,0,0};bool vis[maxn][maxn];int MIN (int a, int b) {if (a < b) return A; return b;} void bfs1 (int x,int y) {queue<int>q; int u = x*w+y; Vis[x][y]=true; Q.push (U); while (!q.empty ()) {U=q.front (); Q.pop (); x = u/w; y = u%w; for (int i = 0; i < 4; i++) {int dx = x + xx[i]; int dy = y + yy[i]; if (Dx>=0&&dx
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3170 Knights of Ni (two times BFS)