题解 | #坐标移动#

坐标移动

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;
}

全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务