fork(2) download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. char info[5][5];
  6. void read ()
  7. {
  8. for (int i=1; i<=4; i++)
  9. {
  10. for (int j=1; j<=4; j++)
  11. cin>>info[i][j];
  12. }
  13. }
  14.  
  15. int arr[25];
  16. int coins[5][5];
  17. int _1Dto2D ()
  18. {
  19. int k=1;
  20. int count = 0;
  21. for (int i=1; i<=4; i++)
  22. {
  23. for (int j=1; j<=4; j++)
  24. {
  25. coins[i][j]=arr[k];
  26. if (arr[k]==1) count++;
  27. k++;
  28. }
  29. }
  30. return count;
  31. }
  32.  
  33. char rs[5][5];
  34. int x_xq[]={+1, +0, -1, +0};
  35. int y_xq[]={+0, -1, +0, +1};
  36. void latxu ()
  37. {
  38. //Cop:
  39. for (int i=1; i<=4; i++)
  40. {
  41. for (int j=1; j<=4; j++)
  42. rs[i][j]=info[i][j];
  43. }
  44. for (int i=1; i<=4; i++)
  45. {
  46. for (int j=1; j<=4; j++)
  47. {
  48. if (coins[i][j]==1)
  49. {
  50. if (rs[i][j]=='b') rs[i][j]='w';
  51. else rs[i][j]='b';
  52. for (int k=0; k<4; k++)
  53. {
  54. int X=j+x_xq[k];
  55. int Y=i+y_xq[k];
  56. if (X>=1 && X<=4 && Y>=1 && Y<=4)
  57. {
  58. if (rs[Y][X]=='b') rs[Y][X]='w';
  59. else rs[Y][X]='b';
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. int check ()
  68. {
  69. char init = rs[1][1];
  70. for (int i=1; i<=4; i++)
  71. {
  72. for (int j=1; j<=4; j++)
  73. {
  74. if (rs[i][j]!=init) return 0;
  75. }
  76. }
  77. return 1;
  78. }
  79.  
  80. char tt[5][5];
  81. int t2[5][5];
  82.  
  83. int Ans = 25;
  84. void sinhNP (int u)
  85. {
  86. if (u>16)
  87. {
  88. int x = _1Dto2D ();
  89. latxu ();
  90. if (check()==1) Ans = min (Ans, x);
  91. }
  92. else
  93. {
  94. arr[u]=0;
  95. sinhNP (u+1);
  96. arr[u]=1;
  97. sinhNP (u+1);
  98. }
  99. }
  100.  
  101. int main ()
  102. {
  103. read ();
  104. sinhNP (1);
  105. if (Ans != 25) cout<<Ans;
  106. else cout<<"Impossible";
  107. return 0;
  108. }
Success #stdin #stdout 0s 16064KB
stdin
bwbb
wwwb
bwbb
bbbb
stdout
1