HDU-2612 Find a way
生活随笔
收集整理的這篇文章主要介紹了
HDU-2612 Find a way
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...# Sample Output 66 88 66
思路: A題的快感勝于一切不解釋 題目不錯,讓我重新認識了bfs的功效。其實這個題思路倒是很容易,就是遍歷下兩個人到每個‘@’的距離,存起來然后看看哪個最短就行,關鍵的問題在于數據結構,即數據的存儲和操作方式。 一開始我的想法是用map,即將每個點到行走人的距離映射到該點的坐標上,但是我卡在了不會求行走距離,之前那會兒總是想用dfs來求,做的題目還不夠多= =后來上網看了題解,發現使用bfs以Y或M為初始點遍歷圖中所有點到出發點的距離,關鍵的公式就是 dis[dx][dy] = dis[temp.x][temp.y]+1;
#include <iostream> #include <cstring> #include <queue> #define INF 0x7fffffff using namespace std;int n,m; char G[207][207]; int dir[4][2] = {{-1,0},{0,-1},{0,1},{1,0}}; struct pos{int x,y; }; int dis_1[207][207]; int dis_2[207][207];bool judge(int x,int y) {if(x>=1&& x<=n && y>=1 && y<=m && G[x][y]!='#')return true;else return false; }void bfs(int a,int b,int vis[207][207],int dis[207][207]) {vis[a][b] = 1;queue<pos> q;pos s;s.x = a;s.y = b;q.push(s);while(!q.empty()){pos temp = q.front();q.pop();for(int i = 0;i <= 3;i++){int dx = temp.x+dir[i][0];int dy = temp.y+dir[i][1];if(!vis[dx][dy] && judge(dx,dy)) {vis[dx][dy] = 1;dis[dx][dy] = dis[temp.x][temp.y]+1;pos n;n.x = dx;n.y = dy;q.push(n);}}} }int main() {while(cin>>n>>m){int x1,y1,x2,y2;int vis_1[207][207];int vis_2[207][207];for(int i = 1;i <= n;i++)cin>>G[i]+1;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++) {if(G[i][j] == 'Y'){x1 = i;y1 = j;}if(G[i][j] == 'M'){x2 = i;y2 = j;}}memset(dis_1,0,sizeof(dis_1));memset(dis_2,0,sizeof(dis_2));memset(vis_1,0,sizeof(vis_1));memset(vis_2,0,sizeof(vis_2));bfs(x1,y1,vis_1,dis_1); bfs(x2,y2,vis_2,dis_2);int ans = INF;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)if(G[i][j] == '@' && vis_1[i][j] && vis_2[i][j])ans = dis_1[i][j]+dis_2[i][j]<ans?dis_1[i][j]+dis_2[i][j]:ans;cout<<ans*11<<endl;}return 0; }
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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...# Sample Output 66 88 66
思路: A題的快感勝于一切不解釋 題目不錯,讓我重新認識了bfs的功效。其實這個題思路倒是很容易,就是遍歷下兩個人到每個‘@’的距離,存起來然后看看哪個最短就行,關鍵的問題在于數據結構,即數據的存儲和操作方式。 一開始我的想法是用map,即將每個點到行走人的距離映射到該點的坐標上,但是我卡在了不會求行走距離,之前那會兒總是想用dfs來求,做的題目還不夠多= =后來上網看了題解,發現使用bfs以Y或M為初始點遍歷圖中所有點到出發點的距離,關鍵的公式就是 dis[dx][dy] = dis[temp.x][temp.y]+1;
當時第一次看這到這個公式的時候反應是DP(我真是好久沒做DP了)
后來發現原來這就是bfs的精髓
#include <iostream> #include <cstring> #include <queue> #define INF 0x7fffffff using namespace std;int n,m; char G[207][207]; int dir[4][2] = {{-1,0},{0,-1},{0,1},{1,0}}; struct pos{int x,y; }; int dis_1[207][207]; int dis_2[207][207];bool judge(int x,int y) {if(x>=1&& x<=n && y>=1 && y<=m && G[x][y]!='#')return true;else return false; }void bfs(int a,int b,int vis[207][207],int dis[207][207]) {vis[a][b] = 1;queue<pos> q;pos s;s.x = a;s.y = b;q.push(s);while(!q.empty()){pos temp = q.front();q.pop();for(int i = 0;i <= 3;i++){int dx = temp.x+dir[i][0];int dy = temp.y+dir[i][1];if(!vis[dx][dy] && judge(dx,dy)) {vis[dx][dy] = 1;dis[dx][dy] = dis[temp.x][temp.y]+1;pos n;n.x = dx;n.y = dy;q.push(n);}}} }int main() {while(cin>>n>>m){int x1,y1,x2,y2;int vis_1[207][207];int vis_2[207][207];for(int i = 1;i <= n;i++)cin>>G[i]+1;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++) {if(G[i][j] == 'Y'){x1 = i;y1 = j;}if(G[i][j] == 'M'){x2 = i;y2 = j;}}memset(dis_1,0,sizeof(dis_1));memset(dis_2,0,sizeof(dis_2));memset(vis_1,0,sizeof(vis_1));memset(vis_2,0,sizeof(vis_2));bfs(x1,y1,vis_1,dis_1); bfs(x2,y2,vis_2,dis_2);int ans = INF;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)if(G[i][j] == '@' && vis_1[i][j] && vis_2[i][j])ans = dis_1[i][j]+dis_2[i][j]<ans?dis_1[i][j]+dis_2[i][j]:ans;cout<<ans*11<<endl;}return 0; }
轉載于:https://www.cnblogs.com/immortal-worm/p/5133046.html
總結
以上是生活随笔為你收集整理的HDU-2612 Find a way的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS ASIHttpRequest 封
- 下一篇: MDEV Primer