赏关

P11228 [CSP-J 2024] 地图探险

qinweijun @ 2024-11-19 18:11:25

#include<iostream>
using namespace std;
int t,n,m,k,x,y,d;
char s[1005][1005];
bool vis[1005][1005];
int ex(){
    int cnt = 1;
    vis[x][y] = 1;
    for(int i=1;i<=k;i++){
        if(d==0){
            if(s[x][y+1]=='x'|| x<1 || y+1<1 || x>n || y+1>m){
                d = (d%4)+1;
                continue;
            }else{
                y++;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
                else y--;
            }
        }else if(d==1){
            if(s[x+1][y]=='x'|| x+1<1 || y<1 || x+1>n || y>m){
                d = (d%4)+1;
                continue;
            }else{
                x++;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
                else x--;
            }
        }else if(d==2){
            if(s[x][y-1]=='x'|| x<1 || y-1<1 || x>n || y-1>m){
                d = (d%4)+1;
                continue;
            }else{
                y--;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
                else y++;
            }
        }else{
            if(s[x-1][y]=='x'|| x-1<1 || y<1 || x-1>n || y>m){
                d = 0;
                continue;
            }else{
                x--;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
                else x++;
            }
        }   
    }
    return cnt;
}
int main(){
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>s[i][j];
                vis[i][j] = 0;
            }
        }
        cout<<ex()<<endl;
    }
    return 0;
}

by wangshengchen @ 2024-11-19 18:30:00

@qinweijun

#include<iostream>
using namespace std;
const int N=1e3+10;
int t,n,m,k,x,y,d,ans,d2[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//d是方向 
string s;
int main(){
    ios::sync_with_stdio(0); 
    cin.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>m>>k>>x>>y>>d;
        char map[N][N]={};
        bool vis[N][N]={};
        for(int i=1;i<=n;i++){
            cin>>s;//用字符串输入快 
            int len=s.size();
            for(int j=0;j<len;j++) map[i][j+1]=s[j];//存到字符数组里 
        }
        ans=1;//起点也有一步
        vis[x][y]=1;//判断走过的(可能有重复的) 
        while(k--){
            int nx=d2[d][0]+x,ny=d2[d][1]+y;//现在的x,y坐标 
            if(nx>0&&nx<n+1&&ny>0&&ny<m+1&&map[nx][ny]!='x'){//判断可不可以走 
                x=nx;
                y=ny;
                if(!vis[nx][ny]){//加个数
                    vis[nx][ny]=1;
                    ans++; 
                }
            }
            else d=(d+1)%4;//转向
        }
        cout<<ans<<"\n";
    }
    return 0;
}

by XiaoHongChong @ 2024-11-19 19:26:51

@qinweijun 这样子试试。

#include<iostream>
using namespace std;
int t,n,m,k,x,y,d;
char s[1005][1005];
bool vis[1005][1005];
int ex(){
    int cnt = 1;
    vis[x][y] = 1;
    for(int i=1;i<=k;i++){
        if(d==0){
            if(s[x][y+1]=='x'|| x<1 || y+1<1 || x>n || y+1>m){
                d = (d % 4) + 1;
                continue;
            }else{
                y++;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
//              else y--;
            }
        }else if(d==1){
            if(s[x+1][y]=='x'|| x+1<1 || y<1 || x+1>n || y>m){
                d = (d % 4) + 1;
                continue;
            }else{
                x++;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
//              else x--;
            }
        }else if(d==2){
            if(s[x][y-1]=='x'|| x<1 || y-1<1 || x>n || y-1>m){
                d = (d % 4) + 1;
                continue;
            }else{
                y--;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
//              else y++;
            }
        }else{
            if(s[x-1][y]=='x'|| x-1<1 || y<1 || x-1>n || y>m){
                d = 0;
                continue;
            }else{
                x--;
                if(vis[x][y]==0) cnt++,vis[x][y]=1;
//              else x++;
            }
        }   
    }
    return cnt;
}
int main(){
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>s[i][j];
                vis[i][j] = 0;
            }
        }
        cout<<ex()<<endl;
    }
    return 0;
}

|