696-How Many Knights (number theory)
696-How Many Knights (number theory)
Question Link
It is required to have the most server guard on a board, but they cannot attack each other. The attack scope of the server guard is shown in the figure below.
Train of Thought: After the image is painted, we will find that the upper and lower lines of the server guard are the best strategy.
(N)
(N)
(N)
But this only applies to 3*3 matrix and larger matrix, smaller points will be discussed separately.
Each position after a row or column can be placed without mutual attacks. The first two columns are required for the two rows, and the four server guard columns are not allowed for the next two columns. Therefore, the four columns and the four columns are considered.
Code:
#include
#include
#include using namespace std;int main () { int n, m, r, c, ans; while (scanf ("%d%d", &n, &m) && (n || m)) { r = min(n, m); c = max(n, m); if (r == 2) { int mod = c % 4; int mul = c / 4; ans = mul * 4 + (mod <= 2? (mod * 2) : 4); } else if (r <= 1) { ans = r * c; } else { ans = (n * m + 1) / 2; } printf ("%d knights may be placed on a %d row %d column board.\n", ans, n, m); } return 0;}