結果

問題 No.1028 闇討ち
ユーザー takayusamuraitakayusamurai
提出日時 2020-04-18 23:46:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,344 bytes
コンパイル時間 2,037 ms
コンパイル使用メモリ 180,020 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-14 21:20:35
合計ジャッジ時間 11,676 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
  ll ans = 0;
  rep(i, n) sp[i] = INFTY;

  int d = INFTY;
  vector<vector<int>> dist(n, vector<int>(n, INFTY));
  rep(i,n) {
    queue<P> q;
    q.push(make_pair(i,0));
    map<int,int> d;
    dist[i][0] = 0;
    while (!q.empty()) {
      P p = q.front(); q.pop();
      int x = p.first;
      int y = p.second;
      //cout << x << " " << y << endl;
      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;
          }
        }
      }
    }
    rep(l,n) rep(m,3) dist[l][m] = INFTY;
    for (auto m : d) {
      sp[m.first] = min(sp[m.first], m.second);
    }
  }
  for (auto s: sp) {
    ans += s.second;
  }
  cout << ans << endl;
}
0