Simple BFS.
1 /*2757*/2#include <iostream>3#include <queue>4#include <cstdio>5#include <cstring>6#include <cstdlib>7 using namespacestd;8 9 #defineMAXN 1005Ten Onetypedefstructnode_t { A intK, T; - node_t () {} -node_t (intKkintTT) { theK = KK; t =tt; - } -FriendBOOL operator< (Constnode_t &a,ConstNode_t &b) { - returnA.T >b.t; + } - } node_t; + A intN, M; at intBX, by, ex, EY; - CharMAP[MAXN][MAXN]; - intVISIT[MAXN][MAXN]; - intdir[8][2] = { --1,0, -1,1,0,1,1,1, - 1,0,1,-1,0,-1, -1,-1 in }; - toInlineBOOLCheckintXinty) { + returnx<0|| X>=n | | y<=0|| y>=m; - } the * intBFs () { $ intxx, yy;Panax Notoginseng intx, y, T; - intI, J, K; the node_t nd; +Priority_queue<node_t>Q; A thememset (Visit,0x3f,sizeof(visit)); +Visit[bx][by] =0; -ND.T =0; $ND.K = bx* ++by ; $ Q.push (ND); - - while(!Q.empty ()) { thend =q.top (); -xx = nd.k/ +;Wuyiyy = nd.k% +; the if(Xx==ex && yy==ey) - returnnd.t; Wu Q.pop (); - for(i=0; i<8; ++i) { Aboutx = xx + dir[i][0]; $y = yy + dir[i][1]; - if(check (x, y)) - Continue; - if(Map[xx][yy] = =i) At =nd.t; + Else thet = nd.t +1; - if(Visit[x][y] >t) { $Visit[x][y] =T; theK = +*x+y; the Q.push (node_t (k, t)); the } the } - } in the return 0; the } About the intMain () { the intI, J, K; the intT; + - #ifndef Online_judge theFreopen ("data.in","R", stdin);BayiFreopen ("Data.out","W", stdout); the #endif the - while(SCANF ("%d%d", &n,&m)! =EOF) { - for(i=0; i<n; ++i) { thescanf"%s", Map[i]); the for(j=0; j<m; ++j) theMAP[I][J]-='0'; the } -scanf"%d", &t); the while(t--) { thescanf"%d%d%d%d",&bx,&by,&ex,&ey); the--BX; --by; --ex; --ey;94 if(Bx==ex && by==ey) theK =0; the Else theK =BFS ();98printf"%d\n", k); About } - }101 102 return 0;103}
"Hdoj" 2757 Ocean Currents