三星研究院上机测试邮件范例2-optimal Path

考试时间三个小时,我在实验室耗费整整三个多小时,只做了一道题!!!说多了全是泪。。。

全英文题,看题时间就。。。。哎、算了。英文题如下:

Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0x100, 0y100) . The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his among all the possibilities.

You are given the locations of the office, Mr. Kims home, and the customers; the number of the customers is in the range of 5 to 10. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. Your program only have to report the distance of a (the) shortest path.

You don’t have to solve this problem efficiently. You could find an answer by looking up all the possible ways. If you can look up all the possibilities well, you will get a perfect score.

大概就是求一个商人从办公室出发,需要经过他今天所有的客户家,最后再回到自己家的路径。

两个地点的路径计算方法为|x1-x2| + |y1-y2|。

并且说无需考虑提高搜索效率问题,全遍历即可!

[Constraints]

5N10. Each location (x,y) is in a bounded grid, 0x100, 0y100, and x, y are integers.

[Input]

You are given 10 test cases. Each test case consists of two lines; the first line has N, the number of the customers, and the following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location consists of the coordinates (x,y), which is reprensented by ‘x y’.

#输入:输入10个测试用例。每个用例先输入今天要去的商户个数N,下一行再以坐标形式输入他办公室地址,家地址,接着输入N个商户的地址!

[Output]

Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x answer’ where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space.

 #输出:以‘#x answer’ 格式输出,x代表第几个测试用例,answer代表算出来的最短路径值。

[I/O Example]

Input (20 lines in total. In the first test case, the locations of the office and the home are (0, 0) and (100, 100) respectively, and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).)

5 Starting test case #1

0 0 100 100 70 40 30 10 10 5 90 70 50 20

6 Starting test case #2

88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14

10 Starting test case #3

39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36

...

 

Output (10 lines in total)

#1 200

#2 304

#3 366

...

#部分输入输出

#解题思路:

定义A[N]保存N个商户的拜访次序.B[]保存办公室、家、商户所在地址,所以B[]大小为N*2+4.

确定商户拜访顺序求路径:用一层for循环依次遍历每个商户的横纵坐标,进行计算即可,如程序中的copath函数。

既然全遍历即可,本题最重要的点是N个商户的先后次序进行全排列的问题。之前有做过一个递归的字符串全排列问题。如上一个博客所示。所以本题将那道题部分代码直接拿来用了一下。如func函数。

#include <stdio.h>
#include <math.h>

int min = 10000;//全局变量最短路径

int main()
{
    
 int i,j,num,N;
 scanf("%d",&num)
 for(j=0; j<num;j++ ){
    scanf("%d\n",&N);
    int a[N],b[2*N+4];
    scanf("%d %d %d %d ",&b[0],&b[1],&b[2],&b[3]);//输入办公室、家坐标
    void func(int a[],int b[],int k,int N);
    for(i = 0;i<N;i++){
    	a[i] = i+1;
    	scanf("%d %d ",&b[4+i*2],&b[5+i*2]);//商户坐标
     }
     scanf("\n");
    func(a,b,0,N);
    printf("#%d %d\n",j+1,min);
    min = 10000; //每个用例结束后min复位
 }
 
 return 0;
}

void func(int a[],int b[],int k,int N){
 int i ,temp,m;
 int copath(int a[],int b[],int N);
 
 if(k == N){
    m = copath(a,b,N);
    
    if(min > m){
       min = m;
       //printf("%d \n",min);
    }
  }
 for(i=k;i<N;i++){
   temp = a[k];
   a[k] = a[i];
   a[i] = temp;
   
   func(a,b,k+1,N);//递归
   
   temp = a[k];
   a[k] = a[i];
   a[i] = temp;
  }
  
}

int copath(int a[],int b[],int N){//计算路径
    int i,sum = 0;
    sum = sum + fabs(b[(a[0]+1)*2]-b[0]) + fabs(b[(a[0]+1)*2+1]-b[1]);
    for (i = 1;i < N;i++){
       sum = sum + fabs(b[(a[i]+1)*2]-b[(a[i-1]+1)*2]) + fabs(b[(a[i]+1)*2+1]-b[(a[i-1]+1)*2+1]);
    }
    sum = sum + fabs(b[2]-b[(a[N-1]+1)*2]) + fabs(b[3]-b[(a[N-1]+1)*2+1]);
    return sum;

}

输入文件:

输出:

与输出文件等同。

遇到的问题:路径参数(min)设置问题。第一版写完的程序中,参数(min)的声明和定义放在main函数内,func函数将参数(min)作为输入参数,发现每一次的func递归带入的min参数均为初值10000,最后结果输出也为10000。后来想到可以将min参数作为全局变量放在main函数外,此时func函数无需带上min参数,并可以直接在函数内应用,成功解决了这个问题。需要注意的是每一次测试用例结束后需要将min复位到原来的10000.

猜你喜欢

转载自blog.csdn.net/u011537121/article/details/83350371