HDU 2612 Find a way bfs
生活随笔
收集整理的這篇文章主要介紹了
HDU 2612 Find a way bfs
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
點(diǎn)擊打開(kāi)鏈接
Find a way
Time Limit: 3000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21857????Accepted Submission(s): 7120
Problem Description Pass 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.
Input The 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
Output For 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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...# Sample Output 668866 Author yifenfei
Source 奮斗的年代
Recommend yifenfei???|???We have carefully selected several similar problems for you:??2717?1254?1728?2102?1072
題意:
給一張地圖,Y代表第一個(gè)人所在的位置,M代表第二個(gè)人所在的位置,.代表路,#代表墻,@代表KFC, 兩個(gè)人想在同一個(gè)kfc見(jiàn)面,問(wèn)他們見(jiàn)面花費(fèi)最小的時(shí)間;分析:
遍歷地圖,對(duì)于每一KFC,分別求出Y到這個(gè)KFC的距離dy,M到這個(gè)KFC的距離dm, 如果距離為dy=-1或者dm=-1,則說(shuō)明無(wú)法到達(dá); 否則,跟新最短距離:ans=min(ans,dy+dm);這個(gè)思路比較簡(jiǎn)單,但是當(dāng)KFC的數(shù)量非常多的時(shí)候,就會(huì)超時(shí)
根據(jù)上面的思路,我們可以優(yōu)化一下,減少重復(fù)計(jì)算,設(shè)置一個(gè)距離數(shù)組dis[N][N][2];初始化為INF
dis[i][j][0]代表Y到坐標(biāo)為(i,j)的KFC的距離,dis[i][j][1]代表M到坐標(biāo)為(i,j)的KFC的距離
然后,BFS求出Y到地圖上所有點(diǎn)的最短距離,若這個(gè)點(diǎn)是KFC,則更新dis[i][j][0],
同理,BFS求出M到地圖上所有點(diǎn)的最短距離,若這個(gè)點(diǎn)是KFC,則更新dis[i][j][1],
最后遍歷地圖,如果這個(gè)點(diǎn)是KFC,且當(dāng)前最短距離 > Y到這個(gè)KFC的距離+M到這個(gè)KFC的距離,
則更新最短距離,最后不要忘記*11哦
超時(shí)代碼
AC代碼
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; const int N = 210; const int inf = 100000000; int n, m, flag;//flag=0代表Y,flag=1代表M int dis[N][N][2]; //dis[i][j][0]代表Y到坐標(biāo)為(i,j)的KFC的距離 //dis[i][j][1]代表M到坐標(biāo)為(i,j)的KFC的距離 int mark[N][N];//標(biāo)記點(diǎn)是否在隊(duì)列中 int dir[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};//搜索方向 char s[N][N];//地圖 struct node {int x, y, step; }; void bfs(int x, int y) {queue<node>q;node temp, type;temp.x = x;temp.y = y;temp.step = 0;q.push(temp);//起點(diǎn)入隊(duì)mark[x][y] = 1;//標(biāo)記while(!q.empty()){temp = q.front();q.pop();//出隊(duì)type.step = temp.step + 1;//步數(shù)加一for(int i = 0; i < 4; i++){type.x = x = temp.x + dir[i][0];type.y = y = temp.y + dir[i][1];if(x >= 0 && x < n && y >= 0 && y < m && mark[x][y] == 0 && s[x][y]!='#')//可以走{mark[x][y] = 1;if(s[x][y] == '@')dis[x][y][flag] = type.step;//距離q.push(type);}}} } int main() {while(scanf("%d%d", &n, &m)!=EOF){int min = inf;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)dis[i][j][0] = dis[i][j][1] = inf;//初始化距離為INFfor(int i = 0; i < n; i++)scanf("%s", s[i]);for(int i = 0; i < n; i++)for(int j = 0; j < m; j++){if(s[i][j] == 'Y'){flag = 0;memset(mark, 0, sizeof(mark));bfs(i, j);}if(s[i][j] == 'M'){flag = 1;memset(mark, 0, sizeof(mark));bfs(i, j);}}for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)if(s[i][j] == '@' && min > dis[i][j][0] + dis[i][j][1])//松馳操作{min = dis[i][j][0] + dis[i][j][1];//更新最短距離}printf("%d\n", min*11);} }
總結(jié)
以上是生活随笔為你收集整理的HDU 2612 Find a way bfs的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: poj3126 Prime Path B
- 下一篇: 计蒜客 Reversion Count