BFS (Shortest path) HDU 2612 Find a way

Source: Internet
Author: User

Topic Portal

1 /*2 BFS: And uva_11624 almost, the subject is to find two points to KFC's shortest, and then add to find the minimum value3 */4 /************************************************5 Author:running_time6 Created time:2015-8-4 19:36:367 File Name:HDOJ_2612.cpp8 ************************************************/9 Ten#include <cstdio> One#include <algorithm> A#include <iostream> -#include <sstream> -#include <cstring> the#include <cmath> -#include <string> -#include <vector> -#include <queue> +#include <deque> -#include <stack> +#include <list> A#include <map> at#include <Set> -#include <bitset> -#include <cstdlib> -#include <ctime> - using namespacestd; -  in #defineLson L, Mid, RT << 1 - #defineRson mid + 1, R, RT << 1 | 1 totypedefLong Longll; + Const intMAXN = 2e2 +Ten; - Const intINF =0x3f3f3f3f; the Const intMOD = 1e9 +7; * CharMAZE[MAXN][MAXN]; $ BOOLVIS[MAXN][MAXN];Panax Notoginseng intSTEP[MAXN][MAXN]; - intSTEP2[MAXN][MAXN]; the intdx[4] = {-1,1,0,0}; + intdy[4] = {0,0, -1,1}; A intN, M; the intSX, SY, ex, EY; +  - BOOLJudge_y (intXinty) { $     if(X <1|| X > N | | Y <1|| Y > M | | Maze[x][y] = ='#'|| Vis[x][y])return false; $     return true; - } -  the BOOLJudge_m (intXinty) { -     if(X <1|| X > N | | Y <1|| Y > M | | Maze[x][y] = ='#'|| Vis[x][y])return false;Wuyi     return true; the } -  Wu voidBfs_y (void)    { -memset (step,0,sizeof(step)); Aboutmemset (Vis,false,sizeof(Vis)); $queue<pair<int,int> >Q; Q.push (Make_pair (SX, SY)); -Vis[sx][sy] =true; Step[sx][sy] =0; -      while(!Q.empty ()) { -         intx = Q.front (). First, y =Q.front (). Second; Q.pop (); A          for(intI=0; i<4; ++i) { +             inttx = x + dx[i], Ty = y +Dy[i]; the             if(!judge_y (TX, Ty))Continue; -Q.push (Make_pair (TX, Ty)); Vis[tx][ty] =true; $Step[tx][ty] = Step[x][y] +1; the         } the     } the } the  - voidBfs_m (void)    { inmemset (Vis,false,sizeof(Vis)); thememset (Step2,0,sizeof(STEP2)); thequeue<pair<int,int> >Q; Q.push (Make_pair (ex, EY)); AboutVis[ex][ey] =true; Step2[ex][ey] =0; the      while(!Q.empty ()) { the         intx = Q.front (). First, y =Q.front (). Second; Q.pop (); the          for(intI=0; i<4; ++i) { +             inttx = x + dx[i], Ty = y +Dy[i]; -             if(!judge_m (TX, Ty))Continue; theStep2[tx][ty] = Step2[x][y] +1;BayiQ.push (Make_pair (TX, Ty)); Vis[tx][ty] =true; the         } the     } - } -  the intMainvoid)    {//HDU 2612 Find a the      while(SCANF ("%d%d", &n, &m) = =2) { the          for(intI=1; i<=n; ++i) { thescanf ("%s", Maze[i] +1); -              for(intj=1; j<=m; ++j) { the                 if(Maze[i][j] = ='Y') SX = i, sy =J; the                 if(Maze[i][j] = ='M') ex = i, EY =J; the             }94         } the bfs_y (); Bfs_m (); the         intAns =INF; the          for(intI=1; i<=n; ++i) {98              for(intj=1; j<=m; ++j) { About                 if(Maze[i][j] = ='@'&&Vis[i][j]) { -ans = min (ans, step[i][j] +step2[i][j]);101                 }102             }103         }104         if(ans = = INF) puts ("-1"); the         Elseprintf ("%d\n", ans * One);106     }107 108     return 0;109}

BFS (Shortest path) HDU 2612 Find a way

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.