Title: A robot from a starting point (only up, down, left, right movement), after some points back to the starting point, the total path to the minimum length.
Analysis: Graph theory, search. The distance between two points is: ABS (X1-X2) + ABS (Y1-Y2), each point must pass at least once.
If there is a point to go through many times, then he must be in the other two points between the path, then this point can not go through so many times;
Therefore, we only consider the situation of each point after one time (there may be a point on a two-point path), to find the whole arrangement, enumerate the optimal solution.
Description: The origin of overconfidence is inferiority ╮(╯▽╰)╭.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std;int save[3628800][11],temp[11],used[11],count;void dfs (int d, int N) {if (d = = N) {for (int i = 0; i < D; + + i) save[count][i] = Temp[i]; Count ++;return;} for (int i = 0; i < n; + + i) if (!used[i]) {used[i] = 1;temp[d] = I;dfs (d+1, n); Temp[d] = i;used[i] = 0;}} int X[11],y[11];int Main () {int t,n,m,s_x,s_y,d;while (cin >> T) while (T--) {cin >> N >> M >> s_x >> s_y >> d;for (int i = 0; i < D; + + i) Cin >> X[i] >> y[i]; Count = 0;dfs (0, D); int Min = 444444;for (int i = 0; i < Count; + + i) {int dist = ABS (s_x-x[save[i][0]) + ABS (s_y-y[ Save[i][0]]); for (int j = 1; j < D; + + j) Dist + = ABS (X[save[i][j-1]]-x[save[i][j]) + ABS (y[save[i][j-1]]-y[save[i][j ]]);d ist + = ABS (S_x-x[save[i][d-1]]) + ABS (S_y-y[save[i][d-1]); min = min (min, dist);} cout << "the shortest Path has length "<< Min << Endl;} return 0;}
UVa 10496-collecting beepers