参考程序(非std)

Star 2022-10-17 22:09:14

由于不明原因 std 在 HYOI 里过不了,所以代码是我的。(高达 5.4K

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 210;
const int M = 40;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int a[N][N]; // 1障碍 2靶子 3水池
int st[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("data", "r", stdin);
#endif // !ONLINE_JUDGE
    int T;
    scanf("%d", &T);
    while (T--) {
        int n, m;
        memset(a, 0, sizeof(a));
        memset(st, -1, sizeof(st));
        int top = 0;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                scanf("%d", &a[i][j]);
                if (a[i][j] == 2) a[i][j] = 4;
            }
        }
        int head1 = 0, head2 = 0, h = 0;
        bool err = false, stop = false;
        int x, y, mx, b[2], k;
        scanf("%d%d%d%d%d%d", &x, &y, &mx, &b[1], &b[0], &k);
        char c = getchar(); // 把后面的换行吃了,否则字符串读入异常
        while (c != '\n') c = getchar();
        while (k--) {
            char s[100];
            cin.getline(s, 100);
            if (stop) continue;
            // puts(s);
            if (s[0] == 'F' && s[1] == 'T') { // 炮台旋转
                for (int i = 0; i < strlen(s); ++i) { // 小数点给爷死
                    if (s[i] == '.') {
                        err = true;
                        stop = true;
                        break;
                    }
                }
                if (stop) continue;
                int i = s[3] - 48;
                // printf("FT %d\n", i);
                if (i != 0 && i != 1) {
                    err = true;
                    stop = true;
                    continue;
                }
                head2 += i ? -1 : 1;
                (head2 += 4) %= 4;
            }
            else if (s[0] == 'F' && s[1] == 'F') { // 填弹
                int i = s[3] - 48;
                // printf("FF %d\n", i);
                if (top == mx || (i != 1 && i != 0)) { // 弹夹已满继续填弹
                    err = true;
                    stop = true;
                    continue;
                }
                else if (!b[i]) continue;
                else st[++top] = i, b[i]--;
            }
            else if (s[0] == 'F' && s[1] == 'E') { // 发射
                // printf("FE\n");
                if (!top) continue;
                for (int i = 0; i < strlen(s); ++i) {
                    if (isdigit(s[i])) {
                        err = true;
                        stop = true;
                        break;
                    }
                }
                if (stop) continue;
                int i = x, j = y;
                while (i >= 0 && i < n && j >= 0 && j < m) {
                    i += dx[head2], j += dy[head2];
                    if (a[i][j] == 1) break;
                    else if (a[i][j] == 2) {
                        a[i][j] = 0;
                        h++;
                        break;
                    }
                    else if (a[i][j] == 4) {
                        if (st[top] == 1) a[i][j] = 0, h++;
                        else if (st[top] == 0) a[i][j] = 2;
                        break;
                    }
                }
                top--;
            }
            else if (s[0] == 'W' && s[1] == 'T') { // 机器人转动
                for (int i = 0; i < strlen(s); ++i) {
                    if (s[i] == '.') {
                        err = true;
                        stop = true;
                        break;
                    }
                }
                if (stop) continue;
                int i = s[3] - 48;
                // printf("WT %d\n", i);
                if (i != 0 && i != 1) {
                    err = true;
                    stop = true;
                    continue;
                }
                head1 += i ? -1 : 1;
                (head1 += 4) %= 4;
            }
            else if (s[0] == 'W' && s[1] == 'G') { // 机器人前进
                for (int i = 0; i < strlen(s); ++i) {
                    if (s[i] == '.') {
                        err = true;
                        stop = true;
                        break;
                    }
                }
                if (stop) continue;
                int t = 0;
                for (int i = 3; isdigit(s[i]); ++i) t = t * 10 + s[i] - 48;
                // printf("WG %d\n", t);
                int tx = x, ty = y;
                while (t--) {
                    x += dx[head1], y += dy[head1];
                    if (a[x][y] != 0) { // 乱撞
                        stop = true;
                        err = true;
                        x = tx, y = ty;
                        break;
                    }
                    else if (x < 0 || x >= n || y < 0 || y >= m) { // 出界
                        stop = true;
                        err = true;
                        x = tx, y = ty;
                        break;
                    }
                }
            }
            else if (s[0] == 'E') {
                // printf("END\n");
                stop = true;
                continue;
            }
            else {
                // puts(s);
                printf("Wrong Input\n");
            }
            // printf("%d %d %d\n", x, y, err);
        }
        if (stop && !err) printf("Complete\n");
        else printf("ERROR\n");
        printf("%d %d\n%d\n%d %d %d %d\n", x, y, h, head2, head1, b[1], b[0]);
    }
    return 0;
}

共 1 条回复

xin_fu

有人要std请私聊我