四子连棋

题目描述 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%相同,题解是条不归路。

 

Codevs p1004 四子连棋的更多相关文章

  1. codevs 1004 四子连棋

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

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

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

  3. CODEVS 1004四子连棋

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

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

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

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

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

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

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

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

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

  8. codevs1004四子连棋

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

  9. P2346 四子连棋

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

随机推荐

  1. Python win32api提取exe图标icon

    转载地址: http://blog.csdn.net/gumanren/article/details/6129416 代码如下: # -*- coding: utf-8 -*- import sys ...

  2. Juery Ajax语法

    $.ajax({ url: "/ForgetCard/ForgetLogin",//方法路径URL data: { strUser: $("#textUser" ...

  3. SQL常用方言列表

    DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS390 org.hi ...

  4. html5 notification桌面提醒功能

    html5 notification桌面提醒功能 <!DOCTYPE html> <html lang="en"> <head> <met ...

  5. js 上传文件后缀名的判断 var flag=false;应用

    js 上传文件后缀名的判断  var flag=false;应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  6. BPEL是个什么东东

    研究团队有个做智能服务组合的,其中用到叫BPEL的东西,因为全称是Business Process Execution Language,译成中文就是商业执行过程语言,这个东东的是整合SOA的一个执行 ...

  7. AndroidStudio中创建Assets文件

  8. 封装用className选元素

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. hdu 2553 N皇后问题

    回溯. 一个主对角线,副对角线的技巧 //vis[0][i]表示第i列有没有皇后 vis[1][cur+i]表示副对角线 vis[2][cur-i+n]表示主对角线 #include <cstd ...

  10. LIS(n^2) POJ 2533 Longest Ordered Subsequence

    题目传送门 题意:LIS(Longest Increasing Subsequence)裸题 分析:状态转移方程:dp[i] = max (dp[j]) + 1   (a[j] < a[i],1 ...