結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  msm1993 | 
| 提出日時 | 2020-06-02 09:45:01 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 945 bytes | 
| コンパイル時間 | 643 ms | 
| コンパイル使用メモリ | 76,232 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-11-23 18:21:37 | 
| 合計ジャッジ時間 | 1,732 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
コンパイルメッセージ
main.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   11 | main()
      | ^~~~
            
            ソースコード
#include<iostream>
#include<queue>
using namespace std;
int W,H;
int dis[100][100][10],M[100][100];
bool ok(int a,int b,int c)
{
	return a!=b&&b!=c&&c!=a&&(b>a&&b>c||b<a&&b<c);
}
int d[4]={1,0,-1};
main()
{
	cin>>W>>H;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)
	{
		cin>>M[i][j];
		for(int I=0;I<10;I++)
		{
			dis[i][j][I]=114514;
		}
	}
	dis[0][0][0]=0;
	queue<pair<pair<int,int>,int> >P;
	P.push(make_pair(make_pair(0,0),0));
	while(!P.empty())
	{
		int x=P.front().first.first,y=P.front().first.second;
		int a=P.front().second;
		if(x==H-1&&y==W-1)
		{
			cout<<dis[x][y][a]<<endl;
			return 0;
		}
		P.pop();
		int b=M[x][y];
		for(int r=0;r<4;r++)
		{
			int tx=x+d[r],ty=y+d[r^1];
			if(tx<0||ty<0||tx>=H||ty>=W)continue;
			int c=M[tx][ty];
			if(a==0||ok(a,b,c))
			{
				int nxt=dis[x][y][a]+1;
				if(nxt<dis[tx][ty][b])
				{
					dis[tx][ty][b]=nxt;
					P.push(make_pair(make_pair(tx,ty),b));
				}
			}
		}
	}
	cout<<-1<<endl;
}
            
            
            
        