1 条题解

  • 0
    @ 2023-1-12 16:00:41
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int main(){
        int m,n;
        while(cin>>m>>n)
        {
            int i,j;
            char direction[]={'n','w','s','e'};
            int d=0;                    //记录初始方向
            char map[30][30];
            int x=0,y=0;    //记录初始位置
            fill(map[0],map[0]+30*30,'*');
            if(m==0&&n==0)break;
            string command;
            cin>>command;
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    cin>>map[i][j];       //记录地图信息到数组中同时找到初始位置
                    if(map[i][j]=='S')
                    {
                        x=i;y=j;
                    }
                }
            }
            //开始执行指令
            int l=command.length();
            for(int turn=0;turn<l;turn++)
            {
    
                if(command[turn]=='F')
                {
                    if(direction[d]=='n'&&map[x-1][y]!='*')
                    {
                        x--;
                    }
                    else if(direction[d]=='s'&&map[x+1][y]!='*')
                    {
                        x++;
                    }
                    else if(direction[d]=='w'&&map[x][y-1]!='*')
                    {
                        y--;
                    }
                    else if(direction[d]=='e'&&map[x][y+1]!='*')
                    {
                        y++;
                    }
                }
    
                else if(command[turn]=='B')
                {
                    if(direction[d]=='n'&&map[x+1][y]!='*')
                    {
                        x++;
                    }
                    else if(direction[d]=='s'&&map[x-1][y]!='*')
                    {
                        x--;
                    }
                    else if(direction[d]=='w'&&map[x][y+1]!='*')
                    {
                        y++;
                    }
                    else if(direction[d]=='e'&&map[x][y-1]!='*')
                    {
                        y--;
                    }
                }
                else if(command[turn]=='L'){
                    d=(d+1)%4;
                }
                else if(command[turn]=='R'){
                    d=(d+3)%4;
                }
            }
    
            //输出最终结果
            cout<<x<<" "<<y<<" "<<direction[d]<<endl;
    
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    1330
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    13
    已通过
    7
    上传者