結果

問題 No.124 門松列(3)
ユーザー hiyokko2hiyokko2
提出日時 2015-01-12 00:29:41
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,593 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 85,712 KB
実行使用メモリ 814,772 KB
最終ジャッジ日時 2023-09-04 04:45:16
合計ジャッジ時間 6,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


class Pos
{
public:
	//書かれている数字
	int num;
	int x, y, cnt;
	int pre;
	
	Pos(int pnum, int px, int py, int pcnt, int ppre)
	{
		num = pnum;
		x = px;
		y = py;
		cnt = pcnt;
		pre = ppre;
	}
	
};

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


int main()
{
	int W, H, M[100][100];
	
	cin >> W >> H;
	rep(i, H) rep(j, W) cin >> M[i][j];
	bool flag = false;
	
	queue<Pos> q;
	q.push(Pos(M[0][0], 0, 0, 0, 0));
	q.push(Pos(M[0][0], 0, 0, 0, 10));
	while (!q.empty())
	{
		Pos now = q.front();
		q.pop();
		if (now.x == W - 1 && now.y == H - 1)
		{
			cout << now.cnt << endl;
			flag = true;
			break;
		}
		for (int i=0; i<4; i++)
		{
			int nx = now.x + dx[i];
			int ny = now.y + dy[i];
			if (0<=nx && nx<W && 0<=ny && ny<H && M[ny][nx] != now.pre)
			{
				if (now.pre < now.num)
				{
					if (M[ny][nx] < now.num)
					{
						Pos next = Pos(M[ny][nx], nx, ny, now.cnt + 1, now.num);
						q.push(next);
					}
				} else {
					if (M[ny][nx] > now.num)
					{
						Pos next = Pos(M[ny][nx], nx, ny, now.cnt + 1, now.num);
						q.push(next);
					}
				}
			}
		}
	}
	
	if (!flag)
	{
		cout << -1 << endl;
	}
	return 0;
}
0