题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <algorithm> #include <deque> #include <functional> #include <iostream> #include <set> #include <string> #include <unordered_map> #include <vector> int main() { enum Status { S_BEGIN, S_DIR_INSTRUCTION, S_VAL_1, S_VAL_2, S_END_SIGN, S_END, S_ERR } status = S_BEGIN; const std::unordered_map<char, std::function<void(int&, int&)>>& dir_instruct{ {'W', [](auto& x, auto& y) { y += 1; }}, {'A', [](auto& x, auto& y) { x -= 1; }}, {'S', [](auto& x, auto& y) { y -= 1; }}, {'D', [](auto& x, auto& y) { x += 1; }}}; int val = 0; int x = 0, y = 0; std::function<void(int&, int&)> instruct_func; std::string input_buf; std::getline(std::cin, input_buf); for (auto input_c : input_buf) { switch (status) { case S_BEGIN: { status = S_DIR_INSTRUCTION; } case S_DIR_INSTRUCTION: { if (dir_instruct.find(input_c) != dir_instruct.end()) { instruct_func = dir_instruct.at(input_c); status = S_VAL_1; } else { status = S_ERR; } } break; case S_VAL_1: { if (input_c <= '9' && input_c >= '0') { val = (input_c - '0') + val * 10; status = S_VAL_2; } else { status = S_ERR; } break; } case S_VAL_2: { if (input_c == ';') { status = S_END; break; } if (input_c <= '9' && input_c >= '0') { val = (input_c - '0') + val * 10; status = S_END_SIGN; } else { status = S_ERR; } break; } case S_END_SIGN: { if (input_c == ';') { status = S_END; } else { status = S_ERR; } } break; default: break; } if (status == S_ERR) { if (input_c != ';') { status = S_END_SIGN; }else { status = S_END; } val = 0; instruct_func = nullptr; } if (status == S_END) { for (int i = 0; i < val; i++) { instruct_func(x, y); } val = 0; instruct_func = nullptr; status = S_BEGIN; } } std::cout << x << "," << y; }