結果

問題 No.1028 闇討ち
ユーザー takayusamuraitakayusamurai
提出日時 2020-04-18 21:50:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,295 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 181,044 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-14 21:16:10
合計ジャッジ時間 8,247 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 10 ms
6,944 KB
testcase_07 AC 2 ms
6,948 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 714 ms
6,944 KB
testcase_10 AC 1,296 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>
#define rep(i,n) for (int i=0; i<(int)(n); i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;


int main() {
  int n; cin >> n;
  int INFTY = 100100100;
  vector<vector<int>> A(n, vector<int>(n));
  rep(i,n) rep(j,n) {
    int a; cin >> a;
    A[i][j] = --a;
  }

  vector<int> dx = {-1,  0,  1, -1, 1, -1, 0, 1};
  vector<int> dy = {-1, -1, -1,  0, 0,  1, 1, 1};
  map<int,int> sp;
  int ans = 0;
  rep(i, n) sp[i] = INFTY;

  int d = INFTY;
  rep(i,n) {
    queue<P> q;
    vector<vector<int>> dist(n, vector<int>(n, INFTY));
    q.push(make_pair(i,0));
    int cnt = 0;
    map<int,int> d;
    dist[i][0] = 0;
    while (cnt != n*n){
      cnt++;
      P p = q.front(); q.pop();
      int x = p.first;
      int y = p.second;
      d[A[x][y]] += dist[x][y];
      rep(j, 8) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (dist[nx][ny] == INFTY) {
            q.push(make_pair(nx,ny));
            dist[nx][ny] = dist[x][y] + 1;
          }
        }
      }
    }
    for (auto m : d) {
      sp[m.first] = min(sp[m.first], m.second);
    }
  }
  for (auto s: sp) {
    ans += s.second;
  }
  cout << ans << endl;
}
0