結果

問題 No.13 囲みたい!
ユーザー plasma_eplasma_e
提出日時 2019-01-20 18:32:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,679 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 127,440 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 21:03:01
合計ジャッジ時間 2,355 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 4 ms
4,352 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 5 ms
4,352 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<set>
#include<map>
#include<string>
#include<iomanip>
#include<cmath>
#include<stack>
#include<tuple>

int main()
{
	int W, H;
	std::cin >> W >> H;
	int field[102][102] = {};
	std::vector<std::pair<int, int>> start[1001];
	for (int i = 1; i <= H; ++i)
	{
		for (int k = 1; k <= W; ++k)
		{
			std::cin >> field[i][k];
			start[field[i][k]].emplace_back(i, k);
		}
	}
	for (int i = 1; i <= 1000; ++i)
	{
		bool check[102][102] = {};
		for (auto[a, b] : start[i])
		{
			if (check[a][b])
			{
				continue;
			}
			std::stack<std::tuple<int, int, int, int>> stack;
			stack.emplace(a, b, 0, 0);
			while (stack.size())
			{
				auto[s, t, ps, pt] = stack.top();
				stack.pop();
				if (check[s][t])
				{
					std::cout << "possible" << std::endl;
					return 0;
				}
				check[s][t] = true;
				{
					auto ns = s + 1;
					auto nt = t;
					if (field[ns][nt] == field[s][t] && (ns != ps || nt != pt))
					{
						stack.emplace(ns, nt, s, t);
					}
				}
				{
					auto ns = s - 1;
					auto nt = t;
					if (field[ns][nt] == field[s][t] && (ns != ps || nt != pt))
					{
						stack.emplace(ns, nt, s, t);
					}
				}
				{
					auto ns = s;
					auto nt = t + 1;
					if (field[ns][nt] == field[s][t] && (ns != ps || nt != pt))
					{
						stack.emplace(ns, nt, s, t);
					}
				}
				{
					auto ns = s;
					auto nt = t - 1;
					if (field[ns][nt] == field[s][t] && (ns != ps || nt != pt))
					{
						stack.emplace(ns, nt, s, t);
					}
				}
			}
		}
	}
	std::cout << "impossible" << std::endl;
}
0