SOJ.Robot

Robot

题目描述

PY喜欢玩机器人,他最近做了一台性能卓越、超乎想象的机器人,只可惜他有一个问题没有解决——这个机器人不会动。他想要让机器人动起来,所以他先要写一个控制机器人的程序。你作为PY的专属IT民工,很荣幸的接到了这个任务。PY给了你一个类Robot,请你把它实现。

类的定义如下:

struct Position {
         int x;
         int y;
};

class Robot {
public:
         Robot();                           /* default constructor, initialize at (0,0) */
         Robot(Position pos);     /* initialize at pos */
         void Move(char Dir);     /* Dir could be 'N', 'E', 'S', 'W', for other characters, the robot don’t move */
         Position GetPosition();        /* return current position */
private:
         Position currentPos;
};

 

注意本题中,'N','E','S','W'分别代表向上,右,下,左。

样例输出
0 00 11 11 20 20 1
提示

只需要提交类的定义和实现不需要提交main函数

#include <iostream>
#include <map>

using namespace std;

struct Position {
    int x;
    int y;
};

// your code will be here

int main() {
    Position c;
    c.x = 0;
    c.y = 1;
    Robot a;
    cout << a.GetPosition().x << ' ' << a.GetPosition().y << endl;
    Robot b( c );
    cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
    b.Move('E');
    cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
    b.Move('N');
    cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
    b.Move('W');
    cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
    b.Move('S');
    cout << b.GetPosition().x << ' ' << b.GetPosition().y << endl;
    return 0;
}




class Robot {
public:
         Robot();                           /* default constructor, initialize at (0,0) */
         Robot(Position pos);     /* initialize at pos */
         void Move(char Dir);     /* Dir could be 'N', 'E', 'S', 'W', for other characters, the robot don’t move */
         Position GetPosition();        /* return current position */
private:
         Position currentPos;
};
Robot::Robot()
{
    currentPos.x=0;
    currentPos.y=0;
}
Robot::Robot(Position pos)
{
    currentPos.x=pos.x;
    currentPos.y=pos.y;
}
void Robot::Move(char Dir)
{
    if(Dir=='N')
        currentPos.y++;
    else if(Dir=='E')
        currentPos.x++;
    else if(Dir=='S')
        currentPos.y--;
    else if(Dir=='W')
        currentPos.x--;
}
Position Robot::GetPosition()
{
    return currentPos;
}                  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值