Дед Мороз решил сделать вам подарок и написал часть к кода к задаче D! Вам осталось только определить класс Robot на месте комментария "Your code here". Класс должен содержать определения нескольких методов:
Вы можете не пользоваться подарком Деда Мороза и написать всё сами. Но даже в этом случае определите класс Robot! Иначе Дед Мороз расстроится.
| Python 3 | C++ |
|---|---|
class Robot:
# Your code here
fin = open('robot.in', 'r')
fout = open('robot.out', 'w')
w, h, n, m = [int(x) for x in fin.readline().split()]
board = [['.'] * w for i in range(h)]
robots = []
for i in range(n):
x, y = [int(x) for x in fin.readline().split()]
robots.append(Robot(x, y))
board[y][x] = '#'
for i in range(m):
command = fin.readline().split()
if command[0] == "left":
robots[int(command[1])].turnLeft(int(command[2]))
elif command[0] == "right":
robots[int(command[1])].turnRight(int(command[2]))
elif command[0] == "forward":
robot_i = int(command[1])
distance = int(command[2])
for step in range(0, distance):
robots[robot_i].goForward(1)
x, y = robots[robot_i].getPos()
board[y][x] = '#'
for row in reversed(board):
print(*row, sep='', file=fout)
fin.close()
fout.close()
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Robot {
// Your code here
};
int main() {
freopen("robot.in", "r", stdin);
freopen("robot.out", "w", stdout);
int w, h, n, m;
cin >> w >> h >> n >> m;
vector<Robot> robots;
vector<string> board(h, string(w, '.'));
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
robots.push_back(Robot(x, y));
board[y][x] = '#';
}
for (int i = 0; i < m; ++i) {
string command;
int robot_i, count;
cin >> command >> robot_i >> count;
if (command == "left") {
robots[robot_i].turnLeft(count);
} else if (command == "right") {
robots[robot_i].turnRight(count);
} else if (command == "forward") {
for (int step = 0; step < count; ++step) {
Robot &r = robots[robot_i];
r.goForward(1);
board[r.y][r.x] = '#';
}
}
}
reverse(board.begin(), board.end());
for (int i = 0; i < board.size(); ++i) {
cout << board[i] << endl;
}
return 0;
}
|