考试忘记判断边界

P11228 [CSP-J 2024] 地图探险

Theonlybreeze @ 2024-10-27 09:09:04

考试时代码:

#include<bits/stdc++.h>
using namespace std;
int vis[1010][1010];
char a[1010][1010];
string s;
int main(){
    int t;
    scanf("%d", &t);
    while(t --) {
        for(int i = 1; i <= 1010; i ++) {
            for(int j = 1; j <= 1010; j ++) {
                vis[i][j] = 0;
            }
        }
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        int x, y, d;
        scanf("%d%d%d", &x, &y, &d);
        vis[x][y] = 1;
        for(int i = 1; i <= n; i ++) {
            cin >> s;
            for(int j = 0; j < s.size(); j ++) {
                a[i][j + 1] = s[j];
            }
        }
        while(k --) {
            if(d == 0) {
                int nx = x;
                int ny = y + 1;
                if(a[nx][ny] == '.') {
                    vis[nx][ny] = 1;
                    x = nx;
                    y = ny;
                }
                else {
                    d = (d + 1) % 4;
                }
            }
            else if(d == 1) {
                int nx = x + 1;
                int ny = y;
                if(a[nx][ny] == '.') {
                    vis[nx][ny] = 1;
                    x = nx;
                    y = ny;
                }
                else {
                    d = (d + 1) % 4;
                }
            }
            else if(d == 2) {
                int nx = x;
                int ny = y - 1;
                if(a[nx][ny] == '.') {
                    vis[nx][ny] = 1;
                    x = nx;
                    y = ny;
                }
                else {
                    d = (d + 1) % 4;
                }
            }
            else {
                int nx = x - 1;
                int ny = y;
                if(a[nx][ny] == '.') {
                    vis[nx][ny] = 1;
                    x = nx;
                    y = ny;
                }
                else {
                    d = (d + 1) % 4;
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                if(vis[i][j] == 1) {
                    ans ++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

|