Advertisement
Malinovsky239

Untitled

Sep 3rd, 2011
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5.  
  6. #define N 50
  7.  
  8. using namespace std;
  9.  
  10. int n, m, a[N][N], answer = 0, res;
  11.  
  12. void rec(int x, int y) {
  13.  
  14.     if (res + (n - y) * m + m - x + 1 <= answer)
  15.         return;
  16.  
  17.     int nx = x + 1, ny = y;
  18.     if (nx == m + 1) {
  19.         nx = 0;
  20.         ny++;
  21.         if (ny == n + 1) {         
  22.             answer = max(answer, res);
  23.             return;
  24.         }
  25.     }
  26.  
  27.     if (a[x][y] == 1) {
  28.         if (x > 1 && a[x - 1][y] > 0) {
  29.             a[x][y]--;
  30.             a[x - 1][y]++;
  31.             res++;
  32.             rec(nx, ny);
  33.             res--;
  34.             a[x][y]++;
  35.             a[x - 1][y]--;
  36.         }
  37.  
  38.         if (x < m && a[x + 1][y] > 0) {
  39.             a[x][y]--;
  40.             a[x + 1][y]++;
  41.             res++;
  42.             rec(nx, ny);
  43.             res--;
  44.             a[x][y]++;
  45.             a[x + 1][y]--;
  46.         }
  47.  
  48.         if (y < n && a[x][y + 1] > 0) {
  49.             a[x][y]--;
  50.             a[x][y + 1]++;
  51.             res++;
  52.             rec(nx, ny);
  53.             res--;
  54.             a[x][y]++;
  55.             a[x][y + 1]--;
  56.         }
  57.  
  58.         if (y > 1 && a[x][y - 1] > 0) {
  59.             a[x][y]--;
  60.             a[x][y - 1]++;
  61.             res++;
  62.             rec(nx, ny);
  63.             res--;
  64.             a[x][y]++;
  65.             a[x][y - 1]--;
  66.         }  
  67.     }
  68.  
  69.     rec(nx, ny);
  70. }
  71.  
  72. int main() {
  73.     freopen("sol.cpp", "w", stdout);
  74.  
  75.     for (m = 1; m <= 40; m++)
  76.         for (n = 1; n * m <= 40; n++) {
  77.             for (int i = 1; i <= m; i++)
  78.                 for (int j = 1; j <= m; j++)
  79.                     a[i][j] = 1;
  80.             answer = 0, res = 0;                       
  81.             rec(1, 1);
  82.             printf("a[%d][%d] = %d;\n", m, n, answer);
  83.             cerr << m << " " << n << endl;
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement