fork(2) download
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define S 7
  6. #define M 4
  7. #define N (S*S) / M
  8.  
  9. int cell(int grid[S][S], int x, int y)
  10. {
  11. if (x < 0 || x >= S) return 0;
  12. if (y < 0 || y >= S) return 0;
  13.  
  14. return grid[y][x];
  15. }
  16.  
  17. int solve(int *repo, int grid[S][S], int x, int y)
  18. {
  19. if (y == S) {
  20. int i, j;
  21.  
  22. for (i = 0; i < 7; i++) {
  23. for (j = 0; j < 7; j++) {
  24. putchar(".RGYB"[grid[j][i]]);
  25. }
  26. putchar(10);
  27. }
  28. putchar(10);
  29.  
  30. return 1;
  31. } else {
  32. int nextx = x + 1;
  33. int nexty = y;
  34. int res = 0;
  35. int i;
  36.  
  37. if (nextx == 7) {
  38. nextx = 0;
  39. nexty++;
  40. }
  41.  
  42. for (i = 0; i < M + 1; i++) {
  43. if (repo[i] == 0) continue;
  44. if (i && cell(grid, x - 1, y - 1) == i) continue;
  45. if (i && cell(grid, x + 0, y - 1) == i) continue;
  46. if (i && cell(grid, x + 1, y - 1) == i) continue;
  47. if (i && cell(grid, x - 1, y + 0) == i) continue;
  48.  
  49. grid[y][x] = i;
  50. repo[i]--;
  51.  
  52. res += solve(repo, grid, nextx, nexty);
  53.  
  54. grid[y][x] = 0;
  55. repo[i]++;
  56. }
  57.  
  58. return res;
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. int repo[M + 1] = {1, N, N, N, N};
  65. int grid[S][S] = {{0}};
  66. int n;
  67.  
  68. n = solve(repo, grid, 0, 0);
  69.  
  70. printf("%d cells, %d groups of %d prisoners: %d\n", S*S, M, N, n);
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0.08s 2168KB
stdin
Standard input is empty
stdout
RYRBGBG
GBGYRYR
RYRBGBG
GBG.YRY
YRYRBGB
BGBGYRY
YRYRBGB

RBRYGYG
GYGBRBR
RBRYGYG
GYG.BRB
BRBRYGY
YGYGBRB
BRBRYGY

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