91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
#include <cstring>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
class Coord {
|
|
public:
|
|
int x = 0, y = 0;
|
|
constexpr Coord(int x, int y) : x(x), y(y) {}
|
|
|
|
constexpr Coord operator+(const Coord other) const {
|
|
return {x + other.x, y + other.y};
|
|
}
|
|
|
|
constexpr Coord& operator+=(const Coord other) {
|
|
x += other.x, y += other.y;
|
|
return *this;
|
|
}
|
|
|
|
constexpr bool operator==(const Coord other) const {
|
|
return x == other.x && y == other.y;
|
|
}
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Coord coord) {
|
|
return os << "(" << coord.x << ", " << coord.y << ")";
|
|
}
|
|
};
|
|
|
|
constexpr int MAX_N = 10;
|
|
int w, h, grid[MAX_N][MAX_N];
|
|
std::string original_str;
|
|
|
|
std::vector<int> get_nums(const char c) {
|
|
if (c == ' ') { return {0, 0}; }
|
|
return {(c - 'A' + 1) / 10, (c - 'A' + 1) % 10};
|
|
}
|
|
|
|
Coord next_move(const Coord prev_coord, const Coord prev_facing) {
|
|
Coord new_coord = prev_coord + prev_facing;
|
|
if (new_coord.x < 0 || new_coord.x >= h ||
|
|
new_coord.y < 0 || new_coord.y >= w ||
|
|
grid[new_coord.x][new_coord.y] != -1) {
|
|
if (prev_facing == Coord{1, 0}) { return {0, -1}; }
|
|
if (prev_facing == Coord{0, -1}) { return {-1, 0}; }
|
|
if (prev_facing == Coord{-1, 0}) { return {0, 1}; }
|
|
if (prev_facing == Coord{0, 1}) { return {1, 0}; }
|
|
throw std::invalid_argument("Invalid facing direction");
|
|
}
|
|
return prev_facing;
|
|
}
|
|
|
|
std::ostream& print_grid(std::ostream& os) {
|
|
for (int i = 0; i < h; i++) {
|
|
for (int j = 0; j < w; j++) {
|
|
os << (grid[i][j] == -1 ? 0 : grid[i][j]);
|
|
}
|
|
}
|
|
os << std::endl;
|
|
return os;
|
|
}
|
|
|
|
void fill_grid(const std::string& s) {
|
|
memset(grid, -1, sizeof(grid));
|
|
Coord coord = {0, 0}, facing = {0, 1};
|
|
for (const char c : s) {
|
|
for (const int x : get_nums(c)) {
|
|
grid[coord.x][coord.y] = x;
|
|
std::clog << "Placing " << x << " at " << coord << std::endl;
|
|
print_grid(std::clog);
|
|
coord += facing = next_move(coord, facing);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
int main() {
|
|
std::ios::sync_with_stdio(false);
|
|
std::cin.tie(nullptr);
|
|
|
|
getline(std::cin, original_str);
|
|
std::stringstream ss(original_str);
|
|
ss >> h >> w;
|
|
getline(std::cin, original_str);
|
|
fill_grid(original_str);
|
|
|
|
print_grid(std::cout);
|
|
|
|
|
|
return 0;
|
|
}
|