Find a way HDU 2612 基础BFS

N – Find a wayTime Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 2612DescriptionPass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.InputThe input contains multiple test cases.Each test case include, first two integers n, m. (2<=n,m<=200).Next n lines, each line included m character.‘Y’ express yifenfei initial position.‘M’ express Merceki initial position.‘#’ forbid road;‘.’ Road.‘@’ KCFOutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input4 4 Y.#@ …. .#.. @..M 4 4 Y.#@ …. .#.. @#.M 5 5 Y..@. .#… .#… @..M. #…#Sample Output

66 88 66

//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<vector>#include<stack>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=205;const int inf=200000;#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define For(i,n) for(int i=0;i<(n);i++)template<class T>inline T read(T&x){char c;while((c=getchar())<=32);bool ok=false;if(c=='-')ok=true,c=getchar();for(x=0; c>32; c=getchar())x=x*10+c-'0';if(ok)x=-x;return x;}template<class T> inline void read_(T&x,T&y){read(x);read(y);}template<class T> inline void write(T x){if(x<0)putchar('-'),x=-x;if(x<10)putchar(x+'0');else write(x/10),putchar(x%10+'0');}template<class T>inline void writeln(T x){write(x);putchar('\n');}// ——-IO template——int n,m;struct node{int x,y;node(int a,int b){x=a;y=b;}};char a[maxn][maxn];int vis[maxn][maxn];int dx[]={-1,1,0,0};int dy[]={0,0,1,-1};void bfs(int x,int y){queue<node> q;q.push(node(x,y));memset(vis,0,sizeof(vis));vis[x][y]=0;while(!q.empty()){node s=q.front();q.pop();For(i,4){int xx=dx[i]+s.x;int yy=dy[i]+s.y;if(xx<0||xx>=n||yy<0||yy>=m||vis[xx][yy]||a[xx][yy]=='#'||a[xx][yy]=='Y'||a[xx][yy]=='M')continue;vis[xx][yy]=vis[s.x][s.y]+1;q.push(node(xx,yy));}}}int d[maxn][maxn];void bfs2(int x,int y){queue<node> q;q.push(node(x,y));memset(d,0,sizeof(d));d[x][y]=0;while(!q.empty()){node s=q.front();q.pop();For(i,4){int xx=dx[i]+s.x;int yy=dy[i]+s.y;if(xx<0||xx>=n||yy<0||yy>=m||d[xx][yy]||a[xx][yy]=='#'||a[xx][yy]=='Y'||a[xx][yy]=='M')continue;d[xx][yy]=d[s.x][s.y]+1;q.push(node(xx,yy));}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endif // ONLINE_JUDGEint i,j,k,t;while(~scanf("%d%d",&n,&m)){int yi,yj,mi,mj;For(i,n){scanf("%s",a[i]);For(j,m){if(a[i][j]=='Y'){yi=i;yj=j;}else if(a[i][j]=='M'){mi=i;mj=j;}}}bfs(yi,yj);bfs2(mi,mj);int ans=inf;For(i,n)For(j,m)if(a[i][j]=='@'){int rec=inf;if(vis[i][j]&&d[i][j])rec=vis[i][j]+d[i][j];ans=min(rec,ans);}writeln(ans*11);}return 0;}

,一个人负心,或许是因为他的记忆力不好。

Find a way HDU 2612 基础BFS

相关文章:

你感兴趣的文章:

标签云: