CF1100D Dasha and Chess

时间:2023-03-09 07:05:57
CF1100D Dasha and Chess

题目地址:CF1100D Dasha and Chess

这是我的第一道交互题

思路不难,主要讲讲这条语句:

fflush(stdout);

stdout是标准输出的意思。因为有时候,我们输出到stdout的内容不能及时输出,使因为stdout的缓冲区没有满或者其他原因,fflush(stdout)就是强迫把stdout内容输出并清空stdout。

代码:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1006, K = 666;
pii a, b[K+6];
bool v[N][N];
int c[6];

inline void upd(int dx, int dy) {
    a.x += dx;
    a.y += dy;
    if (v[a.x][a.y]) a.x -= dx;
    printf("%d %d\n", a.x, a.y);
    fflush(stdout);
    int o, p, q;
    scanf("%d %d %d", &o, &p, &q);
    if (o == -1 && p == -1 && q == -1) exit(0);
    v[b[o].x][b[o].y] = 0;
    v[b[o].x=p][b[o].y=q] = 1;
}

int main() {
    cin >> a.x >> a.y;
    for (int i = 1; i <= K; i++) {
        scanf("%d %d", &b[i].x, &b[i].y);
        v[b[i].x][b[i].y] = 1;
    }
    while (a.x > 500) upd(-1, 0);
    while (a.x < 500) upd(1, 0);
    while (a.y > 500) upd(0, -1);
    while (a.y < 500) upd(0, 1);
    for (int i = 1; i <= K; i++) {
        int k = 0;
        if (b[i].x < 500) k |= 2;
        if (b[i].y < 500) k |= 1;
        c[k]++;
    }
    int t = 0, w = c[0];
    for (int i = 1; i < 4; i++)
        if (c[i] < w) w = c[t=i];
    int dx = (t >> 1) ? 1 : -1, dy = (t & 1) ? 1 : -1;
    while (1) upd(dx, dy);
    return 0;
}