|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|1 초||128 MB||4||3||3||75.000%|
USC has a bunch of quite yummy lunch options.2 Sometimes, when you go for lunch, you want to meet your friends, and for that, there are some great meeting spots on campus, such as Tommy Trojan. But you have to carefully plan where you meet, depending on which restaurant you go to, to minimize the total distance you walk.
More precisely, you will be given a map of campus, with certain spots marked as meeting spots, and others as restaurants. In addition, you are given the starting points for yourself and the friends you are having lunch with. If you have selected a meeting point and a restaurant, this means that you and all your friends will be walking from your respective locations to the meeting point, then to the restaurant, and back to your respective starting locations. Naturally, you want to select the pair of meeting point and restaurant which minimizes the sum of walking distances for you and all your friends.
2If you haven’t done so, check out Parkside Restaurant and Galen Athletic Center.
The first line contains a number K ≥ 1, which is the number of input data sets in the file. This is followed by K data sets of the following form:
The first line of each data set contains numbers h and w, the height and width of the campus map (1 ≤ h, w ≤ 30). This is followed by h lines of w characters each. Each character is one of the following:
Notice that you can only move up, down, left, or right on this map, not diagonally.
For each data set, first output “Data Set x:” on a line by itself, where x is its number. Then, output the total length traveled by you and all your friends together for the best pair of meeting point and restaurant. (You do not need to output the sequence.)
If there is no meeting point, or no restaurant, accessible from all starting points, then output “Impossible” instead of the total length.
2 5 5 ..X.. S.X.S .MX.M ..X.. ..R.. 7 7 S...... M...X.. ...MX.. .XX.X.. ..X.X.. .SX.X.. .R..XRS
Data Set 1: Impossible Data Set 2: 62