1004 四子连棋

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。

 
 
输入描述 Input Description
  1. 从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description

用最少的步数移动到目标棋局的步数。

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

5

 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdio>
  4.  
  5. using namespace std;
  6. int map[][],ans,flag;
  7. int dx[]={,-,,,},dy[]={,,,-,};
  8. inline void dfs(int ch, int deep);
  9.  
  10. inline void swap(int &a,int &b)
  11. {
  12. int t = a;
  13. a = b;
  14. b = t;
  15. }
  16.  
  17. bool check()//判断是否符合条件
  18. {
  19. for (int k=;k<=;k++)
  20. {
  21. if (map[k][]==map[k][]&&map[k][]==map[k][]&&map[k][]==map[k][])return ;
  22. if (map[][k]==map[][k]&&map[][k]==map[][k]&&map[][k]==map[][k])return ;
  23. }
  24. if (map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
  25. if (map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
  26. return ;
  27. }
  28.  
  29. void move(int ch,int deep,int x,int y) //ch表示下一个颜色
  30. {
  31. for(int i=;i<=;i++)
  32. {
  33. int xx=x+dx[i],yy=y+dy[i];
  34. if(map[xx][yy]==ch&&xx>&&xx<&&yy>&&yy<)
  35. {
  36. swap(map[x][y],map[xx][yy]);
  37. dfs(ch,deep+);
  38. swap(map[x][y],map[xx][yy]);
  39. }
  40. }
  41. }
  42.  
  43. void dfs(int ch,int deep)
  44. {
  45. int next=!ch;
  46. if(flag) return;
  47. if(ans==deep)
  48. {
  49. if(check()) flag=;
  50. return;
  51. }
  52. for(int i=;i<=;i++)
  53. for(int j=;j<=;j++)
  54. if(map[i][j]==-) move(next,deep,i,j);
  55. }
  56.  
  57. int main()
  58. {
  59. for(int i = ; i <= ; i++)
  60. for(int j = ; j <= ; j++)
  61. {
  62. char a;
  63. cin>>a;
  64. if(a == 'B') map[i][j] = ;
  65. if(a == 'O') map[i][j] = -;
  66. }
  67. for(ans = ; flag == ; ans++)
  68. {
  69. dfs(, );
  70. if(flag) break;
  71. dfs(, );
  72. if(flag) break;
  73. }
  74. printf("%d\n",ans);
  75. }

dfs

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<map>
  5. #define maxn 5001
  6.  
  7. using namespace std;
  8. struct node
  9. {
  10. int map[][];
  11. int f;//表示这一步走的棋子是黑还是白
  12. };node e[];
  13. int mm[][],tep[maxn],head=,tail=,ans=maxn,flag;
  14. int dx[]={,,,,-};
  15. int dy[]={,,-,,};
  16. map<string,bool> hash;
  17.  
  18. bool equ(int a1,int a2,int a3,int a4){if(a1!=a2||a2!=a3||a3!=a4||a4!=a1)return ;return ;}
  19. bool judge(int w)
  20. {
  21. for(int i=;i<=;i++)
  22. {
  23. if(equ(e[w].map[i][],e[w].map[i][],e[w].map[i][],e[w].map[i][]))return ;
  24. if(equ(e[w].map[][i],e[w].map[][i],e[w].map[][i],e[w].map[][i]))return ;
  25. }
  26. if(equ(e[w].map[][],e[w].map[][],e[w].map[][],e[w].map[][]))return ;
  27. if(equ(e[w].map[][],e[w].map[][],e[w].map[][],e[w].map[][]))return ;
  28. return ;
  29. }
  30.  
  31. void copy()
  32. {
  33. for(int k=;k<=;k++)
  34. for(int l=;l<=;l++)
  35. e[tail].map[k][l]=e[head].map[k][l];
  36. }
  37.  
  38. bool check(node x)//hash判重
  39. {
  40. string s="";
  41. for(int i=;i<=;i++)
  42. for(int j=;j<=;j++)
  43. s+=x.map[i][j]+'';
  44. if(hash[s])return true;
  45. hash[s]=;return false;
  46. }
  47.  
  48. void Bfs()
  49. {
  50. while(head<tail)
  51. {
  52. head++;
  53. for(int i=;i<=;i++)
  54. for(int j=;j<=;j++)
  55. {
  56. if(e[head].map[i][j]==)
  57. {
  58. for(int cnt=;cnt<=;cnt++)
  59. {
  60. int x=i+dx[cnt],y=j+dy[cnt];
  61. if(x>&&x<&&y>&&y<&&e[head].map[x][y]!=e[head].f&&e[head].map[x][y]!=)
  62. {
  63. ++tail;
  64. copy();
  65. e[tail].map[x][y]=;
  66. if(e[head].f==)
  67. {
  68. e[tail].f=-;
  69. e[tail].map[i][j]=-;
  70. }
  71. else
  72. {
  73. e[tail].f=;
  74. e[tail].map[i][j]=;
  75. }
  76. tep[tail]=tep[head]+;
  77. if(judge(tail))
  78. {
  79. flag=;
  80. ans=min(ans,tep[tail]);
  81. break;//手残打成return...
  82. }
  83. if(check(e[tail])) tail--;
  84. }
  85. }
  86. }
  87. if(flag)break;
  88. }
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. for(int i=;i<=;i++)
  95. for(int j=;j<=;j++)
  96. {
  97. char c;
  98. cin>>c;
  99. if(c=='W')mm[i][j]=;
  100. else if(c=='B')mm[i][j]=-;
  101. else mm[i][j]=;
  102. }
  103. for(int k=;k<maxn;k++)
  104. for(int i=;i<=;i++)
  105. for(int j=;j<=;j++)
  106. {
  107. e[].map[i][j]=mm[i][j];
  108. e[k].f=;
  109. tep[k]=;
  110. }
  111. flag=;ans=maxn;
  112. e[].f=-;
  113. Bfs();
  114. hash.clear();
  115. for(int k=;k<maxn;k++)
  116. for(int i=;i<=;i++)
  117. for(int j=;j<=;j++)
  118. {
  119. e[].map[i][j]=mm[i][j];
  120. e[k].f=;
  121. tep[k]=;
  122. }
  123. head=;tail=;flag=;
  124. e[].f=;
  125. Bfs();//因为一开始走黑棋白棋结果可能不一样,所以两遍
  126. printf("%d\n",ans);
  127. return ;
  128. }

bfs

codevs1004四子连棋的更多相关文章

  1. 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋

    一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...

  2. codevs1004四子连棋[BFS 哈希]

    1004 四子连棋   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold   题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...

  3. 迭代加深搜索[codevs1004 四子连棋]

    迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...

  4. codevs1004 四子连棋

    题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双 ...

  5. Codevs p1004 四子连棋

                          四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...

  6. codevs 1004 四子连棋

    1004 四子连棋  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...

  7. codevs 1004 四子连棋 BFS、hash判重

    004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...

  8. 【洛谷 P2346】四子连棋(状态压缩,搜索)

    其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...

  9. P2346 四子连棋

    P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...

随机推荐

  1. 洛谷——P3173 [HAOI2009]巧克力

    P3173 [HAOI2009]巧克力 题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短 ...

  2. 网络模型、IP命令、SS命令介绍

    1. 分层对应关系 OSI七层模型和TCP/IP五层模型都属于TCP/IP协议栈,而TCP/IP协议栈只有两种传输层协议:TCP.UDP,所以对于Telnet.FTP这些协议,建议称之为承载在TCP之 ...

  3. rsync+sersync自动同步备份数据

    一.为什么要用Rsync+sersync架构?1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修 ...

  4. 第八节:web爬虫之urllib(四)

    第三个 模块parse : 是一个工具模块,提供了许多 URL 处理方法,比如拆分.解析.合并等等的方法.

  5. average column data from multiple files

    example in file a, data is [1 , 2, 3; 4,5,6] file b, data is  [4,5, 6; 7,8,9] average=0.5 (a+b) matl ...

  6. Netty学习总结(4)——图解Netty之Pipeline、channel、Context之间的数据流向

    以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件) 当用户调用channel的connect时,会发起一个outbound类型的事件,该事件将在pipel ...

  7. Ural 1114 Boxes

    Boxes Time Limit: 600ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 1114 ...

  8. 不动点(Fixed Point)

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51735818 在数学中,函数的不动点( ...

  9. 个人常用git命令

    最近开始使用git,将自己常用git命令做一个简单归纳,便于记忆. 初始化及配置 git init:初始化资料库 git config --global user.name 'xxx':配置用户名 g ...

  10. hdu_1024_糖果大战_201404021640

    糖果大战 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...