結果

問題 No.124 門松列(3)
ユーザー beetbeet
提出日時 2019-12-17 20:24:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,354 bytes
コンパイル時間 2,548 ms
コンパイル使用メモリ 220,652 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 19:10:07
合計ジャッジ時間 3,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
bool is_kado(T a,T b,T c){
  if(a==b||b==c||c==a) return 0;
  if(a<b&&b>c) return 1;
  if(a>b&&b<c) return 1;
  return 0;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE

signed main(){
  int w,h;
  cin>>w>>h;
  vector<vector<int>> st(h,vector<int>(w));
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      cin>>st[i][j];

  using T = tuple<int, int, int, int>;
  map<T, int> dp;
  queue<T> qu;
  if(st[0][0]!=st[0][1]){
    dp[T(0,0,0,1)]=1;
    qu.emplace(0,0,0,1);
  }
  if(st[0][0]!=st[1][0]){
    dp[T(0,0,1,0)]=1;
    qu.emplace(0,0,1,0);
  }

  int dy[]={0,0,1,-1};
  int dx[]={1,-1,0,0};
  auto in=[&](int y,int x){return 0<=y&&y<h&&0<=x&&x<w;};

  int ans=-1;
  while(!qu.empty()){
    int py,px,cy,cx;
    tie(py,px,cy,cx)=qu.front();qu.pop();
    if(cy==h-1&&cx==w-1){
      ans=dp[T(py,px,cy,cx)];
      break;
    }
    for(int k=0;k<4;k++){
      int ny=cy+dy[k],nx=cx+dx[k];
      if(!in(ny,nx)) continue;
      if(!is_kado(st[py][px],st[cy][cx],st[ny][nx])) continue;
      if(dp.count(T(cy,cx,ny,nx))) continue;
      dp[T(cy,cx,ny,nx)]=dp[T(py,px,cy,cx)]+1;
      qu.emplace(cy,cx,ny,nx);
    }
  }

  cout<<ans<<endl;
  return 0;
}
/*
  verified on 2019/04/16
  https://yukicoder.me/problems/no/124
*/
#endif
0