题解 UVA118 【Mutant Flatworld Explorers】

很有趣的一道题,建议先自己写写

具体思路:模拟

只要模拟每次机器人的坐标就好了

难点

  1. 如何表示方向?

    只要将北东南西依此赋值0,1,2,3

    左转该值-1,右转该值+1

  2. 如何行进?

    每个方向坐标变化都是有规律的,不难发现

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


1. 如何判断是否掉出世界?

   每次走之前先试探一下就好了。
   
## 难点就这些,上代码

```c
#include<bits/stdc++.h>
using namespace std;
int maxx,maxy;
bool warn[105][105];
int face;
char ord[555];
int x,y; 
int dx[]={0,1,0,-1},
    dy[]={1,0,-1,0};
bool check(int a,int b)
{
    if(a<0||a>maxx||b<0||b>maxy)	return false;
    return true;
}	
int main()
{
    cin>>maxx>>maxy;
    while(cin>>x>>y)
    {
        bool flag=0;//标记是否掉出世界 
        memset(ord,0,sizeof(ord));
        char a;
        cin>>a;
        if(a=='N') face=0;
        if(a=='E') face=1;
        if(a=='S') face=2;
        if(a=='W') face=3;
        scanf("%s",&ord);
        for(int i=0;i<strlen(ord);i++)
        {
            if(ord[i]=='L') face=(face+4-1)%4;
            if(ord[i]=='R') face=(face+4+1)%4;
            if(ord[i]=='F')
            {
                int tx=dx[face]+x,ty=dy[face]+y;
                if(check(tx,ty)) 
                {
                    x=tx;
                    y=ty;
                    
                }
                else
                {
                    if(warn[x][y]) continue;
                    warn[x][y]=1;
                    flag=1;
                    break; 
                }
            }
        }
        cout<<x<<" "<<y<<" ";
        if(face==0) cout<<"N";
        if(face==1) cout<<"E";
        if(face==2) cout<<"S";
        if(face==3) cout<<"W";
        if(flag)    cout<<" LOST";
        cout<<endl;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/lizinuo/p/10543877.html