結果

問題 No.124 門松列(3)
ユーザー kurenai3110
提出日時 2016-09-20 23:33:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,480 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 68,448 KB
最終ジャッジ日時 2025-03-18 19:37:55
合計ジャッジ時間 1,083 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘void bfs()’:
main.cpp:16:108: error: ‘INT32_MAX’ was not declared in this scope
   16 |         for (int i = 0; i < 101; i++)for (int j = 0; j < 101; j++)for (int k = 0; k < 4; k++)DP[i][j][k] = INT32_MAX;
      |                                                                                                            ^~~~~~~~~
main.cpp:7:1: note: ‘INT32_MAX’ is defined in header ‘<cstdint>’; did you forget to ‘#include <cstdint>’?
    6 | #include <map>
  +++ |+#include <cstdint>
    7 | using namespace std;
main.cpp: In function ‘int main()’:
main.cpp:57:20: error: ‘INT32_MAX’ was not declared in this scope
   57 |         if (ans == INT32_MAX)cout << -1 << endl;
      |                    ^~~~~~~~~
main.cpp:57:20: note: ‘INT32_MAX’ is defined in header ‘<cstdint>’; did you forget to ‘#include <cstdint>’?

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

int W, H;
int DP[101][101][4];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
vector<vector<int> >M;

void bfs() {
	for (int i = 0; i < 101; i++)for (int j = 0; j < 101; j++)for (int k = 0; k < 4; k++)DP[i][j][k] = INT32_MAX;
	DP[0][1][0] = 1;
	DP[1][0][3] = 1;
	queue<pair<int,int> > que;
	que.push(make_pair(W,0));
	que.push(make_pair(1,3));
	while (!que.empty()) {
		pair<int,int> v = que.front(); que.pop();
		int y = v.first / W;
		int x = v.first % W;
		int p_y = y + dy[v.second];
		int p_x = x + dx[v.second];

		for (int i = 0; i < 4; i++) {
			int n_y = y + dy[i];
			int n_x = x + dx[i];
			if (n_x<0 || n_y<0 || n_x>=W || n_y>=H)continue;
			if (DP[n_x][n_y][(i + 2) % 4] <= DP[x][y][v.second] + 1)continue;
			if (M[y][x] == M[p_y][p_x] || M[y][x] == M[n_y][n_x] || M[p_y][p_x]==M[n_y][n_x])continue;
			if((M[p_y][p_x]<M[y][x]&&M[y][x]<M[n_y][n_x])|| (M[p_y][p_x] > M[y][x] && M[y][x] > M[n_y][n_x]))continue;

			DP[n_x][n_y][(i + 2) % 4] = DP[x][y][v.second] + 1;
			que.push(make_pair(W*n_y + n_x, (i + 2) % 4));
		}
	}
}

int main()
{
	cin >> W >> H;
	M.resize(H);
	for (int i = 0; i < H; i++) {
		M[i].resize(W);
		for (int j = 0; j < W; j++) {
			cin >> M[i][j];
		}
	}

	bfs();

	int ans = min(DP[W - 1][H - 1][0], DP[W - 1][H - 1][3]);
	if (ans == INT32_MAX)cout << -1 << endl;
	else cout << ans << endl;

    return 0;
}
0