結果

問題 No.124 門松列(3)
ユーザー furafura
提出日時 2020-06-08 00:36:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 980 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 203,036 KB
最終ジャッジ日時 2025-01-11 00:01:25
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         int h,w; scanf("%d%d",&w,&h);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:18:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |         rep(i,h) rep(j,w) scanf("%d",&a[i][j]);
      |                           ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

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

bool is_kadomatsu(int a,int b,int c){
	if(a==b || a==c || b==c) return false;
	if(a<b && b>c) return true;
	if(a>b && b<c) return true;
	return false;
}
int main(){
	int h,w; scanf("%d%d",&w,&h);
	int a[100][100];
	rep(i,h) rep(j,w) scanf("%d",&a[i][j]);

	int d[100][100][4];
	rep(i,h) rep(j,w) rep(k,4) d[i][j][k]=-1;
	d[0][1][0]=1;
	d[1][0][3]=1;
	queue<tuple<int,int,int>> Q;
	Q.emplace(0,1,0);
	Q.emplace(1,0,3);
	while(!Q.empty()){
		int y,x,k; tie(y,x,k)=Q.front(); Q.pop();

		if(y==h-1 && x==w-1) return printf("%d\n",d[y][x][k]),0;

		int a0=a[y-dy[k]][x-dx[k]],a1=a[y][x];
		rep(kk,4){
			int yy=y+dy[kk],xx=x+dx[kk];
			if(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx][kk]==-1){
				int a2=a[yy][xx];
				if(is_kadomatsu(a0,a1,a2)){
					d[yy][xx][kk]=d[y][x][k]+1;
					Q.emplace(yy,xx,kk);
				}
			}
		}
	}
	puts("-1");

	return 0;
}
0