RT, 样例不过但是没看出什么问题??
#include<bits/stdc++.h>
using namespace std;
const int maxn = 50 + 5;
int n, m, k1, k2, ans = INT_MAX;
int read() {
int ret = 0, f = 1;char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * f;
}
struct node {
int a, b;
};
bool vis[maxn][maxn];
node s[maxn * maxn >> 1];
void dfs(int x, int y, int step, int maxx) {
if(vis[x - 1][y] && vis[x + 1][y] && !vis[x][y + 1] && !vis[x][y - 1]) return;
if(!vis[x - 1][y] && !vis[x + 1][y] && vis[x][y + 1] && vis[x][y - 1])return;
// if ( (vis[x + 1][y] && vis[x - 1][y] && !(vis[x][y + 1] || vis[x][y - 1])) || (vis[x][y + 1] && vis[x][y - 1] && !(vis[x + 1][y] || vis[x - 1][y])) ) return;
if (maxx > ans) return;
if (step - n * m / 2 <= 0) {
s[step].a = x, s[step].b = y;
} else {
maxx = max(maxx, abs(s[step - n * m / 2].a - x) * k1 + k2 * abs(s[step - n * m / 2].b - y));
}
if (step == n * m) {
ans = min(ans, maxx);
return;
}
if (x + 1 <= m && !vis[x + 1][y]) {
vis[x + 1][y] = 1;
dfs(x + 1, y, step + 1, maxx);
vis[x + 1][y] = 0;
}
if (x - 1 >= 1 && !vis[x - 1][y]) {
vis[x - 1][y] = 1;
dfs(x - 1, y, step + 1, maxx);
vis[x - 1][y] = 0;
}
if (y + 1 <= n && !vis[x][y + 1]) {
vis[x][y + 1] = 1;
dfs(x, y + 1, step++, maxx);
vis[x][y + 1] = 0;
}
if (y - 1 >= 1 && !vis[x][y - 1]) {
vis[x][y - 1] = 1;
dfs(x, y - 1, step++, maxx);
vis[x][y - 1] = 0;
}
}
void init() {
for (int i = 1;i <= n;i++) {
vis[i][0] = 1;
vis[i][m + 1] = 1;
}
for (int i = 1;i <= m;i++) {
vis[0][i] = 1;
vis[n + 1][i] = 1;
}
return;
}
int main() {
n = read(), m = read(), k1 = read(), k2 = read();
init();
dfs(1, m, 1, -INT_MAX);
printf("%d\n", ans);
return 0;
}