Why?

P11228 [CSP-J 2024] 地图探险

pengjingfei267 @ 2024-12-09 13:55:37

#include <bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
char ch[1005][1005];

const int dx[] = {0, 1, 0, -1};

const int dy[] = {1, 0, -1, 0};

void solve() {
    int n, m, k, x0, y0, d0;
    memset(vis, 0, sizeof(vis));
    cin >> n >> m >> k >> x0 >> y0 >> d0;
    for (int i = 1; i <= n; i++) {
        char s[1005];
        cin >> s;
        for (int j = 1; j <= m; j++) {
            ch[i][j] = s[j - 1];
        }
    }
    vis[x0][y0] = true;
    for (int i = 1; i <= k; i++) {
        int x1 = x0 + dx[d0], y1 = y0 + dy[d0];
        if (1 <= x1 && x1 <= n &&
                1 <= y1 && y1 <= m &&
                ch[x1][y1] == '.') {
            x0 = y1;
            y0 = y1;
        } else {
            d0 = (d0 + 1) % 4;
        }
    vis[x0][y0] = true;//这条语句该放哪里?
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ans += vis[i][j];
        }
    }
    cout << ans << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

我是不是哪里写错了?如果是,请更正。


by pengjingfei267 @ 2024-12-09 14:00:20

2.0版本:

#include <bits/stdc++.h>
using namespace std;
char ch[1005][1005];

const int dx[] = {0, 1, 0, -1};

const int dy[] = {1, 0, -1, 0};

void solve() {
    int n, m, k, x0, y0, d0;
    cin >> n >> m >> k >> x0 >> y0 >> d0;
    for (int i = 1; i <= n; i++) {
        char s[1005];
        cin >> s;
        for (int j = 1; j <= m; j++) {
            ch[i][j] = s[j - 1];
        }
    }
    int ans=1;
    for (int i = 1; i <= k; i++) {
        int x1 = x0 + dx[d0], y1 = y0 + dy[d0];
        if (1 <= x1 && x1 <= n &&
                1 <= y1 && y1 <= m &&
                ch[x1][y1] == '.') {
            x0 = y1;
            y0 = y1;
            ans++;
        } else {
            d0 = (d0 + 1) % 4;
        }

    }
    cout << ans << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

by pengjingfei267 @ 2024-12-09 14:09:10

版本 分数
v1.0 20pts
v2.0 40pts

by Lsm2013 @ 2025-01-07 18:59:41

求关求关求关求关求关求关求关求关求关求关求关求关求关求关求关求关求关求关求关

#include<bits/stdc++.h>
using namespace std;
char rmap[1010][1010];
int vis[1010][1010],t; 
int main()
{
    cin>>t;
    while(t--)
    {
        int n,m,d,x,y,k,i,j;
        cin>>n>>m>>k>>x>>y>>d;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>rmap[i][j];
                vis[i][j]=0;
            }
        }
        vis[x][y]=1;
        for(i=1;i<=k;i++)
        {
            int x2,y2;
            if(d==0)
            {
                x2=x;
                y2=y+1;
            }
            if(d==1)
            {
                x2=x+1;
                y2=y;
            }
            if(d==2)
            {
                x2=x;
                y2=y-1;
            }
            if(d==3)
            {
                x2=x-1;
                y2=y;
            }
            if(x2>=1&&y2>=1&&x2<=n&&y2<=m&&rmap[x2][y2]=='.')
            {
                vis[x2][y2]=1;
                x=x2;
                y=y2;
            }
            else
            {
                d=(d+1)%4;
            }
        }
        int sum=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(vis[i][j])
                {
                    sum++;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

|