usaco3.3.3 Camelot

来源:互联网 发布:买衣服 知乎 编辑:程序博客网 时间:2024/05/18 00:35

一 原题

Camelot
IOI 98

Centuries ago, King Arthur and the Knights of the Round Table used to meet every year on New Year's Day to celebrate their fellowship. In remembrance of these events, we consider a board game for one player, on which one chesspiece king and several knight pieces are placed on squares, no two knights on the same square.

This example board is the standard 8x8 array of squares:

The King can move to any adjacent square from  to  as long as it does not fall off the board:

A Knight can jump from  to , as long as it does not fall off the board:

During the play, the player can place more than one piece in the same square. The board squares are assumed big enough so that a piece is never an obstacle for any other piece to move freely.

The player's goal is to move the pieces so as to gather them all in the same square - in the minimal number of moves. To achieve this, he must move the pieces as prescribed above. Additionally, whenever the king and one or more knights are placed in the same square, the player may choose to move the king and one of the knights together from that point on, as a single knight, up to the final gathering point. Moving the knight together with the king counts as a single move.

Write a program to compute the minimum number of moves the player must perform to produce the gathering. The pieces can gather on any square, of course.

PROGRAM NAME: camelot

INPUT FORMAT

Line 1:Two space-separated integers: R,C, the number of rows and columns on the board. There will be no more than 26 columns and no more than 30 rows.Line 2..end:The input file contains a sequence of space-separated letter/digit pairs, 1 or more per line. The first pair represents the board position of the king; subsequent pairs represent positions of knights. There might be 0 knights or the knights might fill the board. Rows are numbered starting at 1; columns are specified as upper case characters starting with `A'.

SAMPLE INPUT (file camelot.in)

8 8D 4A 3 A 8H 1 H 8

The king is positioned at D4. There are four knights, positioned at A3, A8, H1, and H8.

OUTPUT FORMAT

A single line with the number of moves to aggregate the pieces.

SAMPLE OUTPUT (file camelot.out)

10

SAMPLE OUTPUT ELABORATION

They gather at B5. 
Knight 1: A3 - B5 (1 move) 
Knight 2: A8 - C7 - B5 (2 moves) 
Knight 3: H1 - G3 - F5 - D4 (picking up king) - B5 (4 moves) 
Knight 4: H8 - F7 - D6 - B5 (3 moves) 
1 + 2 + 4 + 3 = 10 moves. 


二 分析

棋盘(最大20*26)上有若干骑士和一个国王,骑士可以走“日”字,国王只能走单格的对角线。当一个骑士和国王汇集在同一位置时,骑士可以“带”着国王移动(即国王和骑士同时走“日”字,只算走了一步)。问把棋盘上所有棋子汇聚到同一位置最少需要多少步。

国王走得慢,直觉上国王不应该走太多步。因此我枚举了国王最多走2步的所有情况(正确性也不能保证),国王移动完后等待某个骑士过来将其送至目标点。棋盘上任意两点的距离用BFS算一下就好了。


三 代码

运行结果:

USER: Qi Shen [maxkibb3]TASK: camelotLANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.000 secs, 4192 KB]   Test 2: TEST OK [0.000 secs, 4200 KB]   Test 3: TEST OK [0.000 secs, 4200 KB]   Test 4: TEST OK [0.011 secs, 4204 KB]   Test 5: TEST OK [0.086 secs, 4212 KB]   Test 6: TEST OK [0.119 secs, 4212 KB]   Test 7: TEST OK [0.000 secs, 4192 KB]   Test 8: TEST OK [0.000 secs, 4204 KB]   Test 9: TEST OK [0.054 secs, 4208 KB]   Test 10: TEST OK [0.173 secs, 4468 KB]   Test 11: TEST OK [0.000 secs, 4324 KB]   Test 12: TEST OK [0.000 secs, 4200 KB]   Test 13: TEST OK [0.000 secs, 4224 KB]   Test 14: TEST OK [0.000 secs, 4204 KB]   Test 15: TEST OK [0.000 secs, 4200 KB]   Test 16: TEST OK [0.000 secs, 4200 KB]   Test 17: TEST OK [0.000 secs, 4200 KB]   Test 18: TEST OK [0.000 secs, 4192 KB]   Test 19: TEST OK [0.000 secs, 4208 KB]   Test 20: TEST OK [0.000 secs, 4224 KB]All tests OK.

Your program ('camelot') produced all correct answers! This is yoursubmission #4 for this problem. Congratulations!


AC代码(略丑):

/*ID:maxkibb3LANG:C++PROB:camelot*/#include<cstdio>#include<iostream>#include<fstream>#include<vector>#include<queue>#include<cstring>using namespace std;const int MAXR = 35;const int MAXC = 30;int dx[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},                {1, -2}, {1, 2}, {2, -1}, {2, 1}};int bias[25][2] = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},                   {-1, -2}, {-1, -1}, {-1, 0}, {-1, 1}, {-1, 2},                   {0, -2}, {0, -1}, {0, 0}, {0, 1}, {0, 2},                   {1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2},                   {2, -2}, {2, -1}, {2, 0}, {2, 1}, {2, 2}};int R, C;int king_r, king_c;vector<int> knight_r;vector<int> knight_c;vector<int> pick_r;vector<int> pick_c;int vis[MAXR][MAXC];int ans = 0x7fffffff;int _abs(int num) {    if(num < 0) return -num;    else return num;}struct Node {    int r, c, depth;};Node node(int _r, int _c, int _depth) {    Node ret;    ret.r = _r;    ret.c = _c;    ret.depth = _depth;    return ret;}struct Dis {    int d[MAXR][MAXC];};vector<Dis> knight_dis;vector<Dis> pick_dis;Dis dis(int _d[MAXR][MAXC]) {    Dis ret;    memcpy(ret.d, _d, sizeof(ret.d));    return ret;}void init() {    ifstream fin;    fin.open("camelot.in");    int r;    char c;    fin >> R >> C >> c >> r;    king_r = r; king_c = c - 'A' + 1;    while(fin >> c >> r) {        knight_r.push_back(r);        knight_c.push_back(c - 'A' + 1);    }}bool valid(int _r, int _c) {    if(_r < 1 || _r > R || _c < 1 || _c > C) return false;    else return true;}void bfs(int _r, int _c) {    queue<Node> q;    q.push(node(_r, _c, 0));    vis[_r][_c] = 0;    while(!q.empty()) {        Node head = q.front();        q.pop();        for(int i = 0; i < 8; i++) {            int nr = head.r + dx[i][0];            int nc = head.c + dx[i][1];            if(!valid(nr, nc) || vis[nr][nc] != -1) continue;            vis[nr][nc] = head.depth + 1;            q.push(node(nr, nc, head.depth + 1));        }    }}void solve() {    int knight_num = knight_c.size();    for(int i = 0; i < knight_num; i++) {        memset(vis, -1, sizeof(vis));        bfs(knight_r[i], knight_c[i]);        knight_dis.push_back(dis(vis));    }    for(int i = 0; i < 25; i++) {        memset(vis, -1, sizeof(vis));        int nr = king_r + bias[i][0], nc = king_c + bias[i][1];        if(!valid(nr, nc)) continue;        bfs(nr, nc);        pick_dis.push_back(dis(vis));        pick_r.push_back(bias[i][0]);        pick_c.push_back(bias[i][1]);    }    for(int i = 1; i <= R; i++) {        for(int j = 1; j <= C; j++) {            int _ans = 0, _anscopy;            if(knight_num == 0) {                _ans = max(abs(R - king_r), abs(C - king_c));                if(_ans < ans) ans = _ans;                continue;            }            for(int k = 0; k < knight_num; k++) {                _ans += knight_dis[k].d[i][j];            }            _anscopy = _ans;            for(int k = 0; k < knight_num; k++) {                _ans -= knight_dis[k].d[i][j];                for(int l = 0; l < pick_r.size(); l++) {                    if(knight_dis[k].d[king_r + pick_r[l]][king_c + pick_c[l]] == -1) continue;                    if(pick_dis[l].d[i][j] == -1) continue;                    _ans += knight_dis[k].d[king_r + pick_r[l]][king_c + pick_c[l]] +                            max(_abs(pick_r[l]), _abs(pick_c[l])) +                            pick_dis[l].d[i][j];                    if(_ans < ans) ans = _ans;                    _ans = _anscopy - knight_dis[k].d[i][j];                }                _ans = _anscopy;            }        }    }    freopen("camelot.out", "w", stdout);    printf("%d\n", ans);}int main() {    init();    solve();    return 0;}


0 0
原创粉丝点击