1、计算所有点,到所有点的最短路径。 (马步), 国王的步伐可以O(1)计算。
2、穷举国王要被“接”的位置。 PS:被接送的位置,和国王原来位置,距离不会差太远。 具体差多远…… 我没证明,我猜是3
3、穷举国王和骑士的会和地点
4、穷举所有的骑士, 骑士i来接送国王
5、特判:没有骑士来接送国王
注意:
1、RC分别为高和宽
2、可能会出现骑士无法到达汇合点的情况。
3、可能会出现骑士无法到达接送国王的情况。
Executing... Test 1: TEST OK [0.008 secs, 8912 KB] Test 2: TEST OK [0.011 secs, 8912 KB] Test 3: TEST OK [0.011 secs, 8912 KB] Test 4: TEST OK [0.027 secs, 8912 KB] Test 5: TEST OK [0.176 secs, 8912 KB] Test 6: TEST OK [0.294 secs, 8912 KB] Test 7: TEST OK [0.011 secs, 8912 KB] Test 8: TEST OK [0.019 secs, 8912 KB] Test 9: TEST OK [0.068 secs, 8912 KB] Test 10: TEST OK [0.572 secs, 8912 KB] Test 11: TEST OK [0.011 secs, 8912 KB] Test 12: TEST OK [0.008 secs, 8912 KB] Test 13: TEST OK [0.008 secs, 8912 KB] Test 14: TEST OK [0.011 secs, 8912 KB] Test 15: TEST OK [0.008 secs, 8912 KB] Test 16: TEST OK [0.008 secs, 8912 KB] Test 17: TEST OK [0.005 secs, 8912 KB] Test 18: TEST OK [0.008 secs, 8912 KB] Test 19: TEST OK [0.011 secs, 8912 KB] Test 20: TEST OK [0.008 secs, 8912 KB] All tests OK.
/* TASK:camelot LANG:C++ */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> using namespace std; int R,C; //列 行 int knights = 0; int r[1150], c[1150]; bool vis[28][42] = {0}; int dis[28][42][28][42]={0}; int inf; const int dx[] = {1, 1, 2, 2, -1, -1, -2, -2}; const int dy[] = {2, -2, 1, -1, 2, -2, 1, -1}; deque<pair<int, int> >q; inline void spfa(int x, int y) { q.push_back(make_pair(x, y)); vis[x][y] = true; dis[x][y][x][y] = 0; while (!q.empty()) { int nowx = q.front().first; int nowy = q.front().second; vis[nowx][nowy] = false; q.pop_front(); for (int i = 0; i != 8; ++ i) { int willx = nowx + dx[i]; int willy = nowy + dy[i]; if (willx < 1 || willy < 1 || willx > R || willy > C) continue; if (dis[x][y][nowx][nowy] + 1 < dis[x][y][willx][willy] ) { dis[x][y][willx][willy] = dis[x][y][nowx][nowy] + 1; if (!vis[willx][willy]) { vis[willx][willy] = true; if (!q.empty() && dis[x][y][willx][willy] < dis[x][y][q.front().first][q.front().second]) q.push_front(make_pair(willx, willy)); else q.push_back(make_pair(willx, willy)); } } } } } void init() { scanf("%d%d\n", &C, &R); memset(dis, 65, sizeof(dis)); inf = dis[0][0][0][0]; for (int i = 1; i <= R; ++ i) for (int j = 1; j <= C; ++ j) spfa(i, j); char tmp; while ((tmp = getchar()) != EOF) { getchar(); scanf("%d", &c[knights]); r[knights ++ ] = tmp - 'A' + 1; getchar(); } } void doit() { int ans = 0x7fffffff; // 1* 不会和,直接一起会和 for (int x = 1; x <= R; ++ x) for (int y = 1; y <= C; ++ y) //穷举会合地点 { int tmp = max(abs(x - r[0]), abs(y - c[0])); //王直接走过去 for (int i = 1; i != knights; ++ i) tmp += dis[r[i]][c[i]][x][y]; //每个骑士去的距离 if (tmp < ans) ans = tmp; } // 2* 穷举会和点 for (int i = 1; i <= R; ++ i) for (int j = 1; j <= C; ++ j) //穷举会合点 { int d = max(abs(r[0] - i), abs(c[0] - j)); //会和地点和王的距离 if (d >= 3) continue; //如果会和地点距离王距离超过2步,那么结束 for (int x = 1; x <= R; ++ x) for (int y = 1; y <= C; ++ y) // 穷举汇合点 也就是王去i,j的位置,然后去x,y { int tmp = d; for (int z = 1; z != knights; ++ z) { if (dis[r[z]][c[z]][x][y] == inf) goto breakfor1; //如果有地点无法到达,直接不符合答案 tmp += dis[r[z]][c[z]][x][y]; } for (int k = 1; k != knights; ++ k) { if (dis[r[k]][c[k]][i][j] == inf) goto breakfor1; //无法到达王的位置 int sb = tmp - dis[r[k]][c[k]][x][y] + dis[r[k]][c[k]][i][j] + dis[i][j][x][y]; if (sb < ans) ans = sb; } breakfor1:; } } printf("%d\n", ans); } int main() { freopen("camelot.in","r",stdin); freopen("camelot.out","w",stdout); init(); doit(); return 0; }