結果

問題 No.3131 Twin Slide Puzzles
ユーザー Aeren
提出日時 2025-04-26 07:23:20
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,862 bytes
コンパイル時間 4,667 ms
コンパイル使用メモリ 310,532 KB
実行使用メモリ 299,596 KB
最終ジャッジ日時 2025-04-26 07:24:12
合計ジャッジ時間 51,574 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 32 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

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

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;
	};
	map<long long, vector<int>> mp;
	set<vector<int>> found{a};
	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) < n){
				swap(a[m * zi + zj], a[m * in + jn]);
				if(!found.contains(a)){
					auto ca = get_cost(a);
					if(mp.contains(ca)){
						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;
					}
					mp[ca] = a;
					found.insert(a);
					dq.push_back(a);
				}
				swap(a[m * zi + zj], a[m * in + jn]);
			}
		}
	}
	cout << "No\n";
	return 0;
}

/*

*/
0