結果

問題 No.367 ナイトの転身
ユーザー 0w10w1
提出日時 2016-12-10 18:24:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,332 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 187,516 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-05-06 17:36:57
合計ジャッジ時間 3,336 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 38 ms
17,408 KB
testcase_11 AC 47 ms
17,408 KB
testcase_12 AC 10 ms
8,704 KB
testcase_13 AC 10 ms
8,832 KB
testcase_14 AC 15 ms
8,576 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 16 ms
7,936 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 5 ms
6,944 KB
testcase_19 AC 5 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 5 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
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;
const int kdx[] = { 1, 1, -1, -1, 2, 2, -2, -2 };
const int kdy[] = { 2, -2, 2, -2, 1, -1, 1, -1 };
const int bdx[] = { 1, 1, -1, -1 };
const int bdy[] = { -1, 1, -1, 1 }; 

int H, W;
vs G;

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

int sx, sy;

void preprocess(){
  for( int i = 0; i < H; ++i )
    for( int j = 0; j < W; ++j )
      if( G[ i ][ j ] == 'S' )
        sx = i, sy = j;
}

int in_range( int x, int y ){
  return 0 <= x and x < H and 0 <= y and y < W;
}

void solve(){
  vvvi dis = vvvi( H, vvi( W, vi( 2, INF ) ) );
  queue< tuple< int, int, int, int > > que;
  que.emplace( dis[ sx ][ sy ][ 0 ] = 0, sx, sy, 0 );
  while( not que.empty() ){
    int d, x, y, t; tie( d, x, y, t ) = que.front(); que.pop();
    if( dis[ x ][ y ][ t ] != d ) continue;
    if( G[ x ][ y ] == 'G' )
      cout << d << endl, exit( 0 );
    if( t == 0 ){
      for( int di = 0; di < 8; ++di ){
        int nx = x + kdx[ di ];
        int ny = y + kdy[ di ];
        if( not in_range( nx, ny ) ) continue;
        int nt = t;
        nt ^= G[ nx ][ ny ] == 'R';
        if( upmin( dis[ nx ][ ny ][ nt ], dis[ x ][ y ][ t ] + 1 ) )
          que.emplace( dis[ nx ][ ny ][ nt ], nx, ny, nt );
      }
    } else{
      for( int di = 0; di < 4; ++di ){
        int nx = x + bdx[ di ];
        int ny = y + bdy[ di ];
        if( not in_range( nx, ny ) ) continue;
        int nt = t;
        nt ^= G[ nx ][ ny ] == 'R';
        if( upmin( dis[ nx ][ ny ][ nt ], dis[ x ][ y ][ t ] + 1 ) )
          que.emplace( dis[ nx ][ ny ][ nt ], nx, ny, nt );
      }
    }
  }
  cout << -1 << endl;
}

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