暑假集训(1)第四弹 -----Find a way(Hdu2612)

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
[email protected]
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

问题分析:依旧是bfs,不过要考虑kfc被包围,以及重置地图,和两人所占位都是可以互相走过的。

  1 #include "iostream"
  2 #include "queue"
  3 using namespace std;
  4 struct ma
  5 {
  6     char z;
  7     int sum;
  8 };
  9 struct person
 10 {
 11     int i;
 12     int j;
 13     int time;
 14 };
 15 int v[4][2] ={1,0,-1,0,0,-1,0,1};
 16 ma maze[202][202];
 17 ma maze2[202][202];
 18 person fir,sec;
 19 void scopy(int n,int m)
 20 {
 21      for (int i=0;i<=n+1;i++)
 22       for (int j=0;j<=m+1;j++)
 23       {
 24           if (maze2[i][j].z == ‘@‘)
 25                maze[i][j].z = ‘@‘;
 26           else
 27             maze[i][j]=maze2[i][j];
 28       }
 29 }
 30 void mbegin(int n,int m)
 31 {
 32     int i,j;
 33     for (i=0;i<=n+1;i++)
 34      for (j=0;j<=m+1;j++)
 35        if (i*j == 0 || i == n+1 || j ==m+1)
 36              maze2[i][j].z =‘#‘;
 37        else
 38        {
 39          cin>>maze2[i][j].z;
 40          maze2[i][j].sum=0;
 41          maze[i][j].sum=0;
 42          if (maze2[i][j].z == ‘Y‘)
 43          {
 44              fir.i = i;
 45              fir.j = j;
 46          }
 47          if (maze2[i][j].z == ‘M‘)
 48          {
 49              sec.i =i;
 50              sec.j =j;
 51          }
 52        }
 53 }
 54 void bfs(person &then)
 55 {
 56    queue<person> p;
 57    person next;
 58    then.time=0;
 59    maze[then.i][then.j].z = ‘#‘;
 60    p.push(then);
 61    while (!p.empty())
 62    {
 63        next = p.front();
 64        p.pop();
 65       for (int k=0;k<4;k++)
 66       {
 67             then.i = next.i+v[k][0];
 68             then.j = next.j+v[k][1];
 69         if (maze[then.i][then.j].z != ‘#‘)
 70         {
 71             then.time = next.time+11;
 72             if (maze[then.i][then.j].z == ‘@‘)
 73               maze[then.i][then.j].sum += then.time;
 74             maze[then.i][then.j].z =‘#‘;
 75             p.push(then);
 76         }
 77       }
 78    }
 79 }
 80 int mintime(int n,int m)
 81 {
 82    int k=0,time;
 83   for (int i=1;i<=n;i++)
 84     for (int j=1;j<=m;j++)
 85       if (maze[i][j].z == ‘@‘)
 86       {
 87          if (maze[i][j].sum == 0)
 88                           continue;
 89           if (k++ == 0)
 90              time=maze[i][j].sum;
 91           if (time > maze[i][j].sum)
 92               time=maze[i][j].sum;
 93       }
 94     return time;
 95 }
 96 int main()
 97 {
 98   int n,m;
 99     while (cin>>n>>m)
100     {
101         mbegin(n,m);
102         scopy(n,m);
103         bfs(fir);
104         scopy(n,m);
105         bfs(sec);
106          scopy(n,m);
107         cout<<mintime(n,m)<<endl;
108     }
109     return 0;
110 }

时间: 2024-12-09 11:54:09

暑假集训(1)第四弹 -----Find a way(Hdu2612)的相关文章

暑假集训(4)第四弹 -----排列,计数(hdu1465)

题意概括:嗯,纵使你数次帮助小A脱离困境,但上一次,小A终于还是失败了.那数年的奔波与心血,抵不过轻轻一指,便彻底 湮灭,多年的友谊终归走向末路.这一切重击把小A彻底击溃! 不为什么,你到底还是要继续帮助小A,让他走出这难言的挫折.不过怎样帮助他呢?苦苦思索之际,你不小心看到某美分集中营 上的“心灵鸡汤”,决定用这些四溢着“美味”的鸡汤帮助小A,第一个鸡汤是举例子,一个人给他的每一个网友寄信,但是都寄错了, 你认为寄错的次数绝对比完全正确的1种庞大的多,所以你需要算出这些数字来安慰小A. 问题分

暑假集训(2)第四弹 ----- 敌兵布阵(hdu1166)

D - 敌兵布阵 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Lily 特别喜欢养花,但是由于她的花特别多,所以照料这些花就变得不太容易.她把她的花依次排成一行,每盆花都有一个美观值.如果Lily把某盆花照料的好的话,这盆花的美观值就会上升,如果照料的不好的话,这盆花的美观值就会下降.有

暑假集训(2)第五弹 ----- Who&#39;s in the Middle(poj2388)

G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median

暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了. 作为球迷,一定想看尽量

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

暑假集训(1)第八弹 -----Catch the cow(Poj3984)

Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. Input 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. Output 左上角到右下角的最短路径,格式如样例所示. Sa

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

暑假集训(4)第五弹——— 数论(hdu1222)

题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A又迟迟不现身,FFF团团长连下七道圣火令追杀你们,最先赶到地,机械化部队,它们除了智能不高外,可以说是无懈可击.这正是你要利用的一点,利用他们的行动轨迹,躲藏起来. 问题分析:首先用辗转相除法求得gcd(n,m),若n>m 则gcd(n,m)为一可逃反之,非一可逃. 1 #include "c