Simple HDU 2612 Find a way,bfs.

Source: Internet
Author: User

Description

Pass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S.

is to ask two people to the minimum value of a KFC, this question remember to do when the pit was miserable, to pay attention to the initialization of the INF, and said that the starting position of people to be expressed as a pass.

The code is as follows:

#include <iostream>#include<cstring>using namespacestd;Const intinf=1e7; Shortmap1[205][205];intque[40005],las,fir;intans[205][205];intans1[205][205];intn,m;intCoukfc,si,sj,ei,ej;BOOLJudgeintXintYint(*rem) [205]){    if(x<=0|| y<=0|| x>n| | Y>M)return 0; if(rem[x][y]!=INF)return 0; if(map1[x][y]==0)        return 0; return 1;}voidBFsintXintYint(*rem) [205]) {las=fir=0; intcou=0; inttemp,t1,t2; Que[las++]=x* ++y; Rem[x][y]=0;  while(las-fir) {Temp=que[fir++]; T1=temp/ +; T2=temp% +; Temp=REM[T1][T2]; if(map1[t1][t2]==2)            ++cou; if(cou>=COUKFC)return; --T1; if(judge (T1,t2,rem)) {Rem[t1][t2]=temp+1; Que[las++]=t1* ++T2; } T1+=2; if(judge (T1,t2,rem)) {Rem[t1][t2]=temp+1; Que[las++]=t1* ++T2; }            --T1; --T2; if(judge (T1,t2,rem)) {Rem[t1][t2]=temp+1; Que[las++]=t1* ++T2; } T2+=2; if(judge (T1,t2,rem)) {Rem[t1][t2]=temp+1; Que[las++]=t1* ++T2; }    }}intSlove () {BFS (Si,sj,ans);    BFS (EI,EJ,ANS1); intminn=INF;  for(intI=1; i<=n;++i) for(intj=1; j<=m;++j)if(map1[i][j]==2)                if(minn>ans[i][j]+ANS1[I][J]) Minn=ans[i][j]+Ans1[i][j]; returnminn* One;}intMain () {Ios::sync_with_stdio (false); CharC;  while(cin>>n>>M) {COUKFC=0;  for(intI=1; i<=n;++i) for(intj=1; j<=m;++j) {cin>>C; ANS[I][J]=ans1[i][j]=INF; Switch(c) { Case 'Y': Map1[i][j]=1; Si=i; Sj=J;  Break;  Case 'M': Map1[i][j]=1; Ei=i; Ej=K;  Break;  Case '.': Map1[i][j]=1;  Break;  Case '#': Map1[i][j]=0;  Break;  Case '@': Map1[i][j]=2; ++COUKFC;  Break; }} cout<<slove () <<Endl; }    return 0;}
View Code

Simple HDU 2612 Find a way,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.