• USACO Section 3.3 Camlot(BFS)


      BFS.先算出棋盘上每个点到各个点knight需要的步数;然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个结论?还是usaco数据太弱了?不过看跑出来的时间,全部枚举或许也可以))。一开始觉得挺麻烦的,不过只要思路清晰写起来应该也没多大问题。大概就是这样了.

    ---------------------------------------------------------------------------------------

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<cmath>
    #define rep(i,l,r) for(int i=l;i<=r;i++)
    #define dow(i,l,r) for(int i=l;i>=r;i--)
    #define clr(x,c) memset(x,c,sizeof x)
    using namespace std;
    const int inf=0x3f3f3f3f,maxr=30+5,maxc=26+5;
    const int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    int d[maxr][maxc][maxr][maxc];
    int r,c,num=0;
    struct coor { int x,y; };
    queue<coor> q;
    coor king,knight[maxr*maxc];
    void init() {
        cin>>r>>c;
        char p;
        int t;
        cin>>p>>t;
        king={t,p-'A'+1};
        while(cin>>p>>t) knight[++num]={t,p-'A'+1};
        
        clr(d,inf);
        rep(i,1,r) rep(j,1,c) {
            d[i][j][i][j]=0;
            q.push((coor){i,j});
            while(!q.empty()) {
                coor e=q.front(); q.pop();
                rep(k,0,7) {
                    int x=e.x+dir[k][0];
                    int y=e.y+dir[k][1];
                    if(x<=0 || x>r || y<=0 || y>c) continue;
                    if(d[i][j][x][y]==inf) {
                        d[i][j][x][y]=d[i][j][e.x][e.y]+1;
                        q.push((coor){x,y});
                    }
                }
            }
        }
    }
    int s() {
        int ans=inf;
        int i=5,j=2;
        rep(i,1,r) rep(j,1,c) {
            int w=0,t=inf;
            rep(k,1,num) {
                coor e=knight[k];
                w+=d[i][j][e.x][e.y];
            }
            
            rep(a,max(king.x-2,1),min(king.x+2,r))
                rep(b,max(king.y-2,1),min(king.y+2,c)) 
                    rep(k,1,num) {
                        coor e=knight[k];
                        int x=abs(king.x-a);
                        int y=abs(king.y-b);
                        t=min(t,d[i][j][a][b]+d[a][b][e.x][e.y]+x+y-min(x,y)-d[i][j][e.x][e.y]);
                    }
            
            int x=abs(king.x-i);
            int y=abs(king.y-j);
            int h=min(x+y-min(x,y),t)+w;
            ans= h>=0 && h<ans ? h:ans;
        }
        return ans;
    }
    int main() {
        freopen("camelot.in","r",stdin);
        freopen("camelot.out","w",stdout);
        
        init();
        cout<<s()<<endl;
        
        return 0;
    }

    -------------------------------------------------------------------------------------------

    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 8
    D 4
    A 3 A 8
    H 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. 

  • 相关阅读:
    学习的过程必须要知其所以然
    根据人类的学习与记忆过程来高效学习
    大脑的信息获取特点与记忆模式
    31个让你变聪明的有效方法
    心智模式:心智模式的更多资料
    心智模式:仁者见仁、智者见智
    心智模式:如何看待成败?
    心智模式:如何面对逆境?
    心智模式:认识你自己
    阿里巴巴JAVA工程师面试经验
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4293332.html
Copyright © 2020-2023  润新知