50分,求助

P11228 [CSP-J 2024] 地图探险

bzz_ZhangHongrui @ 2024-10-26 22:07:12

#include <iostream>
using namespace std;

const int N = 1000 + 10;

int main() {
    int t;
    cin>>t;

    while (t--) {
        int n, m, k;
        int x, y, d;
        cin>>n>>m>>k;
        cin>>x>>y>>d;

        char c[N][N];
        bool st[N][N];
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cin>>c[i][j];
                st[i][j] = false;
            }
        }

        int sum = 1;
        while (k != 0) {
            if (d == 0) {
                if (1 <= x && x <= n && 1 <= y + 1 && y + 1 <= m && c[x][y + 1] == '.') {
                    y++;
                    k--;
                    if (st[x][y] == false) {
                        sum++;
                        st[x][y] = true;
                    }
                } else {
                    d = (d + 1) % 4;
                    k--;
                }
            } else if (d == 1) {
                if (1 <= x + 1 && x + 1 <= n && 1 <= y && y <= m && c[x + 1][y] == '.') {
                    x++;
                    k--;
                    if (st[x][y] == false) {
                        sum++;
                        st[x][y] = true;
                    }
                } else {
                    d = (d + 1) % 4;
                    k--;
                }
            } else if (d == 2) {
                if (1 <= x && x <= n && 1 <= y - 1 && y - 1 <= m && c[x][y - 1] == '.') {
                    y--;
                    k--;
                    if (st[x][y] == false) {
                        sum++;
                        st[x][y] = true;
                    }
                } else {
                    d = (d + 1) % 4;
                    k--;
                }
            } else if (d == 3) {
                if (1 <= x - 1 && x - 1 <= n && 1 <= y && y <= m && c[x - 1][y] == '.') {
                    x--;
                    k--;
                    if (st[x][y] == false) {
                        sum++;
                        st[x][y] = true;
                    }
                } else {
                    d = (d + 1) % 4;
                    k--;
                }
            }
        }
        cout<<sum<<endl;
    }

    return 0;
}

by bzz_ZhangHongrui @ 2024-10-26 22:09:39

样例全过了


|