//-------------dilanyan------------\\
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
//------------------KarginDefines--------------------\\
#define ll long long
#define pb push_back
#define all(u) (u).begin(), (u).end()
#define pqueue priority_queue
#define upper upper_bound
#define lower lower_bound
#define umap unordered_map
#define uset unordered_set
#define Kargin ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Usaco freopen(".in", "r", stdin); freopen(".out", "w", stdout);
//-------------------KarginConstants------------------\\
const ll mod = 1000000007;
const ll inf = 2e9;
//-------------------KarginCode------------------------\\
const int N = 1005;
char a[N][N], ans[N][N];
int cnt = 0;
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
int n, m;
int A = 0, B = 0;
void dfs(int sx, int sy,char c) {
a[sx][sy] = c;
cnt++;
for (int i = 0;i < 4;i++) {
int x = sx + dx[i], y = sy + dy[i];
if (x >= 1 && x <= n && y >= 1 && y <= m) {
if (a[x][y] == '0') {
dfs(x, y, '+');
dfs(x, y, '-');
}
}
}
if (cnt == n * m) {
int ax = 0, bx = 0;
for (int i = 1;i <= n;i++) {
int r = 0, b = 0;
for (int j = 1;j <= m;j++) {
if (a[i][j] == '+') r++;
else b++;
}
if (r > b) ax++;
}
for (int i = 1;i <= m;i++) {
int r = 0, b = 0;
for (int j = 1;j <= n;j++) {
if (a[j][i] == '+') r++;
else b++;
}
if (b > r) bx++;
}
if (ax + bx > A + B) {
A = ax, B = bx;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
ans[i][j] = a[i][j];
}
}
}
}
a[sx][sy] = '0';
cnt--;
}
void KarginSolve() {
cin >> n >> m;
if (n <= 4 && m <= 4) {
A = 0, B = 0;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
a[i][j] = '0';
ans[i][j] = '0';
}
}
dfs(1, 1, '+');
dfs(1, 1, '-');
cout << A + B << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << ans[i][j];
}
cout << '\n';
}
}
else if (min(n, m) <= 4) {
if (n < m) {
if (n == 1) {
cout << m << '\n';
for (int i = 1;i <= m;i++) cout << '-';
cout << '\n';
}
else if (n == 2) {
cout << m << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << '-';
}
cout << '\n';
}
}
else {
cout << m + 1 << '\n';
for (int i = 1;i <= m;i++) cout << '+';
cout << '\n';
for (int i = 2;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << '-';
}
cout << '\n';
}
}
}
else {
if (m == 1) {
cout << n << '\n';
for (int i = 1;i <= n;i++) cout << "+\n";
}
else if (m == 2) {
cout << n << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << '+';
}
cout << '\n';
}
}
else {
cout << n + 1 << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
if (j <= m / 2) cout << '-';
else cout << '+';
}
cout << '\n';
}
}
}
}
else if (min(n, m) <= 5) {
assert(min(n, m) == 5);
if (n < m) {
if (m & 1) {
for (int i = 1;i <= m / 2 + 1;i++) ans[1][i] = '+';
for (int i = m / 2 + 2;i <= m;i++) ans[1][i] = '-';
for (int i = 1;i <= m / 2;i++) ans[2][i] = '-';
for (int i = m / 2 + 1;i <= m;i++) ans[2][i] = '+';
for (int i = 1;i <= m / 2;i++) ans[3][i] = '+';
ans[3][m / 2 + 1] = '-', ans[3][m / 2 + 2] = '+';
for (int i = m / 2 + 3;i <= m;i++) ans[3][i] = '-';
for (int i = 4;i <= n;i++) {
for (int j = 1;j <= m;j++) {
ans[i][j] = '-';
}
}
}
else {
for (int i = 1;i <= m / 2 + 1;i++) ans[1][i] = '+';
for (int i = m / 2 + 2;i <= m;i++) ans[1][i] = '-';
for (int i = 1;i < m / 2;i++) ans[2][i] = '-';
for (int i = m / 2;i <= m;i++) ans[2][i] = '+';
for (int i = 1;i < m / 2;i++) ans[3][i] = '+';
ans[3][m / 2] = '-', ans[3][m / 2 + 1] = '-';
ans[3][m / 2 + 2] = '+', ans[3][m / 2 + 3] = '+';
for (int i = m / 2 + 4;i <= m;i++) ans[3][i] = '-';
for (int i = 4;i <= n;i++) {
for (int j = 1;j <= m;j++) {
ans[i][j] = '-';
}
}
}
cout << m + 3 << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << ans[i][j];
}
cout << '\n';
}
}
else {
if (n & 1) {
for (int i = 1;i <= n / 2 + 1;i++) ans[i][1] = '-';
for (int i = n / 2 + 2;i <= n;i++)ans[i][1] = '+';
for (int i = 1;i <= n / 2;i++) ans[i][2] = '+';
for (int i = n / 2 + 1;i <= n;i++) ans[i][2] = '-';
for (int i = 1;i <= n / 2;i++) ans[i][3] = '-';
ans[n / 2 + 1][3] = '+', ans[n / 2 + 2][3] = '-';
for (int i = n / 2 + 3;i <= n;i++) ans[i][3] = '+';
for (int i = 4;i <= m;i++) {
for (int j = 1;j <= n;j++) {
ans[j][i] = '+';
}
}
}
else {
for (int i = 1;i <= n / 2 + 1;i++) ans[i][1] = '-';
for (int i = n / 2 + 2;i <= n;i++)ans[i][1] = '+';
for (int i = 1;i < n / 2;i++) ans[i][2] = '+';
for (int i = n / 2;i <= n;i++) ans[i][2] = '-';
for (int i = 1;i < n / 2;i++) ans[i][3] = '-';
ans[n / 2][3] = '+', ans[n / 2 + 1][3] = '+';
ans[n / 2 + 2][3] = '-', ans[n / 2 + 3][3] = '-';
for (int i = n / 2 + 4;i <= n;i++) ans[i][3] = '+';
for (int i = 4;i <= m;i++) {
for (int j = 1;j <= n;j++) {
ans[j][i] = '+';
}
}
}
cout << n + 3 << '\n';
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << ans[i][j];
}
cout << '\n';
}
}
}
}
int main() {
//Usaco
Kargin;
int test = 1;
cin >> test;
while (test--) {
KarginSolve();
}
return 0;
}
Compilation message
stones.cpp:1:1: warning: multi-line comment [-Wcomment]
1 | //-------------dilanyan------------\\
| ^
stones.cpp:8:1: warning: multi-line comment [-Wcomment]
8 | //------------------KarginDefines--------------------\\
| ^
stones.cpp:22:1: warning: multi-line comment [-Wcomment]
22 | //-------------------KarginConstants------------------\\
| ^
stones.cpp:27:1: warning: multi-line comment [-Wcomment]
27 | //-------------------KarginCode------------------------\\
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
508 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
508 ms |
348 KB |
Output is correct |
3 |
Correct |
21 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1024 ms |
504 KB |
in the table A+B is not equal to 19 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Wrong answer |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
506 ms |
440 KB |
Wrong answer |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
508 ms |
348 KB |
Output is correct |
3 |
Correct |
21 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1024 ms |
504 KB |
in the table A+B is not equal to 19 |
5 |
Halted |
0 ms |
0 KB |
- |