Topic Links:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 2620
Test instructions: Given a sea, the number represents the current direction, along the current does not charge energy, the inverse of the current to charge 1 points of energy, each inquiry to a starting point of an end, ask the beginning to the end of the cost of the minimum energy
Idea: Wide search, queue with priority queue, each time to take the lowest energy point.
Code:
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#include <math.h>#include <map>#include <string>using namespace STD;intdir[Ten][2] = { { -1,0},{ -1,1},{0,1},{1,1},{1,0},{1, -1},{0, -1},{ -1, -1} };intR, C;Charp[1010][1010];intvis[1010][1010];intx1, Y11, x2, y2;BOOLIS_OK (intXintY) {if(x >=1&& x <= r && y >=1&& y <= C)return true;return false;}structnode{intx, y;intT Node () {}friend BOOL operator< (node A,node b) {returnA.T > b.t; }};intMain () { while(scanf("%d%d", &r,&c)! = EOF) { for(inti =1; I <= R; i++)scanf('%s ', P[i] +1);intNCin>> N; while(n--) {intAns =0;Cin>> x1 >> y11 >> x2 >> y2; Priority_queue<node> que; while(!que.empty ()) Que.pop ();memset(Vis,-1,sizeof(VIS)); VIS[X1][Y11] =0; Node A; a.x = x1, a.y = y11, A.T =0; Que.push (a); while(!que.empty ()) {Node TMP = Que.top (); Que.pop ();if(tmp.x = = x2 && tmp.y = = y2) {ans = tmp.t; Break; } node res; for(inti =0; I <8; i++) {intx = tmp.x + dir[i][0];inty = tmp.y + dir[i][1];if(!is_ok (x, y))Continue;//if (!IS_OK (x, y) && vis[x][y]! =-1) continue;res.x = x; Res.y = y;if(p[tmp.x][tmp.y]-' 0 '= = i) res.t = tmp.t;ElseRES.T = tmp.t +1;if(Vis[x][y] = =-1|| RES.T < Vis[x][y]) {vis[x][y] = RES.T; Que.push (RES); } } }printf("%d\n", ans); } }return 0;}
Copyright NOTICE: Reprint please indicate the source.
UVA 11573-ocean Currents "bfs+ priority queue"