求助
  • 板块P1189 SEARCH
  • 楼主LordLaffey
  • 当前回复0
  • 已保存回复0
  • 发布时间2020/10/15 19:22
  • 上次更新2023/11/5 10:42:57
查看原帖
求助
335136
LordLaffey楼主2020/10/15 19:22

二十分

#include<iostream>
#include<cstdio>
#define ttt ((s[ndeep][0]=='N'||s[ndeep][0]=='S')?n:m)
using namespace std;
int n,m,q;
int endp[100][100];
char map[100][100];
string s[1010];

void dfs(int x,int y,int ndeep){

    if(ndeep==q)
    {
        endp[x][y]=1;
        return ;
    }

    for(int i=1;i<=ttt;i++)
    {
        int nx=x,ny=y;
        char c=s[ndeep][0];

        if(c=='N')      nx-=i;
        else if(c=='S') nx+=i;
        else if(c=='W') ny-=i;
        else if(c=='E') ny+=i;
        
        if(((nx<=0)&&(c=='N'))||((nx>n)&&(c=='S'))||((ny<=0)&&(c=='W'))||((ny>m)&&(c=='E')))
        return ;
        if(map[nx][ny]=='X')
        return ;
        if(endp[nx][ny]==1)
        continue;
        if(nx<=0||nx>n||ny<=0||ny>n)
        continue;

        dfs(nx,ny,ndeep+1);

    }

}

int main(){

    int sx,sy;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='*')
            sx=i,sy=j;
        }
    }

    scanf("%d",&q);
    for(int i=0;i<q;i++)
    cin>>s[i];

    dfs(sx,sy,0);

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(endp[i][j]==1)
            printf("*");
            else if(map[i][j]=='*')
            {
                if(endp[i][j]==1)
                cout<<"*";
                else
                cout<<".";
            }
            else
            cout<<map[i][j];
        }
        cout<<endl;
    }

    return 0;
    
}

感谢大佬

2020/10/15 19:22
加载中...