結果

問題 No.3131 Twin Slide Puzzles
ユーザー Aeren
提出日時 2025-04-26 07:44:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3,594 ms / 4,000 ms
コード長 4,229 bytes
コンパイル時間 5,349 ms
コンパイル使用メモリ 350,056 KB
実行使用メモリ 390,752 KB
最終ジャッジ日時 2025-06-20 02:53:15
合計ジャッジ時間 53,297 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <bits/allocator.h> // Temp fix for gcc13 global pragma
// #pragma GCC target("avx2,bmi2,popcnt,lzcnt")
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
#ifdef LOCAL
	#include "Debug.h"
#else
	#define debug_endl() 42
	#define debug(...) 42
	#define debug2(...) 42
	#define debug_bin(...) 42
#endif

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
struct splitmix64_hash{
	static unsigned long long _splitmix64(unsigned long long x){
		x += 0x9e3779b97f4a7c15;
		x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
		x = (x ^ x >> 27) * 0x94d049bb133111eb;
		return x ^ x >> 31;
	}
	size_t operator()(unsigned long long x) const{
		static const unsigned long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return _splitmix64(x + FIXED_RANDOM);
	}
	template<class T>
	size_t operator()(const vector<T> &a) const{
		static const unsigned long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		static const unsigned long long base = chrono::steady_clock::now().time_since_epoch().count();
		unsigned long long h = 0;
		for(auto c: a) h = _splitmix64(base * h + c + FIXED_RANDOM);
		return h;
	}
};
template<class K, class U, class Compare = less<>>
using indexable_map = tree<K, U, Compare, rb_tree_tag, tree_order_statistics_node_update>;
template<class K, class Compare = less<>>
using indexable_set = indexable_map<K, null_type, Compare>;
template<class K, class V, class Hash = splitmix64_hash>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
template <class K, class Hash = splitmix64_hash>
using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>;

namespace direction_vectors{
	vector<array<int, 2>> dr2{{1, 0}, {0, 1}};
	vector<array<int, 2>> dr4{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
	vector<array<int, 2>> dr4diag{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
	vector<array<int, 2>> dr8{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
	vector<array<int, 2>> drk{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
	vector<array<int, 2>> generate(int low, int high){
		assert(0 <= low && low <= high);
		int th = sqrt(high) + 1;
		vector<array<int, 2>> dr;
		for(auto x = -th; x <= th; ++ x) for(auto y = -th; y <= th; ++ y) if(auto d = x * x + y * y; low <= d && d <= high) dr.push_back({x, y});
		return dr;
	}
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	auto dr = direction_vectors::dr4;
	int n;
	cin >> n;
	vector coef(n, vector<int>(n));
	for(auto &x: coef | ranges::views::join){
		cin >> x;
	}
	const int m = min(n, 4);
	vector<int> a(m * m);
	for(auto i = 0; i < m; ++ i){
		for(auto j = 0; j < m; ++ j){
			a[m * i + j] = n * i + j;
		}
	}
	auto get_cost = [&](const vector<int> &a)->long long{
		long long cost = 0;
		for(auto i = 0; i < m; ++ i){
			for(auto j = 0; j < m; ++ j){
				cost += 1LL * coef[i][j] * a[m * i + j];
			}
		}
		return cost;
	};
	hash_set<vector<int>> found;
	found.insert(a);
	hash_map<long long, vector<int>> mp;
	mp[get_cost(a)] = a;
	deque<vector<int>> dq{a};
	while(!dq.empty()){
		auto a = dq.front();
		dq.pop_front();
		int zi = -1, zj = -1;
		for(auto i = 0; i < m; ++ i){
			for(auto j = 0; j < m; ++ j){
				if(a[m * i + j] == 0){
					zi = i, zj = j;
					goto DONE;
				}
			}
		}
		DONE:;
		for(auto [di, dj]: dr){
			int in = zi + di, jn = zj + dj;
			if(0 <= min(in, jn) && max(in, jn) < m){
				swap(a[m * zi + zj], a[m * in + jn]);
				if(found.insert(a).second){
					auto ca = get_cost(a);
					if(!mp.insert({ca, a}).second){
						auto b = mp[ca];
						cout << "Yes\n";
						for(auto rep = 2; rep; -- rep){
							for(auto i = 0; i < n; ++ i){
								for(auto j = 0; j < n; ++ j){
									if(i < m && j < m){
										cout << a[m * i + j] << " ";
									}
									else{
										cout << n * i + j << " ";
									}
								}
								cout << "\n";
							}
							swap(a, b);
						}
						return 0;
					}
					dq.push_back(a);
				}
				swap(a[m * zi + zj], a[m * in + jn]);
			}
		}
	}
	cout << "No\n";
	return 0;
}

/*

*/
0