#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void SwapMN(vector<string>& a)
{
int n = a.size(), m = a[0].length();
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
a[i][j] = (a[i][j] == '-' ? '+' : '-');
swap(n, m);
vector<string> temp(n, string(m, '-'));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
temp[i][j] = a[j][i];
a.swap(temp);
}
void Solve12(int n, int m)
{
if (m <= 2 && n > m)
{
cout << n << '\n';
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
cout << "+";
cout << '\n';
}
}
else if (n <= 2)
{
cout << m << '\n';
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
cout << "-";
cout << '\n';
}
}
}
int Value(const vector<string>& a)
{
int n = a.size();
int m = a[0].length();
int res = 0;
for (int i = 0; i < n; ++i)
if (2 * count(a[i].begin(), a[i].end(), '+') > m)
++res;
for (int j = 0; j < m; ++j)
{
int cnt = 0;
for (int i = 0; i < n; ++i)
if (a[i][j] == '-')
++cnt;
if (2 * cnt > n)
++res;
}
return res;
}
bool SolveEvens(int n, int m)
{
bool swaped = n < m;
if (swaped)
swap(n, m);
vector<string> a(n, string(m, '-'));
for (int i = 0; i < n; ++i)
for (int j = m - 3; j < m; ++j)
a[i][j] = '+';
int block = (m / 2 + 1 - 3);
int j = m - 4;
for (int i = n - 1; i >= 0; --i)
{
for (int cnt = 0; cnt < block; ++cnt)
a[i][(j - cnt + m - 3) % (m - 3)] = '+';
j = (j - block + m - 3) % (m - 3);
}
if (swaped)
{
swaped = false;
swap(n, m);
SwapMN(a);
}
int value;
if (value = Value(a) < n + m - 4)
return false;
cout << Value(a) << '\n';
for (int i = 0; i < n; ++i)
cout << a[i] << '\n';
cout << flush;
return true;
}
void SolveOdd(int n, int m)
{
bool swaped = (n % 2 == 0);
if (swaped)
swap(n, m);
vector<string> a(n, string(m, '-'));
for (int i = 0; i < n; ++i)
for (int j = m - 2; j < m; ++j)
a[i][j] = '+';
int block = (m / 2 + 1 - 2);
int j = m - 3;
for (int i = n - 1; i >= 0; --i)
{
for (int cnt = 0; cnt < block; ++cnt)
a[i][(j - cnt + m - 2) % (m - 2)] = '+';
j = (j - block + m - 2) % (m - 2);
}
if (swaped)
{
swaped = false;
swap(n, m);
SwapMN(a);
}
cout << Value(a) << '\n';
for (int i = 0; i < n; ++i)
cout << a[i] << '\n';
cout << flush;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
if (min(n, m) <= 2)
{
Solve12(n, m);
continue;
}
if (n % 2 == 0 && m % 2 == 0)
{
if (SolveEvens(n, m))
continue;
}
else
{
SolveOdd(n, m);
continue;
}
vector<string> a(n, string(m, '+'));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
a[i][j] = ((i + j) % 2 ? '+' : '-');
for (int i = 0; i < n; ++i)
for (int j = m - 1 - (m + 1) % 2; j < m; ++j)
a[i][j] = '+';
for (int j = 0; j < m; ++j)
for (int i = n - 1 - (n + 1) % 2; i < n; ++i)
a[i][j] = '-';
cout << Value(a) << '\n';
for (int i = 0; i < n; ++i)
cout << a[i] << '\n';
cout << flush;
}
}
Compilation message
stones.cpp: In function 'bool SolveEvens(int, int)':
stones.cpp:86:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (value = Value(a) < n + m - 4)
~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Wrong answer in test 4 3: 4 < 5 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
376 KB |
Wrong answer in test 3 45: 26 < 46 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Wrong answer in test 4 3: 4 < 5 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
1400 KB |
Wrong answer in test 23 57: 62 < 78 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1404 KB |
Output is correct |
2 |
Correct |
10 ms |
1500 KB |
Output is correct |
3 |
Correct |
10 ms |
1312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Wrong answer in test 4 3: 4 < 5 |