結果

問題 No.2958 Placing Many L-s
ユーザー 👑 binapbinap
提出日時 2024-10-25 10:35:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,071 bytes
コンパイル時間 4,561 ms
コンパイル使用メモリ 266,852 KB
実行使用メモリ 38,016 KB
最終ジャッジ日時 2024-11-08 20:52:43
合計ジャッジ時間 25,134 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 141 ms
38,016 KB
testcase_04 AC 139 ms
37,888 KB
testcase_05 WA -
testcase_06 AC 1,929 ms
37,632 KB
testcase_07 WA -
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 9 ms
5,248 KB
testcase_10 AC 14 ms
5,248 KB
testcase_11 AC 960 ms
5,248 KB
testcase_12 AC 250 ms
5,248 KB
testcase_13 AC 500 ms
5,248 KB
testcase_14 WA -
testcase_15 AC 101 ms
5,248 KB
testcase_16 AC 263 ms
5,248 KB
testcase_17 AC 518 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 WA -
testcase_20 AC 120 ms
32,896 KB
testcase_21 AC 3 ms
5,248 KB
testcase_22 AC 135 ms
35,072 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 107 ms
5,248 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 TLE -
testcase_30 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

int T;

// 右向きに
// 1 1 1 2
// 1 2 2 2
// 優先探索
// 弱いと思う

int solve(){
	
	int h, w;
	cin >> h >> w;
	int n = h * w;
	
	auto start = std::chrono::system_clock::now();
	auto TL = (long double)1900 / T;
	
	if(n % 4 != 0){
		cout << "-1\n";
		return 0;
	}
	int k = n / 4;
	vector<int> s(n, 0);
	bool success = false;
	auto f = [&](int y, int x){
		if(y < 0 || y >= h || x < 0 || x >= w) return -1;
		return y * w + x;
	};
	auto out = [&](){
		cout << k << "\n";
		rep(y, h){
			rep(x, w) cout << s[f(y, x)] << ' ';
			cout << "\n";
		}
	};
	vector<vector<int>> dy = {{0, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 1, 2}, {0, 1, 2, 2}, {0, 0, 1, 2}, {0, 1, 2, 2}, {0, 1, 1, 1}};
	vector<vector<int>> dx = {{0, 0, 1, 2}, {0, 1, 2, 2}, {0, 1, 2, 0}, {0, 1, 1, 1}, {0, 0, 0, 1}, {0, 1, 2, 3}, {0, 0, 0, -1}, {0, 0, -1, -2}};
	auto next = [&](int pos){
		int y = pos / w;
		int x = pos % w;
		vector<vector<int>> cand;
		rep(t, 8){
			bool ok = true;
			vector<int> v;
			rep(i, 4){
				int pos_new = f(y + dy[t][i], x + dx[t][i]);
				if(pos_new == -1){ok = false; break;}
				if(s[pos_new] != 0){ok = false; break;}
				v.push_back(pos_new);
			}
			if(ok) cand.push_back(v);
		}
		return cand;
	};
	
	
	auto dfs = [&](auto dfs, int pos, int num) -> void{
		
		auto end = std::chrono::system_clock::now();  // 計測終了時間
		auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count(); //処理に要した時間をミリ秒に変換
		if(elapsed >= TL) return;
	
		if(success) return;
		if(pos == n){
			out();
			success = true;
			return;
		}
		if(s[pos] == 0){
			vector<vector<int>> cand = next(pos);
			for(auto& v : cand){
				for(auto pos_new : v) s[pos_new] = num;
				dfs(dfs, pos + 1, num + 1);
				for(auto pos_new : v) s[pos_new] = 0;
			}
		}else dfs(dfs, pos + 1, num);
	};
	dfs(dfs, 0, 1);
	if(!success) cout << "-1\n";
	return 0;
}

int main(){
	cin >> T;
	rep(_, T) solve();
}
0