結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー pengin_2000pengin_2000
提出日時 2022-07-31 11:49:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 260 ms / 3,000 ms
コード長 1,824 bytes
コンパイル時間 1,247 ms
コンパイル使用メモリ 31,056 KB
実行使用メモリ 6,444 KB
最終ジャッジ日時 2023-09-28 04:03:16
合計ジャッジ時間 6,070 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 138 ms
5,452 KB
testcase_08 AC 37 ms
5,516 KB
testcase_09 AC 183 ms
6,368 KB
testcase_10 AC 183 ms
6,368 KB
testcase_11 AC 172 ms
5,884 KB
testcase_12 AC 168 ms
6,012 KB
testcase_13 AC 167 ms
6,384 KB
testcase_14 AC 95 ms
6,444 KB
testcase_15 AC 96 ms
6,396 KB
testcase_16 AC 23 ms
5,604 KB
testcase_17 AC 260 ms
6,396 KB
testcase_18 AC 23 ms
5,476 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 206 ms
5,952 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 90 ms
5,888 KB
testcase_25 AC 128 ms
5,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int h, w, y, x;
long long int a[502][502];
int heap[1000006], l;
int comp_h(int A, int B)
{
	if (a[heap[A] / w][heap[A] % w] > a[heap[B] / w][heap[B] % w])
		return 1;
	else
		return -1;
}
void swap_h(int A, int B)
{
	int f = heap[A];
	heap[A] = heap[B];
	heap[B] = f;
	return;
}
void push(int ne)
{
	heap[l] = ne;
	int p = l;
	l++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
int pop()
{
	l--;
	swap_h(0, l);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < l)
		{
			if (comp_h(2 * p + 1, 2 * p + 2) > 0)
			{
				if (comp_h(p, 2 * p + 2) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < l)
		{
			if (comp_h(p, 2 * p + 1) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return heap[l];
}
int used[502][502];
int main()
{
	scanf("%d %d %d %d", &h, &w, &y, &x);
	int i, j, k;
	for (i = 0; i < h; i++)
		for (j = 0; j < w; j++)
			scanf("%lld", &a[i][j]);
	long long int v = a[y - 1][x - 1];
	a[y - 1][x - 1] = 0;
	for (i = 0; i < h; i++)
		for (j = 0; j < w; j++)
			used[i][j] = 0;
	int di[4] = { -1,0,1,0 };
	int dj[4] = { 0,-1,0,1 };
	int ii, jj;
	l = 0;
	push(w * (y - 1) + x - 1);
	while (l > 0)
	{
		i = pop();
		j = i % w;
		i /= w;
		if (used[i][j] > 0)
			continue;
		if (a[i][j] >= v)
			break;
		v += a[i][j];
		used[i][j]++;
		for (k = 0; k < 4; k++)
		{
			ii = i + di[k];
			jj = j + dj[k];
			if (ii < 0 || jj < 0 || ii >= h || jj >= w)
				continue;
			if (used[ii][jj] > 0)
				continue;
			push(w * ii + jj);
		}
	}
	k = 0;
	for (i = 0; i < h; i++)
		for (j = 0; j < w; j++)
			if (used[i][j] == 0)
				k++;
	if (k > 0)
		printf("No\n");
	else
		printf("Yes\n");
	return 0;
}
0