四子连棋

题目描述 Description

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

 
 

输入描述 Input Description

从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。


输出描述 Output Description

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


样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO


样例输出 Sample Output

5


数据范围及提示 Data Size & Hint

hi


思路分析: bfs+hash判重,记录每次走过的棋局,进行每条直线与对角线的判断(类似于八皇后的判断),记录下一步该走黑棋还是白棋。

我是看的hzw神犇的blog才会的,程序基本上是理解了照搬过来的,不建议看。hzw神犇的题解-->http://hzwer.com/848.html

程序:

 #include <iostream>
using namespace std;
struct data
{
int map[][];
}dt[];
int next[]={,};
int step[];
bool haxi[];
int x1[]={,,,-},
y1[]={,-,,};
int t=,w=,f=;
int hash()
{
int k=,s=;
for (int i=;i<=;i++)
for (int j=;j<=;j++)
{
s+=dt[w].map[i][j]*k;
k*=;
}
s%=;
if (!haxi[s])
{
haxi[s]=;
return ;
}
return ;
}
bool pd4l(int a1,int b1,int c,int d)
{
if (a1!=b1||b1!=c||c!=d||a1!=d) return ;
return ;
}
bool fnis()
{
for (int i=;i<=;i++)
{
if (pd4l(dt[w].map[i][],dt[w].map[i][],dt[w].map[i][],dt[w].map[i][])) return ;
if (pd4l(dt[w].map[][i],dt[w].map[][i],dt[w].map[][i],dt[w].map[][i])) return ;
}
if (pd4l(dt[w].map[][],dt[w].map[][],dt[w].map[][],dt[w].map[][])) return ;
if (pd4l(dt[w].map[][],dt[w].map[][],dt[w].map[][],dt[w].map[][])) return ;
return ;
}
void excg(int &a,int &b)
{ int t;
t=a;
a=b;
b=t;
}
bool pd(int x,int y)
{ int k;
k=next[t];
if (x>||x==||y>||y==) return ;
else if (dt[t].map[x][y]==k) return ;
return ;
}
void move(int x,int y)
{
int p,q;
for (int i=;i<;i++)
{
p=x1[i]+x;
q=y1[i]+y;
if (pd(p,q))
{
for (int j=;j<=;j++)
for (int k=;k<=;k++)
dt[w].map[j][k]=dt[t].map[j][k];
excg(dt[w].map[p][q],dt[w].map[x][y]);
step[w]=step[t]+;
if (fnis()) {cout<<step[w]; f=;return;}
if (hash())
{ if (next[t]==) next[w++]=;
if (next[t]==) next[w++]=;
}
}
}
}
void search()
{
while (t<w)
{
for (int i=;i<=;i++)
for (int j=;j<=;j++)
{
if (dt[t].map[i][j]==)
move(i,j);
if (f==) return;
}
t++;
}
}
int main()
{
char x;
for (int i=;i<=;i++)
for (int j=;j<=;j++)
{
cin>>x;
if (x=='W') {dt[].map[i][j]=dt[].map[i][j]=;}
if (x=='B') {dt[].map[i][j]=dt[].map[i][j]=;}
// if (x=='O') {dt[0].map[i][j]=dt[1].map[i][j]=0;}
}
search();
return ;
}

99%相同,题解是条不归路。

 

最新文章

  1. angular 数据绑定
  2. zookeeper dubbo 问题解决录
  3. hadoop-hdfs分布式文件系统
  4. phpcms图片模型调用组图的问题
  5. Node.js 入门手册:那些最流行的 Web 开发框架
  6. Oracle 11g-R2 SQL Developer连接MSSQL2008
  7. java静态代理,动态代理,cglib代理
  8. sql CAST用法
  9. C# INotifyPropertyChanged使用方法
  10. UIKit视图动画的微扩展
  11. native2ascii命令
  12. 使用git提交代码到github,每次都要输入用户名和密码的解决方法
  13. 关于shell命令的一些用法和技巧
  14. useful urls
  15. sudo执行脚本找不到环境变量和命令
  16. 20155336 虎光元《网络攻防》Exp2后门原理与实践
  17. 计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]
  18. ruby + phantomjs 自动化测试 - GA
  19. 使用Docker构建AspNetCore应用
  20. SpringMVC常用方法大全

热门文章

  1. 设计模式学习之装饰者模式(Decorator,结构型模式)(16)
  2. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
  3. 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序
  4. Win10 启动模拟器
  5. [Tools] maven-eclipse安装及配置
  6. PAT A 1004. Counting Leaves (30)【vector+dfs】
  7. Android三种基本的加载网络图片方式(转)
  8. css与js后边有?v=20160101
  9. Codeforces Round #375 (Div. 2) - B
  10. POJ 3693 后缀数组