"Hdoj" 2757 Ocean Currents

Source: Internet
Author: User

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

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.