题解 UVA118 【Mutant Flatworld Explorers】

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

具体思路:模拟

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

难点

  1. 如何表示方向?

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

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

  2. 如何行进?

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

    int dx[]={0,1,0,-1},
     dy[]={1,0,-1,0};
  3. 如何判断是否掉出世界?

    每次走之前先试探一下就好了。

难点就这些,上代码

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值