看题传送门:http://poj.org/problem?id=1753

DFS枚举的应用。

基本上是参考大神的。。。。

学习学习。。

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. int n,s,d,ans;
  5. bool a[5][5],flag=false;
  6.  
  7. //判断全部一样的情况
  8. bool alllike()
  9. {
  10. for(int i=0;i<4;i++)
  11. for(int j=0;j<4;j++)
  12. if(a[i][j]!=a[0][0])
  13. return false;
  14.  
  15. return true;
  16. }
  17.  
  18. //翻转棋
  19. void flip(int r,int c)
  20. {
  21. if(r>0)
  22. a[r-1][c]=!a[r-1][c];
  23.  
  24. if(c>0)
  25. a[r][c-1]=! a[r][c-1];
  26.  
  27. if(r<3)
  28. a[r+1][c]=!a[r+1][c];
  29.  
  30. if(c<3)
  31. a[r][c+1]=!a[r][c+1];
  32.  
  33. a[r][c]=!a[r][c];
  34. }
  35.  
  36. void dfs(int r,int c,int deep)
  37. {
  38. if(deep==ans)
  39. {
  40. flag=alllike();
  41. return;
  42. }
  43.  
  44. if(r==4||flag)
  45. return;
  46.  
  47. flip(r,c); //翻转棋
  48.  
  49. if(c<3)
  50. dfs(r,c+1,deep+1);
  51. else
  52. dfs(r+1,0,deep+1);
  53.  
  54. flip(r,c); //把棋翻回来
  55.  
  56. if(c<3) //如果不满足则不翻转该棋
  57. dfs(r,c+1,deep);
  58. else
  59. dfs(r+1,0,deep);
  60.  
  61. return;
  62. }
  63.  
  64. int main()
  65. {
  66. char c;
  67. for(int i=0;i<4;i++)
  68. for(int j=0;j<4;j++)
  69. {
  70. scanf("%c",&c);
  71. if(j==3)
  72. getchar();
  73. // cin>>c; //直接用cin也可以
  74. if(c=='b')
  75. a[i][j]=true;
  76. else
  77. a[i][j]=false;
  78. }
  79.  
  80. for(ans=0;ans<=16;ans++)
  81. {
  82. dfs(0,0,0);
  83. if(flag)
  84. break;
  85. }
  86.  
  87. if(flag)
  88. printf("%d\n",ans);
  89. else
  90. printf("Impossible\n");
  91.  
  92. }

POJ 1753 Flip Game DFS枚举的更多相关文章

  1. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  2. poj 1753 Flip Game (dfs)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28805   Accepted: 12461 Descr ...

  3. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

  4. poj 1753 Flip Game(暴力枚举)

    Flip Game   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52279   Accepted: 22018 Des ...

  5. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  6. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  9. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

随机推荐

  1. hq-源码编译

    这里编译整个项目的基本格式是 ./mk + 平台名 +项目名_客户名 +mmm +new 例如 ./mk hq6735_65c_b1k_l1 al811_doov mmm new 编译单个模块 ./m ...

  2. region实现大纲效果

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 具有可视化的功能的一款开源软件Gource

    今天为大家介绍一个非常有趣儿的开源软件,Gource可以将代码版本控制系统里面的日志全部可视化,也就是说可以看见每个成员在系统里面提交代码的行为,Gource目前支持git,hg,svn. 650) ...

  4. 基于Linux平台的Openvas配置使用视频教学

    常见的漏洞扫描工具有Openvas.LSAT.Nessus.X-scan.ShadowSecurityScanner和流光等,openvas是一款最全面的开源漏洞扫描工具,由于openvas安装比较困 ...

  5. BZOJ——T 1355: [Baltic2009]Radio Transmission

    http://www.lydsy.com/JudgeOnline/problem.php?id=1355 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  6. Flume的可靠性

    Flume的可靠性 当节点出现故障时,日志能够被传送到其他节点上而不会丢失. Flume提供了三种级别的可靠性保障,从强到弱依次分别为:end-to- end(收到数据agent首先将event写到磁 ...

  7. 洛谷P1143 进制转换

    题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 输入数据共有三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10 ...

  8. select发生改变使用js提交form表单(get传值)

    form表单如下: <form id="my_form" method="get" action=""> <input t ...

  9. int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....

    Android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指针问题,费劲心思才找到报空指针的原因: 代码 ...

  10. 在window编写好的网站往linux上发布

    得知ASP.NET CORE可以跨平台,我内心很躁动,毕竟自己喜欢的,之前没有学过linux导致一开始上手linux有点困难,按照https://www.microsoft.com/net/core# ...