On the H * w map, there are n cheese factories, each producing a 1-n-hardness cheese. There was a foodie mouse ready to go from the rat hole and eat the cheese from every factory. Rats have a physical value, initially 1, and each plant has a 1 increase in the strength of the cheese (only once per factory), and the mice can only eat cheese that is less than the current physical value.
The mouse from the current lattice to the adjacent barrier-free lattice (up and down) takes time 1 units, there are obstacles to the lattice can not walk. When you walk to the factory, you can eat the cheese of the factory and eat the cheese time. Ask for the least amount of cheese to eat all over.
Input: The first line is three integers h (1 <= H <= 1000), W (1 <= W <=1000), N (1 <= N <= 9), then H Row W is listed as the map, "." For the open space, "X" for the Obstacle, "S" for the mouse Hole, 1-n represents the hardness of 1-n cheese factory. (Chinese translation Reference http://bbs.byr.cn/#!article/ACM_ICPC/73337?au=Milrivel)
Train of thought: foodie must according to factory n value from small to large order to eat, otherwise physically incompetent. So this topic is actually seeking to traverse the map in sequence 12345 ... The shortest path to these few points. When it comes to the shortest path, of course it is BFS.
#include <iostream> #include <queue>using namespace Std;int W, H, N;char map[1024][1024];//the distance from each point to the current factory int d[ 1024][1024];const int direction[4][2] = {{-1, 0},{1, 0},{0, 1},{0, 1},}; int Factory[16][2];typedef pair<int, int> P; method:bfs//fullname:bfs//access:public//returns:int//Parameter : Const int & SX start x//parameter:const int & sy start y//parameter:const int & GX end x//Parameter:const int &A mp GY endpoint Y//************************************int BFS (const int& SX, const int& SY, const int& GX, const INT& ; GY) {//memset (d,-1, sizeof (d)), for (int i = 0; i < H; ++i) {for (int j = 0; J < W; ++j) {D[j][i] =-1;}} Queue<p> Que;que.push (P (SX, SY));d [Sx][sy] = 0;while (Que.size ()) {const P p = Que.front (); Que.pop ();//If it is the end, it ends if ( P.first = = GX && P.second = = gy) break; Four-direction roaming for (int i = 0; i < 4; ++i) {int NX = P.first + direction[i][0];int ny = P.second + DireWhether the ction[i][1];//can be moved and the point has not been accessed if (0 <= NX && NX < w && 0 <= NY && NY < h && m Ap[nx][ny]! = ' X ' && d[nx][ny] = =-1) {Que.push (P (NX, NY));d [Nx][ny] = D[p.first][p.second] + 1;}}} return d[gx][gy];} int main () {cin >> h >> w >> n;for (int i = 0; i < H; ++i) {for (int j = 0; J < W; ++j) {cin >> ; Map[j][i];}} for (int i = 0, i < H; ++i) {for (int j = 0; J < W; ++j) {if (map[j][i] = = ' S ') {factory[0][0] = j;factory[0][1] = I;ma P[j][i] = '. ';} else if (IsDigit (Map[j][i])) {int index = map[j][i]-' 0 '; factory[index][0] = j;factory[index][1] = i;}}} int step = 0;for (int i = 0; i < n; ++i) {//sequentially eat step + = BFS (Factory[i][0], factory[i][1], Factory[i + 1][0], factory[i + 1][1]);} cout << step << Endl; return 0;}
AOJ 0558 Cheese "BFS"