HDU 2612-find A-on-the-it (detail BFS)

Source: Internet
Author: User

Topic Link: Find A-to

The topic is not difficult, a few days ago, at that time prepared to write two-way BFS, and then deal with the details of some problems, to catch up with something on hold. Rewrite this evening, no two-way, with two times BFS search, and two-way BFS almost the same principle. It's just a little hole in the problem, you need to be careful.

1.Y cannot go through M. M cannot go through Y. In other words, Y and M are squares that can be thought of as walls.

2. KFC must be both Y and M to reach, only one of them arrives.


The ratio is the following column data; The answer is neither 22 nor 88 but 110, the KFC full condition in the lower left corner

5 5Y.. #@... m.....#.....@ ....
A little bit of a hole for me.

。。

Thank you nickname: Zstu_jayye Jay did not read his message in the discuss board. I don't expect it .

31MS 852K

Code is very slag, rushing.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue >const int N = 1e6;const int M = 220;using namespace Std;char mapp[m][m];bool vis1[m][m],vis2[m][m];int Dis1[M][M],dis2 M  [M],n,m,l;struct node{int X,y,a;node () {x = 0; y = 0;a = 0; }};struct node{int x, y;}    Kfc[40010];int mv[4][2]={{-1,0},{1,0},{0,-1},{0,1}};void BFS (int x,int y,bool visit[m][m],int Ans[M][M]) {node f,t;    queue<node>q;    Visit[x][y]=true; F.A = 0;f.x=x;f.y=y;q.push (f), while (!q.empty ()) {t = Q.front (); Q.pop (); if (mapp[t.x][t.y]== ' @ ') ans[t. X][T.Y] = T.A; for (int i=0;i<4;i++) {f.x=t.x+mv[i][0]; f.y=t.y+mv[i][1]; if (!visit[f.x][f.y]&&0<=f.x&&f.x <n&&0<=f.y&&f.y<m&&mapp[f.x][f.y]!= ' # ') {F.A = t.a + 1; visit[f.x][f.y]=true; q.push (f);           }}}}int Main () {int Sx,sy,ex,ey;int minn;while (~scanf ("%d%d%*c", &n,&m)) {L = 0;for (int i=0;i<n;i++) {  scanf ("%s", Mapp[i]);  for (int j=0;j<m;j++) {if (mapp[i][j]== ' Y ') {sx=i; sy=j;mapp[i][j]= ' # ';}  else if (mapp[i][j]== ' M ') {mapp[i][j]= ' # '; ex=i; Ey=j;}            else if (mapp[i][j]== ' @ ') {kfc[l].x = i; kfc[l++].y = j;     }}}memset (Vis1,0,sizeof (VIS1));        memset (dis1,0,sizeof (DIS1));     BFS (SX,SY,VIS1,DIS1); memset (Vis2,0,sizeof (VIS2)); memset (dis2,0,sizeof (DIS2)); BFS (EX,EY,VIS2,DIS2); minn=n;for (int i = 0;i<l;i++) {if (Minn > DIS1[KFC[I].X][KFC[I].Y] + DIS2[KFC [I].X][KFC[I].Y]) if (DIS1[KFC[I].X][KFC[I].Y]!=0&AMP;&AMP;DIS2[KFC[I].X][KFC[I].Y]!=0) Minn        = Dis1[kfc[i].x][kfc[i].y] + dis2[kfc[i].x][kfc[i].y]; }printf ("%d\n", minn*11);} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HDU 2612-find A-on-the-it (detail BFS)

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.