UVA 11573-ocean Currents "bfs+ priority queue"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.