https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1437
Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position. Karel can only move along the x and y axis, never diagonally. Moving from one position (i, j) to an adjacent position (i, j + 1), (i, j − 1), (i − 1, j), or (i + 1, j) has a cost of one. You can assume that Karel’s world is never larger than 20 × 20 squares and that there will never be more than 10 beepers to pick up. Each coordinate will be given as a pair (x, y) where each value will be in the range 1 through the size of that particular direction of the coordinate system.
http://www.voidcn.com/article/p-sfzmnzuz-ud.html
Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position. Karel can only move along the x and y axis, never diagonally. Moving from one position (i, j) to an adjacent position (i, j + 1), (i, j − 1), (i − 1, j), or (i + 1, j) has a cost of one. You can assume that Karel’s world is never larger than 20 × 20 squares and that there will never be more than 10 beepers to pick up. Each coordinate will be given as a pair (x, y) where each value will be in the range 1 through the size of that particular direction of the coordinate system.
http://www.voidcn.com/article/p-sfzmnzuz-ud.html
题目:一个机器人从一个起始点出发(只能上、下、左、右运动),经过一些点后回到起点,求总路径最小长度。
分析:图论,搜索。两点间的距离为:abs(x1-x2)+ abs(y1-y2);每个点必须至少经过一次。
如果存在一个点走过多次,那么他一定在其他两点间的路径上,则这个点可以不经过这么多次;
因此我们只考虑每个点经过一次的情况即可(可能存在点在某两点路径上),求出全排列,枚举最优解。
https://algorithmcafe.wordpress.com/category/uva/problem-solving-paradigms/dynamic-programming/traveling-salesman-problem-tsp/
https://mohamedmosamin.wordpress.com/2012/10/23/uva-10496-collecting-beepers/
public
static
void
main(String[] args)
throws
NumberFormatException,
IOException {
BufferedReader buf =
new
BufferedReader(
new
InputStreamReader(System.in));
int
t = Integer.parseInt(buf.readLine());
while
(t-- >
0
) {
String[] line = buf.readLine().split(
" "
);
line = buf.readLine().split(
" "
);
int
sX = Integer.parseInt(line[
0
]);
int
sY = Integer.parseInt(line[
1
]);
int
n = Integer.parseInt(buf.readLine());
int
[][] matrix =
new
int
[n +
1
][n +
1
];
int
[][] points =
new
int
[n +
1
][
2
];
points[
0
][
0
] = sX;
points[
0
][
1
] = sY;
for
(
int
i =
1
; i <= n; i++) {
line = buf.readLine().split(
" "
);
int
x = Integer.parseInt(line[
0
]);
int
y = Integer.parseInt(line[
1
]);
points[i][
0
] = x;
points[i][
1
] = y;
}
for
(
int
i =
0
; i <= n; i++)
for
(
int
j =
0
; j <= n; j++) {
matrix[i][j] = matrix[j][i] = Math.abs(points[j][
0
]
- points[i][
0
])
+ Math.abs(points[j][
1
] - points[i][
1
]);
}
int
[][] dp =
new
int
[n +
1
][(
1
<< (n +
1
)) +
1
];
for
(
int
i =
0
; i < dp.length; i++)
Arrays.fill(dp[i], -
1
);
int
min = TSP(
0
,
0
, dp, matrix);
System.out.printf(
"The shortest path has length %d\n"
, min);
}
}
private
static
int
TSP(
int
node,
int
bitmask,
int
[][] dp,
int
[][] matrix) {
if
(bitmask == (
1
<< dp.length) -
1
&& node ==
0
)
return
0
;
if
(bitmask == (
1
<< dp.length) -
1
)
return
100000
;
else
if
(dp[node][bitmask] != -
1
)
return
dp[node][bitmask];
else
{
int
min = Integer.MAX_VALUE;
for
(
int
i =
0
; i < dp.length; i++)
if
((bitmask & (
1
<< i)) ==
0
) {
min = Math.min(
min,
matrix[node][i]
+ TSP(i, bitmask | (
1
<< i), dp, matrix));
}
return
dp[node][bitmask] = min;
}
}
static
int
n, r, c;
static
int
[][] dp, beepers;
static
int
max =
1
<<
23
;
static
int
dist(
int
a,
int
b){
return
Math.abs(beepers[a][
0
] - beepers[b][
0
]) + Math.abs(beepers[a][
1
] - beepers[b][
1
]);
}
static
int
solve(
int
i,
int
bitmask) {
if
((bitmask == (
1
<< (n +
1
)) -
1
)){
return
dist(
0
, i);
}
else
if
(dp[i][bitmask] != -
1
){
return
dp[i][bitmask];
}
else
{
dp[i][bitmask] = max;
for
(
int
j =
0
; j <= n; j++) {
if
(((
1
<<j) & bitmask) ==
0
)
dp[i][bitmask] = Math.min(dp[i][bitmask], dist(i, j) + solve(j, bitmask | (
1
<<j)));
}
return
dp[i][bitmask];
}
}
public
static
void
main(String[] args)
throws
NumberFormatException,
IOException {
Scanner sc =
new
Scanner(System.in);
StringBuilder out =
new
StringBuilder();
int
t = sc.nextInt();
int
si,sj;
for
(
int
i =
0
; i < t; i++) {
r = sc.nextInt();
c = sc.nextInt();
si = sc.nextInt();
sj = sc.nextInt();
n = sc.nextInt();
beepers =
new
int
[n+
1
][
2
];
dp =
new
int
[n+
1
][(
1
<<(n +
1
)) +
1
];
for
(
int
j =
0
; j < dp.length; j++) {
for
(
int
k =
0
; k < dp[
0
].length; k++) {
dp[j][k] = -
1
;
}
}
beepers[
0
][
0
] = si;
beepers[
0
][
1
] = sj;
for
(
int
j =
1
; j <= n; j++) {
beepers[j][
0
] = sc.nextInt();
beepers[j][
1
] = sc.nextInt();
}
out.append(
"The shortest path has length "
+ solve(
0
,
1
) +
"\n"
);
}
System.out.print(out);
}
int DP[maxState][maxN], map[20][20];
int TSP(int state, int last, int n) {
if(state == 0 && last != 0) return 0xfffff;
if(DP[state][last] != -1) return DP[state][last];
int i, min = 0xfffff, tmp;
for(i = 0; i <= n; i++) {
if((state&(1<<i)) != 0) {
tmp = TSP(state-(1<<i), i, n);
tmp += map[i][last];
if(min > tmp) min = tmp;
}
}
DP[state][last] = min;
return DP[state][last];
}
int const MAXB = 11;
int dist[MAXB][MAXB];
int memo[MAXB][1 << MAXB];
int x[MAXB];
int y[MAXB];
int nx, ny, n;
int solve(int index, int mask)
{
if (mask == (1 << (n + 1)) - 1)
return dist[index][0];
if (memo[index][mask] != -1)
return memo[index][mask];
memo[index][mask] = INT_MAX;
for (int i = 0; i <= n; ++i)
if (i != index && !(mask & (1 << i)))
memo[index][mask] = min(memo[index][mask], solve(i, mask ^ (1 << i)) + dist[index][i]);
return memo[index][mask];
}
int main()
{
int T; cin >> T;
while (T-- != 0)
{
cin >> nx >> ny;
cin >> x[0] >> y[0];
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> x[i] >> y[i];
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
memset(memo, -1, sizeof(memo));
printf("The shortest path has length %d\n", solve(0, 1));
}
return 0;
}