結果

問題 No.1028 闇討ち
ユーザー iqueue02iqueue02
提出日時 2020-04-17 23:07:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,282 bytes
コンパイル時間 1,861 ms
コンパイル使用メモリ 174,752 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-14 15:24:39
合計ジャッジ時間 6,679 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,756 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 6 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 260 ms
6,944 KB
testcase_10 AC 458 ms
6,940 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr int INF = 2e9;
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int main() {
  int n;
  cin >> n;
  vector<int> dp(n,INF);
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cin >> a[i][j], a[i][j]--;
  }
  
  auto bfs = [&](int sx, int sy){
    queue<P> que;
    que.push(make_pair(sx, sy));
    vector<vector<int>> d(n, vector<int>(n, INF));
    vector<int> tmp(n, 0);
    d[sx][sy] = 0;
    while (!que.empty()) {
      auto p = que.front(); que.pop();
      tmp[a[p.first][p.second]] += d[p.first][p.second];
      for (int i = 0; i < 8; i++) {
        int x = p.first + dx[i], y = p.second + dy[i];
        if (x < 0 || y < 0 || x >= n || y >= n) continue;
        if (d[x][y] != INF) continue;
        d[x][y] = d[p.first][p.second] + 1;
        que.push(make_pair(x, y));
      }
    }

    for (int i = 0; i < n; i++) dp[i] = min(dp[i], tmp[i]);

  };

  for (int i = 0; i < n; i++) {
    bfs(i, 0);
  }

  ll res = 0;

  for (int i = 0; i < n; i++) {
    res += dp[i];
  }
  cout << res << endl;
  return 0;
}
0