From 8866ee849297eb487a437fcb2e9e703dad54a9a6 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 2 Jul 2022 20:14:42 +0800 Subject: [PATCH] B - Number Box https://atcoder.jp/contests/abc258/submissions/32896593 --- AtCoder/ABC258/B/B.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 AtCoder/ABC258/B/B.cpp diff --git a/AtCoder/ABC258/B/B.cpp b/AtCoder/ABC258/B/B.cpp new file mode 100644 index 00000000..c31e82b3 --- /dev/null +++ b/AtCoder/ABC258/B/B.cpp @@ -0,0 +1,57 @@ +#include +#include + +const int N = 15; + +int n, a[N][N]; +long long ans; + +const int dx[] = {0, 1, 0, -1, 0, 1, 1, -1, -1}, + dy[] = {0, 0, -1, 0, 1, 1, -1, 1, -1}; + +std::vector res; + +void dfs(int x, int y, int k, int cnt, long long sum, std::vector s) { + if (x > n) x = 1; + if (x < 1) x = n; + if (y > n) y = 1; + if (y < 1) y = n; + + if (cnt == n) { + if (sum > ans) { + ans = sum; + res = s; + } + + return; + } + + s.push_back(a[x][y]); + + dfs(x + dx[k], y + dy[k], k, cnt + 1, sum * 10 + a[x][y], s); +} + +int main() { + scanf("%d", &n); + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + scanf("%1d", &a[i][j]); + } + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + for (int k = 1; k <= 8; k++) { + dfs(i, j, k, 0, 0, std::vector()); + } + } + } + + for (const int &x : res) { + printf("%d", x); + } + printf("\n"); + + return 0; +}