fork download
  1. #include<iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int xx[] = {-1,0,1,0}, yy[] = {0,1,0,-1};
  7. int ans = 0, vir[10][10],orgVir[10][10];
  8. int N, M;
  9.  
  10. void countVir() {
  11. int tmp = 0;
  12. for (int i = 0; i < N; i++)
  13. for (int j = 0; j < M; j++)
  14. if (vir[i][j] == 0)
  15. tmp += 1;
  16. ans = max(ans, tmp);
  17. }
  18. void copyVir() {
  19. for(int i=0;i<N;i++)
  20. for (int j = 0; j < M; j++) {
  21. orgVir[i][j] = vir[i][j];
  22. }
  23. }
  24. void copyOrg() {
  25. for (int i = 0; i < N; i++)
  26. for (int j = 0; j < M; j++) {
  27. vir[i][j] = orgVir[i][j];
  28. }
  29. }
  30. void func(int x, int y) { // 바이러스 전파
  31. if (x < 0 || y < 0 || x >= N || y >= M) return;
  32. if (vir[x][y] == 2) {
  33. for (int i = 0; i < 4; i++) {
  34. if ((x + xx[i] >= 0 && y + yy[i] >= 0 && x + xx[i] < N && y + yy[i] < M) && vir[x + xx[i]][y + yy[i]] == 0) {
  35. vir[x + xx[i]][y + yy[i]] = 2;
  36. func(x + xx[i], y + yy[i]);
  37. }
  38. }
  39. }
  40. if (y == M - 1) func(x + 1, 0);
  41. else func(x, y + 1);
  42. }
  43.  
  44. void wall(int x,int y,int count) { // 벽 설치
  45. if (x < 0 || y < 0 || x >= N || y >= M) return;
  46. if (count == 3) {
  47. copyVir();
  48. func(0, 0);
  49. countVir();
  50. copyOrg();
  51. return;
  52. }
  53. if (vir[x][y] == 0) {
  54. int tmpx, tmpy;
  55. if (y == M - 1) {
  56. tmpx = x + 1; tmpy = 0;
  57. }
  58. else {
  59. tmpx = x; tmpy = y+1;
  60. }
  61. wall(tmpx, tmpy, count);
  62. vir[x][y] = 1;
  63. wall(tmpx, tmpy, count+1);
  64. vir[x][y] = 0;
  65. }
  66. else {
  67. if (y == M - 1)
  68. wall(x + 1, 0, count);
  69. else
  70. wall(x, y + 1, count);
  71. }
  72. }
  73. int main(void) {
  74. ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  75. cin >> N >> M;
  76. for(int i=0;i<N;i++)
  77. for (int j = 0; j < M; j++) {
  78. int tmp;
  79. cin >> tmp;
  80. vir[i][j] = tmp;
  81. }
  82. wall(0, 0, 0);
  83. cout << ans;
  84. }
  85.  
Success #stdin #stdout 0s 5324KB
stdin
1 6
0 2 0 2 0 2
stdout
Standard output is empty