結果

問題 No.2328 Build Walls
ユーザー umezoumezo
提出日時 2023-05-28 16:04:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,301 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 209,048 KB
実行使用メモリ 8,932 KB
最終ジャッジ日時 2023-08-27 13:41:10
合計ジャッジ時間 7,673 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,884 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 173 ms
5,680 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int INF=1e9;
int dx[]={0,-1,-1,-1,0,1,1,1};
int dy[]={1,1,0,-1,-1,-1,0,1};

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int h,w;
  cin>>h>>w;
  vector<vector<int>> A(h-2,vector<int>(w));
  rep(i,h-2) rep(j,w) cin>>A[i][j];
  
  int ans=INF;
  rep(i,h-2){
    if(A[i][0]==-1) continue;
    queue<pair<int,int>> que;
    vector<vector<int>> dist(h-2,vector<int>(w,INF));
    dist[i][0]=A[i][0];
    que.push({i,0});
    rep(j,8){
      int nx=i+dx[j],ny=0+dy[j];
      if(nx<0 || ny<0 || nx>=h-2 || ny>=w) continue;
      if(A[nx][ny]==-1) continue;
      dist[nx][ny]=dist[i][0]+A[nx][ny];
      que.push({nx,ny});
    }
    while(que.size()){
      auto p=que.front(); que.pop();
      int x=p.first,y=p.second;
      rep(j,8){
        int nx=x+dx[j],ny=y+dy[j];
        if(nx<0 || ny<0 || nx>=h-2 || ny>=w) continue;
        if(A[nx][ny]==-1) continue;
        if(dist[nx][ny]>dist[x][y]+A[nx][ny]){
          dist[nx][ny]=dist[x][y]+A[nx][ny];
          que.push({nx,ny});
        }
      }
    }
    rep(i,h-2) ans=min(ans,dist[i][w-1]);
  }
  if(ans==INF) cout<<-1<<endl;
  else cout<<ans<<endl;
      
  return 0;
}
0