[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612
Posted 九月旧约
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612相关的知识,希望对你有一定的参考价值。
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. ‘@’ KCF OutputFor 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 Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 [email protected] .#... .#... @..M. #...#
Sample Output
66 88 66
开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。
果然超时
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<cmath> using namespace std; #define maxn 40005 int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[maxn],vis[205][205]; char mapn[205][205]; struct point{ int x,y; }; point p[maxn]; struct node{ int x,y,step; }; void bfs(int a,int b,int c,int d,int cnt){ node p; p.x = a,p.y = b,p.step = 0; queue<node> q; q.push(p); while(!q.empty()){ node tmp = q.front(); q.pop(); if(tmp.x == c && tmp.y == d){ sum[cnt] += tmp.step; return; } for(int i=0;i<4;i++){ int xx = tmp.x + dx[i]; int yy = tmp.y + dy[i]; if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=‘#‘){ vis[xx][yy] = 1; node tp; tp.x = xx,tp.y = yy,tp.step = tmp.step + 1; q.push(tp); } } } } int main() { while(cin >> n >> m){ num = 0; memset(sum,0,sizeof(sum)); if(!n || !m){ break; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> mapn[i][j]; if(mapn[i][j] == ‘@‘){ p[num].x = i,p[num].y = j,num++; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mapn[i][j] == ‘Y‘ || mapn[i][j] == ‘M‘){ for(int k=0;k<num;k++){ memset(vis,0,sizeof(vis)); bfs(i,j,p[k].x,p[k].y,k); } } } } sort(sum,sum+num); cout << sum[0] * 11 << endl; } return 0; }
后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<cmath> using namespace std; #define maxn 40005 #define inf 0x1f1f1f1f int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[205][205][2],vis[205][205],z; char mapn[205][205]; struct node{ int x,y,step; }; void bfs(int a,int b){ memset(vis,0,sizeof(vis));//在这里重置vis数组 node p; p.x = a,p.y = b,p.step = 0; vis[p.x][p.y] = 1; queue<node> q; q.push(p); while(!q.empty()){ node tmp = q.front(); q.pop(); for(int i=0;i<4;i++){ int xx = tmp.x + dx[i]; int yy = tmp.y + dy[i]; if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=‘#‘){ vis[xx][yy] = 1; node tp; tp.x = xx,tp.y = yy,tp.step = tmp.step + 1; sum[tp.x][tp.y][z] = tp.step; q.push(tp); } } } } int main() { while(cin >> n >> m){ num = 0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ sum[i][j][0] = inf; sum[i][j][1] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数 } } if(!n || !m){ break; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> mapn[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mapn[i][j] == ‘Y‘){ z = 0; bfs(i,j); } if(mapn[i][j] == ‘M‘){ z = 1; bfs(i,j); } } } int ans = inf; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mapn[i][j] == ‘@‘){ ans = min(ans,sum[i][j][0] + sum[i][j][1]); } } } cout << ans*11 << endl; } return 0; }
以上是关于[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的主要内容,如果未能解决你的问题,请参考以下文章
[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master
[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612
[kuangbin带你飞]专题一 简单搜索 E. Find The Multiple