#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
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 f = n < m;
if (f)
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 (f)
{
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(m, 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);
}
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 Solve3(int n, int m)
{
vector<string> a(n, string(m, '-'));
if (m == 3)
{
for (int i = 0; i < n; ++i)
{
a[i][0] = '-';
a[i][1] = a[i][2] = '+';
}
}
else if (n == 3)
{
for (int j = 0; j < m; ++j)
{
a[0][j] = '+';
a[1][j] = a[2][j] = '-';
}
}
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 == 3 || m == 3)
{
Solve3(n, m);
continue;
}
if (n % 2 == 0 && m % 2 == 0)
if (SolveEvens(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:79: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 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Incorrect |
4 ms |
376 KB |
Wrong answer in test 38 5: 40 < 41 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1400 KB |
Output is correct |
2 |
Correct |
11 ms |
1656 KB |
Output is correct |
3 |
Correct |
11 ms |
1924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1400 KB |
Output is correct |
2 |
Correct |
11 ms |
1528 KB |
Output is correct |
3 |
Correct |
11 ms |
1288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Incorrect |
4 ms |
376 KB |
Wrong answer in test 38 5: 40 < 41 |