HDU - 2612 Find a way(BFS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27179557/article/details/79962890


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. 

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
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

思路:题意就是两个人选择一个接头地点,两个人去见面,然后要求两个人到达那个地点的路程之和最小。

可以分别计算两个人到达每个kfc的距离,用广度搜索计算,然后从两人都能到达的kfc中选取路程最小的那个。

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 typedef struct{
 9     int i,j;
10     int step;
11 }loca;
12 
13 int n,m;
14 char mm[205][205];//地图
15 int flag[205][205];//此位置是否待过
16 int step[205][205];//到达此处的步数,只有@时才赋值
17 int isarrival[205][205];//如果此处两个人都到达才可以
18 int yifeni=0,yifenj=0;//yifen位置
19 int meri=0,merj=0;//mer位置
20 queue<loca> q;
21 loca now,nextt;
22 int add[2][4]={-1,1,0,0,
23                 0,0,-1,1};
24 
25 void go(int si,int sj){
26     now.i=si;
27     now.j=sj;
28     now.step=0;
29     flag[si][sj]=1;
30     q.push(now);
31     while(!q.empty()){
32         now=q.front(); q.pop();
33         if(mm[now.i][now.j]=='@'){
34             step[now.i][now.j]+=now.step;
35             isarrival[now.i][now.j]++;
36         }
37         flag[now.i][now.j]=1;
38         for(int i=0;i<4;i++){
39             nextt.i=now.i+add[0][i];
40             nextt.j=now.j+add[1][i];
41             nextt.step=now.step+1;
42             if(nextt.i<0||nextt.i>=n||nextt.j<0||nextt.j>m){
43                 continue;
44             }
45             if(flag[nextt.i][nextt.j]==0 && mm[nextt.i][nextt.j]!='#'){
46                 q.push(nextt);
47                 flag[nextt.i][nextt.j]=1;
48             }
49         }
50     }
51 }
52 
53 int main()
54 {
55     while(~scanf("%d %d",&n,&m)){
56         memset(step,0,sizeof(step));
57         memset(isarrival,0,sizeof(isarrival));
58         memset(flag,0,sizeof(flag));
59         for(int i=0;i<n;i++){
60             getchar();
61             for(int j=0;j<m;j++){
62                 scanf("%c",&mm[i][j]);
63                 if(mm[i][j]=='Y'){
64                     yifeni=i; yifenj=j;
65                 }
66                 if(mm[i][j]=='M'){
67                     meri=i; merj=j;
68                 }
69             }
70         }
71         go(yifeni,yifenj);
72         memset(flag,0,sizeof(flag));
73         go(meri,merj);
74 
75         int minn=0x3f3f3f3f;
76         for(int i=0;i<n;i++){
77             for(int j=0;j<m;j++){
78                 if(step[i][j]!=0&&isarrival[i][j]==2){
79                     int temp=step[i][j];
80                     if(temp<minn){
81                         minn=temp;
82                     }
83                 }
84             }
85         }
86         printf("%d\n",minn*11);
87     }
88     return 0;
89 }
阅读更多

Find A Way

11-25

Problem DescriptionnWe'll consider an interesting geometric problem here. Given a number of circles with varying radius on the plane, and define the P-value of a point (x, y) on the plane as the number of circles covering this point. Here, by "covering", we mean that the point is either strictly within the circle, or on the boundary of the circle.nGiven the starting position (Sx, Sy), and the destination position (Tx, Ty), please find a path between the two points, such that every point of the path is on the boundary of one or more circles, and the absolute difference between the maximum P-value and the minimum P-value among all points on the path is minimized. nCan you find the minimum absolute value with the help of your computer?n nnInputnThere are multiple test cases in the input file. Each test case starts with one integer N (1 <= N <= 150), the number of circles, followed by four real numbers, Sx, Sy, Tx, Ty, representing the x-coordinate and y-coordinate of the starting position and the destination. Each of the following N lines consists of three real numbers X, Y and R (R >= 1), indicating that there is a circle at position (X, Y) with radius R.nThere is a blank line after each test case. Input ends with End-of-File.nNote: It is guaranteed that the input data is always legal, i.e. both the starting position and the destination are on the boundary of one or more circles, no two circles will be at the same position, every real number in the input file has at most three digits after the decimal point, and the absolute value of any real number does not exceed 10000.n nnOutputnFor each test case, output one integer on one separate line as requested. If there is no way to reach the destination, output -1 instead.n nnSample Inputn2n-1.000 0.000 1.000 0.000n0.000 0.000 1.000n1.000 0.000 1.000nn2 n-1.000 0.000 5.000 0.000n-1.000 -1.000 1.000n4.000 0.000 1.000n nnSample OutputnCase 1: 1nCase 2: -1n

没有更多推荐了,返回首页