记录编号 287766 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOIP 2010冲刺七]翻转游戏 最终得分 100
用户昵称 GravatarSOBER GOOD BOY 是否通过 通过
代码语言 C++ 运行时间 0.230 s
提交时间 2016-08-02 13:52:01 内存使用 0.41 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. //wuli 暴力搜索
  6. using namespace std;
  7.  
  8. const int Move[5][5]={{0,0},{0,-1},{0,1},{-1,0},{1,0}};
  9.  
  10. bool A[6][6];
  11.  
  12. bool flag[6][6];
  13.  
  14. int f[6][6][888];
  15. int Ans=99999999;
  16. void sou(int,int,int);
  17. //b-> 0,w -> 1
  18. int main()
  19. {
  20. freopen("flip.in","r",stdin);
  21. freopen("flip.out","w",stdout);
  22. char ch;
  23. int tot=0;
  24. for(int i=1;i<=4;i++)
  25. {
  26. for(int j=1;j<=4;j++)
  27. {
  28. scanf(" %c",&ch);
  29. if(ch=='w')
  30. {
  31. A[i][j]=1;
  32. tot++;
  33. }
  34. }
  35. }
  36. if(tot==0||tot==16)
  37. {
  38. printf("0");
  39. return 0;
  40. }
  41. for(int i=1;i<=4;i++)
  42. {
  43. for(int j=1;j<=4;j++)
  44. {
  45. sou(i,j,1);
  46. }
  47. }
  48. if(Ans==99999999)printf("Impossible");
  49. else printf("%d",Ans);
  50. fclose(stdin);
  51. fclose(stdout);
  52. return 0;
  53. }
  54. void sou(int x,int y,int step)
  55. {
  56. if(f[x][y][step]>556)return;
  57. f[x][y][step]++;
  58. flag[x][y]=1;
  59. for(int i=0;i<5;i++)
  60. {
  61. int xx=x+Move[i][0],yy=y+Move[i][1];
  62. //if(xx<1||xx>4||yy<1||yy>4)continue;
  63. A[xx][yy]=!A[xx][yy];
  64. }
  65. int tot=0;
  66. for(int i=1;i<=4;i++)
  67. {
  68. for(int j=1;j<=4;j++)
  69. {
  70. tot+=A[i][j];
  71. }
  72. }
  73. if(tot==0||tot==16)
  74. {
  75. Ans=min(Ans,step);
  76. flag[x][y]=0;
  77. for(int i=0;i<5;i++)
  78. {
  79. int xx=x+Move[i][0],yy=y+Move[i][1];
  80. //if(xx<1||xx>4||yy<1||yy>4)continue;
  81. A[xx][yy]=!A[xx][yy];
  82. }
  83. return;
  84. }
  85. else
  86. {
  87. for(int i=1;i<=4;i++)
  88. {
  89. for(int j=1;j<=4;j++)
  90. {
  91. if(!flag[i][j])
  92. {
  93. sou(i,j,step+1);
  94. }
  95. }
  96. }
  97. flag[x][y]=0;
  98. for(int i=0;i<5;i++)
  99. {
  100. int xx=x+Move[i][0],yy=y+Move[i][1];
  101. //if(xx<1||xx>4||yy<1||yy>4)continue;
  102. A[xx][yy]=!A[xx][yy];
  103. }
  104. }
  105. }