40 分求救
查看原帖
40 分求救
1560058
Albert_Peng楼主2025/6/25 13:44

代码:

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

void optimise () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

void input_data (int &d, int &k, int &m, int &n, int &x, int &y, string temp, bool tab[1009][1009]) {
    cin >> n >> m >> k >> x >> y >> d;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        for (int j = 0; j < m; j++)
            if (temp[j] == '.')
                tab[i][j] = true;
	}
}

void move_robot (int &d, int m, int n, int &x, int &y, bool tab_1[1009][1009], bool tab_2[1009][1009]) {
    int x_ = x;
	int y_ = y;

    if (d == 0)
        y_++;
    else if (d == 1)
        x_++;
    else if (d == 2)
        y_--;
    else
        x_--;
    if (x_ >= 1 and x_ <= n and y_ >= 1 and y_ <= m and tab_1[x_ - 1][y_ - 1])
        x = x_, y = y_;
    else
        d = (d + 1) % 4;

    tab_2[x][y] = true;
}

int count_true (int size_1, int size_2, bool tab[1009][1009], int count) {
    for (int i = 1; i <= size_1; i++)
        for (int j = 1; j <= size_2; j++)
            if (tab[i][j])
                count++;
    return count;
}

bool can_go[1009][1009] = {}, visited[1009][1009] = {};

int main () {
    int cnt = 0, d, k, m, n, t, x, y;
    string input;
    optimise();

    cin >> t;

    while (t--) {
        memset(visited, false, sizeof(visited));
        input_data(d, k, m, n, x, y, input, can_go);
        visited[x][y] = true;
        while (k--)
            move_robot(d, m, n, x, y, can_go, visited);
        cout << count_true(n, m, visited, cnt) << '\n';
    }

    return 0;
}

有大佬能帮忙看一下吗

2025/6/25 13:44
加载中...