#include "bits/stdc++.h"
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution0708 {
public:
void solve()
{
int height = 3;
int width = 4;
string operations = "MRMOMDMLMUMCRMODMC";
vector<int> distances = {1, 1, 2, 1, 2, 2, 1, 2};
vector<char> opts(operations.begin(), operations.end());
cout << opts.size() << opts[0] << opts[opts.size() - 1] << endl;
cout << getNumOfCubes(height, width, opts, distances) << endl;
}
int getNumOfCubes2(int height, int width, vector<char> &opts, vector<int> &dist)
{
unordered_set<tuple<int, int, int, int>> conns;
unordered_map<char, vector<int>> direct{{'U', {-1, 0}}, {'D', {1, 0}}, {'L', {0, -1}}, {'R', {0, 1}}};
bool open = false;
char head = 'D';
int row = 0, col = 0;
int index = 0;
for (char opt : opts) {
if (opt == 'O') {
open = true;
} else if (opt == 'C') {
open = false;
} else if (direct.count(opt) > 0) {
head = opt;
} else {
int dis = dist[index++];
for (int k = 0; k < dis; k++) {
int startRow = row, startCol = col;
int endRow = row + direct[head][0];
int endCol = col + direct[head][1];
conns.emplace(make_tuple(startRow, startCol, endRow, endCol));
}
}
}
int ret = 0;
for (int i = 0; i < height + 1; i++) {
for (int j = 0; j < width + 1; j++) {
if (test(height + 1, w + 1, make_tuple(i, j))) {
ret += 1;
}
}
}
}
bool test(int h, int w, tuple<int, int> pt, unordered_set<tuple<int, int, int, int>> &conns)
{
int row, col;
tie(row, col) = pt;
int check = 0;
//
int row2 = row, col2 = col + 1;
if (row == 0 || conns(make_tuple(row, col, row2, col2))) {
check++;
}
row2 = row + 1, col2 = col;
if (col == 0 || conns(make_tuple(row, col, row2, col2))) {
check++;
}
row2 = row, col2 = col + 1;
if (row == 0 || conns(make_tuple(row, col, row2, col2))) {
check++;
}
row2 = row, col2 = col + 1;
if (row == 0 || conns(make_tuple(row, col, row2, col2))) {
check++;
}
}
int getNumOfCubes(int height, int width, vector<char> &opts, vector<int> &dist)
{
height += 1;
width += 1;
vector<vector<int>> arr(height, vector<int>(width));
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || j == 0 || i == height - 1 || j == width - 1) {
arr[i][j] = 1;
} else {
arr[i][j] = 0;
}
}
}
unordered_map<char, vector<int>> direct{{'U', {-1, 0}}, {'D', {1, 0}}, {'L', {0, -1}}, {'R', {0, 1}}};
int i = 0, j = 0;
int index = 0;
bool open = false;
vector<int> head = direct['D'];
for (char opt : opts) {
if (opt == 'O') {
open = true;
} else if (opt == 'C') {
open = false;
} else if (direct.count(opt) > 0) {
head = direct[opt];
} else if (opt == 'M') {
int dis = dist[index++];
for (int k = 0; k < dis; k++) {
int row = i, col = j;
i += head[0];
j += head[1];
if (open) {
arr[i][j] = 1;
arr[row][col] = 1;
}
}
}
}
//
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr[i].size(); j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//
vector<pair<int, int>> testPos{{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int cnt = 0;
for (int i = 0; i < height - 1; i++) {
for (int j = 0; j < width - 1; j++) {
bool flag = true;
for (auto &iter : testPos) {
int row = i + iter.first;
int col = j + iter.second;
if (row < 0 || col < 0 || row >= height || col >= width) {
continue;
}
if (arr[row][col] == 0) {
flag = false;
break;
}
}
if (flag) {
cnt++;
}
}
}
return cnt;
}
};
class Solution {
public:
int GetUnitBlockNum(int height, int width, string operations, const vector<int>& distances)
{
unordered_map<int, unordered_map<int, bool>> adjList;
int x(0), y(0), dirx(1), diry(0), operIdx(0);
char dir('D'), knifeStatus('C');
unordered_map<char, vector<int>> dirDict;
dirDict['D'] = {1, 0};
dirDict['U'] = {-1, 0};
dirDict['L'] = {0, -1};
dirDict['R'] = {0, 1};
for(auto& op : operations) {
if(op == 'M') {
int step = distances[operIdx];
++operIdx;
if(knifeStatus == 'O') {
for(int i = 0; i < step; ++i) {
int coord1 = encode(x, y, width + 1);
x += dirx, y += diry;
int coord2 = encode(x, y, width + 1);
adjList[coord1][coord2] = true;
adjList[coord2][coord1] = true;
}
}
else {
x += dirx * step;
y += diry * step;
}
}
else if(op == 'O' or op == 'C') {
knifeStatus = op;
}
else {
dir = op;
dirx = dirDict[dir][0];
diry = dirDict[dir][1];
}
}
return searchAns(adjList, height, width);
}
int searchAns(
unordered_map<int, unordered_map<int, bool>>& adjList,
int height,
int width) {
int ans(0);
for(int i = 0; i < height; ++i)
for(int j = 0; j < width; ++j) {
bool flag(true);
int ul(encode(i, j, width + 1)), ur(encode(i, j + 1, width + 1));
int ll(encode(i + 1, j, width + 1)), lr(encode(i + 1, j + 1, width + 1));
flag = flag && (adjList[ul][ur] || i == 0);
flag = flag && (adjList[ur][lr] || (j + 1 == width));
flag = flag && (adjList[lr][ll] || (i + 1 == height));
flag = flag && (adjList[ll][ul] || (j == 0));
if(flag) {
++ans;
}
}
return ans;
}
int encode(int x, int y, int width) {
return x * width + y;
}
};