40pts求助

P11228 [CSP-J 2024] 地图探险

jiuyehuaer @ 2024-10-31 21:38:21

#include<bits/stdc++.h>
using namespace std;
int n, m, d, sx, sy, k;
char ma[10001][10001];
int cnt = 1, t, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
    cin >> t;
    while (t--) {
        cnt = 1;
        cin >> n >> m >> k;
        cin >> sx >> sy >> d;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++)cin >> ma[i][j];
        }
        while (k) {
            int nx = sx + dx[d];
            int ny = sy + dy[d];
            k--;
            if (nx <= 0 || ny <= 0 || nx > n || ny > m || ma[nx][ny] == 'x') {
                d=(d+1)%4;
            } else {
                sx=nx;
                sy=ny;
                cnt++;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

by sunpeilun @ 2024-10-31 21:57:41

重复走过的点不被再次计入,开一个vis数组,把每个点有没有走过做标记,没走过的才累加


by jiuyehuaer @ 2024-10-31 22:05:54

@sunpeilun 谢谢牛犇


by jiuyehuaer @ 2024-10-31 22:07:36

@sunpeilun 为什么0pts了


by jiuyehuaer @ 2024-10-31 22:07:52

#include<bits/stdc++.h>
using namespace std;
int n, m, d, sx, sy, k;
char ma[10001][10001];
bool vis[10001][10001];
int cnt = 1, t, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
    cin >> t;
    while (t--) {
        cnt = 1;
        cin >> n >> m >> k;
        cin >> sx >> sy >> d;
        vis[sx][sy]=1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++)cin >> ma[i][j];
        }
        while (k) {
            int nx = sx + dx[d];
            int ny = sy + dy[d];
            k--;
            if (nx <= 0 || ny <= 0 || nx > n || ny > m || ma[nx][ny] == 'x') {
                d=(d+1)%4;
            } else {
                sx=nx;
                sy=ny;
                if(vis[nx][ny]=0)cnt++;
                vis[nx][ny]=1;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

by sunpeilun @ 2024-10-31 22:17:45

vis[nx][ny]=0
是两个等号判断相等


by sunpeilun @ 2024-10-31 22:22:20

@jiuyehuaer


by jiuyehuaer @ 2024-10-31 22:25:19

@sunpeilun 还是40pts啊


by sunpeilun @ 2024-11-01 12:25:19

@jiuyehuaer 要对vis数组初始化


by sunpeilun @ 2024-11-01 12:36:23


#include<bits/stdc++.h>
using namespace std;
int n, m, d, sx, sy, k;
char ma[10001][10001];
bool vis[10001][10001];
int cnt = 1, t, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
    cin >> t;
    while (t--) {
        cnt = 1;
        cin >> n >> m >> k;
        cin >> sx >> sy >> d;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++){
                cin >> ma[i][j];
                vis[i][j]=0;
            }
        }
        vis[sx][sy]=1;
        while (k) {
            int nx = sx + dx[d];
            int ny = sy + dy[d];
            k--;
            if (nx <= 0 || ny <= 0 || nx > n || ny > m || ma[nx][ny] == 'x') {
                d=(d+1)%4;
            } else {
                sx=nx;
                sy=ny;
                if(vis[nx][ny]==0)cnt++;
                vis[nx][ny]=1;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

by jiuyehuaer @ 2024-11-01 18:51:07

@sunpeilun 谢谢你


| 下一页