求调,4个re
查看原帖
求调,4个re
1652249
kmszcll2024楼主2025/6/21 15:41
#include <iostream>
using namespace std;

char c[12][12]; // 0-11行/列
int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}}; // 上、右、下、左

int main() {
    // 初始化整个地图为障碍物
    for (int i = 0; i < 12; i++)
        for (int j = 0; j < 12; j++)
            c[i][j] = '*';

    int x1, y1, x2, y2; // C和F的位置
    int w1 = 0, w2 = 0; // 初始方向:0(北)

    // 读入地图(1-10行,1-10列)
    for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            c[i][j] = getchar();
            if (c[i][j] == 'C') {
                x1 = i;
                y1 = j;
                c[i][j] = '.';
            } else if (c[i][j] == 'F') {
                x2 = i;
                y2 = j;
                c[i][j] = '.';
            }
        }
        getchar(); // 吃掉换行符
    }

    // 模拟移动,最多1000000分钟
    for (int ans = 1; ans <= 1000000; ans++) {
        // 移动牛
        int nx = x1 + dir[w1][0];
        int ny = y1 + dir[w1][1];
        if (c[nx][ny] == '*') { // 障碍物,转向
            w1 = (w1 + 1) % 4;
        } else {
            x1 = nx;
            y1 = ny;
        }

        // 移动农夫
        nx = x2 + dir[w2][0];
        ny = y2 + dir[w2][1];
        if (c[nx][ny] == '*') {
            w2 = (w2 + 1) % 4;
        } else {
            x2 = nx;
            y2 = ny;
        }

        // 移动后检查是否相遇
        if (x1 == x2 && y1 == y2) {
            cout << ans << endl;
            return 0;
        }
    }

    cout << 0 << endl;
    return 0;
}
2025/6/21 15:41
加载中...