結果

問題 No.2958 Placing Many L-s
ユーザー 👑 binap
提出日時 2024-05-21 08:16:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,663 bytes
コンパイル時間 4,590 ms
コンパイル使用メモリ 256,292 KB
最終ジャッジ日時 2025-02-21 16:22:37
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 TLE * 1 -- * 27
権限があれば一括ダウンロードができます

ソースコード

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 solve(){
int h, w;
cin >> h >> w;
int n = h * w;
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, 0, 0, 1}, {0, 1, 1, 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, 1, 2, 2}, {0, 0, 1, 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{
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(){
int t;
cin >> t;
rep(_, t) solve();
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0