求助!22分
查看原帖
求助!22分
576403
LuckySu楼主2023/9/6 23:37

对照了一个特征值的题解,感觉除了用了个bool数组替代特征值,其他都一样,只有通过2个用例

#include <bits/stdc++.h>

#define SIZE    (10)

#define UP      (0)
#define RIGHT   (1)
#define DOWN    (2)
#define LEFT    (3)

inline void write(int x) {
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int steps[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
int board[SIZE][SIZE] = {0};
bool history[SIZE][SIZE][4][2] = {0};

int f_x, f_y, c_x, c_y;
int f_d = UP, c_d = UP;
int step, x, y;

int main(int argc, char const *argv[]) {
    for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) {
        char ch = getchar();
        if (ch == '\n') { j--; continue; }
        board[i][j] = ch;
        if (board[i][j] == 'C') { c_x = i; c_y = j; board[i][j] = '.';}
        else if (board[i][j] == 'F') { f_x = i; f_y = j; board[i][j] = '.'; }
    }
    step = 1;
    while (true) {
        // cow: move / turn
        x = c_x + steps[c_d][0]; y = c_y + steps[c_d][1];
        if (x < 0 || x >= 10 || y < 0 || y >= 10 || board[x][y] == '*') c_d = (c_d + 1) % 4;
        else { c_x = x; c_y = y; }
        // farmer: move / turn
        x = f_x + steps[f_d][0]; y = f_y + steps[f_d][1];
        if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y] == '*') f_d = (f_d + 1) % 4;
        else { f_x = x; f_y = y; }
        // meet
        if (c_x == f_x && c_y == f_y) { write(step); break; }
        // failed
        if (history[c_x][c_y][c_d][0] && history[f_x][f_y][f_d][1]) { write(0); break; }
        // record
        history[c_x][c_y][c_d][0] = true;
        history[f_x][f_y][f_d][1] = true;
        step++;
    }
    return 0;
}

2023/9/6 23:37
加载中...