Codeforces-1100D:Dasha and Chess(思维)

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/86483827

D. Dasha and Chess
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
This is an interactive task.

Dasha and NN like playing chess. While playing a match they decided that normal chess isn’t interesting enough for them, so they invented a game described below.

There are 666 black rooks and 1 white king on the chess board of size 999×999. The white king wins if he gets checked by rook, or, in other words, if he moves onto the square which shares either a row or column with a black rook.

The sides take turns, starting with white. NN plays as a white king and on each of his turns he moves a king to one of the squares that are adjacent to his current position either by side or diagonally, or, formally, if the king was on the square (x,y), it can move to the square (nx,ny) if and only max(|nx−x|,|ny−y|)=1 , 1≤nx,ny≤999. NN is also forbidden from moving onto the squares occupied with black rooks, however, he can move onto the same row or column as a black rook.

Dasha, however, neglects playing by the chess rules, and instead of moving rooks normally she moves one of her rooks on any space devoid of other chess pieces. It is also possible that the rook would move onto the same square it was before and the position wouldn’t change. However, she can’t move the rook on the same row or column with the king.

Each player makes 2000 turns, if the white king wasn’t checked by a black rook during those turns, black wins.

NN doesn’t like losing, but thinks the task is too difficult for him, so he asks you to write a program that will always win playing for the white king. Note that Dasha can see your king and play depending on its position.

Input
In the beginning your program will receive 667 lines from input. Each line contains two integers x and y (1≤x,y≤999) — the piece’s coordinates. The first line contains the coordinates of the king and the next 666 contain the coordinates of the rooks. The first coordinate denotes the number of the row where the piece is located, the second denotes the column. It is guaranteed that initially the king isn’t in check and that all pieces occupy different squares.

Output
After getting king checked, you program should terminate immediately without printing anything extra.

Interaction
To make a move with the king, output two integers x and y (1≤x,y≤999) — the square to which the king would be moved. The king cannot move onto the square already occupied by a rook. It is guaranteed that the king would always have a valid move.

After each of your turns read the rook’s turn in the following format: a single line containing three integers k, x and y (1≤k≤666, 1≤xi,yi≤999) — the number of the rook that would move and the square it would move to. It is guaranteed that the rook wouldn’t move to a square already occupied by another chess piece, but it can move onto the square where it was before the turn so that its position wouldn’t change. It is guaranteed that the move does not put your king into a check. If your king got in check, all three integers would be equal to −1 and in that case your program should terminate immediately.

扫描二维码关注公众号,回复: 5102596 查看本文章

After printing your turn do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:

fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see documentation for other languages.
Answer “0 0 0” instead of a correct answer means that you made an invalid query. Exit immediately after receiving “0 0 0” and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

思路:先把king移到中心(500,500),于是king就把棋盘分成了四个区域,那么平均每个区域约有166个rook,我们找到拥有最少rook的那个区域,然后向其反方向走即可。

因为拥有最少rook的区域最多可能有166个rook,那么其它区域总共有500个rook,沿着反方向走的话,499步就可以走完,所以沿着反方向走一定会碰到rook。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10;
const int MOD=1e9+7;
const int INF=1e9+7;
typedef long long ll;
int a[700],b[700];
int v[1000][1000];
int main()
{
    int nx,ny;
    cin>>nx>>ny;
    for(int i=1;i<=666;i++)scanf("%d%d",&a[i],&b[i]);
    for(int i=1;i<=666;i++)v[a[i]][b[i]]=1;
    while(nx<500)
    {
        printf("%d %d\n",++nx,ny);
        fflush(stdout);
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(k==-1&&x==-1&&y==-1)return 0;
        swap(v[a[k]][b[k]],v[x][y]);
        a[k]=x;
        b[k]=y;
    }
    while(nx>500)
    {
        printf("%d %d\n",--nx,ny);
        fflush(stdout);
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(k==-1&&x==-1&&y==-1)return 0;
        swap(v[a[k]][b[k]],v[x][y]);
        a[k]=x;
        b[k]=y;
    }
    while(ny<500)
    {
        printf("%d %d\n",nx,++ny);
        fflush(stdout);
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(k==-1&&x==-1&&y==-1)return 0;
        swap(v[a[k]][b[k]],v[x][y]);
        a[k]=x;
        b[k]=y;
    }
    while(ny>500)
    {
        printf("%d %d\n",nx,--ny);
        fflush(stdout);
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(k==-1&&x==-1&&y==-1)return 0;
        swap(v[a[k]][b[k]],v[x][y]);
        a[k]=x;
        b[k]=y;
    }
    int A=0,B=0,C=0,D=0;
    for(int i=1;i<=999;i++)
    {
        for(int j=1;j<=999;j++)
        {
            if(i<500&&j<500)A+=v[i][j];
            if(i<500&&j>500)B+=v[i][j];
            if(i>500&&j<500)C+=v[i][j];
            if(i>500&&j>500)D+=v[i][j];
        }
    }
    if(A==min(min(A,B),min(C,D)))
    {
        for(int i=1;i<500;i++)
        {
            if(v[nx+1][ny+1]==0)
            {
                printf("%d %d\n",++nx,++ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
                swap(v[a[k]][b[k]],v[x][y]);
                a[k]=x;
                b[k]=y;
            }
            else
            {
                printf("%d %d\n",nx,++ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
            }
        }
    }
    else if(B==min(min(A,B),min(C,D)))
    {
        for(int i=1;i<500;i++)
        {
            if(v[nx+1][ny-1]==0)
            {
                printf("%d %d\n",++nx,--ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
                swap(v[a[k]][b[k]],v[x][y]);
                a[k]=x;
                b[k]=y;
            }
            else
            {
                printf("%d %d\n",nx,--ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
            }
        }
    }
    else if(C==min(min(A,B),min(C,D)))
    {
        for(int i=1;i<500;i++)
        {
            if(v[nx-1][ny+1]==0)
            {
                printf("%d %d\n",--nx,++ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
                swap(v[a[k]][b[k]],v[x][y]);
                a[k]=x;
                b[k]=y;
            }
            else
            {
                printf("%d %d\n",nx,++ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
            }
        }
    }
    else
    {
        for(int i=1;i<500;i++)
        {
            if(v[nx-1][ny-1]==0)
            {
                printf("%d %d\n",--nx,--ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
                swap(v[a[k]][b[k]],v[x][y]);
                a[k]=x;
                b[k]=y;
            }
            else
            {
                printf("%d %d\n",nx,--ny);
                fflush(stdout);
                int k,x,y;
                scanf("%d%d%d",&k,&x,&y);
                if(k==-1&&x==-1&&y==-1)return 0;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/86483827