Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #define N 50
- using namespace std;
- int n, m, a[N][N], answer = 0, res;
- void rec(int x, int y) {
- if (res + (n - y) * m + m - x + 1 <= answer)
- return;
- int nx = x + 1, ny = y;
- if (nx == m + 1) {
- nx = 0;
- ny++;
- if (ny == n + 1) {
- answer = max(answer, res);
- return;
- }
- }
- if (a[x][y] == 1) {
- if (x > 1 && a[x - 1][y] > 0) {
- a[x][y]--;
- a[x - 1][y]++;
- res++;
- rec(nx, ny);
- res--;
- a[x][y]++;
- a[x - 1][y]--;
- }
- if (x < m && a[x + 1][y] > 0) {
- a[x][y]--;
- a[x + 1][y]++;
- res++;
- rec(nx, ny);
- res--;
- a[x][y]++;
- a[x + 1][y]--;
- }
- if (y < n && a[x][y + 1] > 0) {
- a[x][y]--;
- a[x][y + 1]++;
- res++;
- rec(nx, ny);
- res--;
- a[x][y]++;
- a[x][y + 1]--;
- }
- if (y > 1 && a[x][y - 1] > 0) {
- a[x][y]--;
- a[x][y - 1]++;
- res++;
- rec(nx, ny);
- res--;
- a[x][y]++;
- a[x][y - 1]--;
- }
- }
- rec(nx, ny);
- }
- int main() {
- freopen("sol.cpp", "w", stdout);
- for (m = 1; m <= 40; m++)
- for (n = 1; n * m <= 40; n++) {
- for (int i = 1; i <= m; i++)
- for (int j = 1; j <= m; j++)
- a[i][j] = 1;
- answer = 0, res = 0;
- rec(1, 1);
- printf("a[%d][%d] = %d;\n", m, n, answer);
- cerr << m << " " << n << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement