結果

問題 No.124 門松列(3)
ユーザー 0w10w1
提出日時 2016-12-13 13:15:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,163 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 179,676 KB
実行使用メモリ 7,616 KB
最終ジャッジ日時 2023-08-20 01:49:33
合計ジャッジ時間 2,902 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,376 KB
testcase_01 AC 5 ms
7,488 KB
testcase_02 AC 4 ms
7,392 KB
testcase_03 AC 5 ms
7,604 KB
testcase_04 AC 5 ms
7,412 KB
testcase_05 AC 5 ms
7,344 KB
testcase_06 AC 4 ms
7,488 KB
testcase_07 AC 4 ms
7,288 KB
testcase_08 AC 4 ms
7,304 KB
testcase_09 AC 4 ms
7,300 KB
testcase_10 AC 4 ms
7,384 KB
testcase_11 AC 4 ms
7,344 KB
testcase_12 AC 5 ms
7,292 KB
testcase_13 AC 4 ms
7,332 KB
testcase_14 AC 4 ms
7,560 KB
testcase_15 AC 5 ms
7,312 KB
testcase_16 AC 5 ms
7,372 KB
testcase_17 AC 4 ms
7,436 KB
testcase_18 AC 5 ms
7,356 KB
testcase_19 AC 4 ms
7,360 KB
testcase_20 AC 5 ms
7,352 KB
testcase_21 AC 4 ms
7,616 KB
testcase_22 AC 5 ms
7,368 KB
testcase_23 AC 4 ms
7,332 KB
testcase_24 AC 5 ms
7,376 KB
testcase_25 AC 5 ms
7,340 KB
testcase_26 AC 4 ms
7,300 KB
testcase_27 AC 5 ms
7,368 KB
testcase_28 AC 5 ms
7,600 KB
testcase_29 AC 5 ms
7,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int H, W;
vvi G;

void init(){
  cin >> W >> H;
  G = vvi( H, vi( W ) );
  for( int i = 0; i < H; ++i )
    for( int j = 0; j < W; ++j )
      cin >> G[ i ][ j ];
}

int dp[ 100 + 1 ][ 100 + 1 ][ 9 + 1 ][ 9 + 1 ];

int kado( int a, int b, int c ){
  if( a == 0 )
    return 1;
  if( a == b or b == c or c == a )
    return 0;
  return b == min( { a, b, c } ) or b == max( { a, b, c } );
}

void preprocess(){
  memset( dp, INF, sizeof( dp ) );
  queue< tuple< int, int, int, int > > que;
  dp[ 0 ][ 0 ][ 0 ][ G[ 0 ][ 0 ] ] = 0;
  que.emplace( 0, 0, 0, G[ 0 ][ 0 ] );
  while( not que.empty() ){
    int x, y, a, b; tie( x, y, a, b ) = que.front(); que.pop();
    // cout << x << " " << y << " " << a << " " << b << " " << dp[ x ][ y ][ a ][ b ] << endl;
    const int dx[] = { 0, 1, 0, -1 };
    const int dy[] = { 1, 0, -1, 0 };
    for( int di = 0; di < 4; ++di ){
      int nx = x + dx[ di ];
      int ny = y + dy[ di ];
      if( not ( 0 <= nx and nx < H and 0 <= ny and ny < W ) ) continue;
      if( not kado( a, b, G[ nx ][ ny ] ) ) continue;
      int na = b;
      int nb = G[ nx ][ ny ];
      if( dp[ nx ][ ny ][ na ][ nb ] < INF ) continue;
      dp[ nx ][ ny ][ na ][ nb ] = dp[ x ][ y ][ a ][ b ] + 1;
      que.emplace( nx, ny, na, nb );
    }
  }
}

void solve(){
  int ans = INF;
  for( int i = 0; i <= 9; ++i )
    for( int j = 0; j <= 9; ++j )
      upmin( ans, dp[ H - 1 ][ W - 1 ][ i ][ j ] );
  if( ans == INF )
    cout << -1 << endl;
  else
    cout << ans << endl;
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0