前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ----(2612)Find a way

HDUOJ----(2612)Find a way

作者头像
Gxjun
发布2018-03-21 13:10:54
6700
发布2018-03-21 13:10:54
举报
文章被收录于专栏:mlml

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3177    Accepted Submission(s): 1031

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

简单的搜索,bfs,以Y和M为中心搜索

代码: 

代码语言:javascript
复制
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<deque>
  6 using namespace std;
  7 const int maxn=201;
  8 typedef struct Map
  9 {
 10   char ss;
 11   int x,y;
 12   int value;
 13 }gmap;
 14 unsigned int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};   /*·½Ïò*/
 15 gmap bb[maxn][maxn];
 16 int ans[maxn][maxn];
 17 int n,m;
 18 void bfs1(gmap a[][201],int x,int y)
 19 {
 20     deque<gmap>q;
 21     gmap temp;
 22     q.push_back(a[x][y]);
 23     while(!q.empty())
 24     {
 25         temp=q.front();
 26         q.pop_front();
 27         for(int i=0;i<4/*&&temp.ss!='@'*/;i++)
 28         {
 29 
 30           if((temp.x+dir[i][0])<0||(temp.x+dir[i][0])>=n||(temp.y+dir[i][1])<0||(temp.y+dir[i][1])>=m)  continue;
 31           if(a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='.'||a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='@')
 32           {
 33                     a[temp.x+dir[i][0]][temp.y+dir[i][1]].value+=temp.value+1;
 34                   q.push_back(a[temp.x+dir[i][0]][temp.y+dir[i][1]]);
 35                if(a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='@')
 36                {
 37                    a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss='d';
 38                    ans[temp.x+dir[i][0]][temp.y+dir[i][1]]+=a[temp.x+dir[i][0]][temp.y+dir[i][1]].value;
 39                }
 40                else
 41                    a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss='r';
 42           }
 43         }
 44     }
 45 }
 46 
 47 int main()
 48 {
 49     int i,j,sx1,sy1,sx2,sy2; 
 50     while(scanf("%d%d",&n,&m)!=EOF)
 51     {
 52          getchar();
 53         memset(ans,0,sizeof(ans));
 54         for(i=0;i<n;i++)
 55         {
 56             for(j=0;j<m;j++)
 57             {
 58                 scanf("%c",&bb[i][j].ss);
 59                 bb[i][j].value=0;
 60                 bb[i][j].x=i;
 61                 bb[i][j].y=j;
 62                 if(bb[i][j].ss=='Y')
 63                 {
 64                     sx1=i;
 65                     sy1=j;
 66                 }
 67                 else if(bb[i][j].ss=='M')
 68                 {
 69                     sx2=i;
 70                     sy2=j;
 71                 }
 72             }
 73             getchar();
 74         }
 75         bfs1(bb,sx1,sy1);
 76         for(i=0;i<n;i++)
 77         {
 78               for(j=0;j<m;j++)
 79               {
 80                       bb[i][j].value=0;
 81                     if(bb[i][j].ss=='d')
 82                         bb[i][j].ss='@';
 83                     else
 84                      if(bb[i][j].ss=='r')
 85                         bb[i][j].ss='.';
 86               }
 87         }
 88         bfs1(bb,sx2,sy2);
 89        int     minm=INT_MAX;
 90             for(i=0;i<n;i++)
 91             {
 92               for(j=0;j<m;j++)
 93               {
 94                  if(bb[i][j].ss=='d'&&minm>ans[i][j])
 95                       minm=ans[i][j];
 96               }
 97             }
 98             printf("%d\n",minm*11);
 99     }
100     return 0;
101 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-12-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Find a way
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档