結果

問題 No.124 門松列(3)
ユーザー 夜
提出日時 2021-02-23 21:40:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,850 bytes
コンパイル時間 2,842 ms
コンパイル使用メモリ 101,692 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 22:36:43
合計ジャッジ時間 2,330 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
bool bo[4][110][110] = { false };
int b1[4] = { 0,0,1,-1 };
int b2[4] = { 1,-1,0,0 };
int m[110][110];
int main() {
	int w, h;
	cin >> w >> h;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cin >> m[i][j];
		}
	}
	queue<tuple<int, int, int, int, int>> que;
	if (m[0][0] < m[0][1]) {
		que.push(make_tuple(1, 0, 1, 0, m[0][0]));
		bo[0][0][1] = true;
	}
	if (m[0][0] > m[0][1]) {
		que.push(make_tuple(1, 0, 1, 1, m[0][0]));
		bo[0][0][1] = true;
	}
	if (m[0][0] < m[1][0]) {
		que.push(make_tuple(1, 1, 0, 0, m[0][0]));
		bo[2][1][0] = true;
	}
	if (m[0][0] > m[1][0]) {
		que.push(make_tuple(1, 1, 0, 1, m[0][0]));
		bo[2][1][0] = true;
	}
	while (!que.empty()) {
		tuple<int, int, int, int, int> t = que.front();
		int a = get<0>(t);
		int x = get<1>(t);
		int y = get<2>(t);
		int z = get<3>(t);
		int mem = get<4>(t);
		que.pop();
		if (x == h - 1 && y == w - 1) {
			cout << a << endl;
			return 0;
		}
		for (int i = 0; i < 4; i++) {
			if (x + b1[i] < 0 || x + b1[i] >= h || y + b2[i] < 0 || y + b2[i] >= w)continue;
			if (z == 0) {
				if (!bo[i][x + b1[i]][y + b2[i]] && m[x][y] > m[x + b1[i]][y + b2[i]] && mem != m[x + b1[i]][y + b2[i]]) {
					que.push(make_tuple(a + 1, x + b1[i], y + b2[i], 1, m[x][y]));
					bo[i][x + b1[i]][y + b2[i]] = true;
				}
			}
			else {
				if (!bo[i][x + b1[i]][y + b2[i]] && m[x][y] < m[x + b1[i]][y + b2[i]] && mem != m[x + b1[i]][y + b2[i]]) {
					que.push(make_tuple(a + 1, x + b1[i], y + b2[i], 0, m[x][y]));
					bo[i][x + b1[i]][y + b2[i]] = true;
				}
			}
		}
	}
	cout << -1 << endl;

}
0