From 09e60215e4e174f6b2ce814c4a44797490b3743b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 26 Mar 2022 19:28:20 +0800 Subject: [PATCH] =?UTF-8?q?4318.=20=E6=9C=80=E7=9F=AD=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/12517850/ --- AcWing/4318/4318.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 AcWing/4318/4318.cpp diff --git a/AcWing/4318/4318.cpp b/AcWing/4318/4318.cpp new file mode 100644 index 00000000..97e64ce4 --- /dev/null +++ b/AcWing/4318/4318.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int to[5][2] = {{0, 0}, {0, -1}, {0, 1}, {-1, 0}, {1, 0}}; + +int x, y; +std::string s; +std::map> vis; + +int main() { + std::ios::sync_with_stdio(false); + cin >> s; + vis[x][y] = true; + for (char c : s) { + int xx = 0, yy = 0; + if (c == 'U') { + y--; + yy--; + } else if (c == 'D') { + y++; + yy++; + } else if (c == 'L') { + x--; + xx--; + } else if (c == 'R') { + x++; + xx++; + } + + if (vis[x][y]) { + cout << "NO" << endl; + exit(0); + } + + vis[x][y] = true; + for (int i = 1; i <= 4; i++) { + vis[x + to[i][0] - xx][y + to[i][1] - yy] = true; + } + } + cout << "YES" << endl; + return 0; +}