結果

問題 No.2675 KUMA
ユーザー ltf0501
提出日時 2025-03-20 19:45:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,158 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 215,924 KB
実行使用メモリ 36,736 KB
最終ジャッジ日時 2025-03-20 19:45:08
合計ジャッジ時間 5,007 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
  os << "[ ";
	for(auto &x : v)
    os << x << ' ';
	return os << "]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator << (Ostream &os, const pair<Ts...> &p){
  return os << "{" << p.first << ", " << p.second << "}";
}
void dbg_cerr() { cerr << "\e[0m\n"; }
template<typename Head, typename... Tail> void dbg_cerr(Head H, Tail... T) { cerr << ' ' << H; dbg_cerr(T...); }
#ifdef LTF
#define DEBUG(...) cerr << "\e[1;31m[" #__VA_ARGS__ "]:", dbg_cerr(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

void Solve() {
	int N; cin >> N;
	vector<pair<int, int>> p(N);
	for (auto &[x, y] : p) cin >> x >> y;
	map<pair<int, int>, int> mp;
	for (int i = 0; i < N; i++) mp[p[i]] = i;

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

	set<pair<int, int>> st;
	vector<pair<int, int>> pos;
	for (auto &[x, y] : p) {
		for (int o = 0; o < 8; o++) {
			int nx = x + dx[o], ny = y + dy[o];
			if (!mp.count({nx, ny}) && st.find({nx, ny}) == st.end()) {
				st.insert({nx, ny});
				pos.emplace_back(nx, ny);
			}
		}
	}

	int M = static_cast<int>(pos.size());
	constexpr int kInf = int(1e9);
	vector dp(M + 1, vector<int>(1 << N, kInf));
	dp[0][0] = 0;
	for (int i = 0; i < M; i++) {
		auto [x, y] = pos[i];
		for (int mask = 0; mask < (1 << N); mask++) {
			dp[i + 1][mask] = min(dp[i + 1][mask], dp[i][mask]);
		}

		for (int o = 0; o < 8; o += 2) {
			int good_mask = 0;
			for (int t = o; t < o + 2; t++) {
				int nx = x + dx[t], ny = y + dy[t];
				if (mp.count({nx, ny})) good_mask ^= (1 << mp[{nx, ny}]);
			}

			for (int mask = 0; mask < (1 << N); mask++) {
				dp[i + 1][mask ^ good_mask] = min(dp[i + 1][mask ^ good_mask], dp[i][mask] + 1);
			}
		}
	}

	if (dp[M][(1 << N) - 1] == kInf) cout << -1 << '\n';
	else cout << dp[M][(1 << N) - 1] << '\n';
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr);
  int T = 1;
  // cin >> T;
  while (T--) {
    Solve();
  }
  return 0;
}
0