codevs 1004 四子连棋 BFS、hash判重
004 四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
● | ○ | ● | |
○ | ● | ○ | ● |
● | ○ | ● | ○ |
○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
样例输出
Sample Output
5
数据范围及提示
Data Size & Hint
hi
#include<iostream>
using namespace std;
struct data{
int mp[][];//1白棋,2黑棋,0空格
}d[];
int next[]={,};//下一步走黑棋还是白棋,1为白,2为黑
int step[];
bool hash[];
int xx[]={,,,-},yy[]={,-,,};
int t=,w=,flag=;
bool equ(int a1,int a2,int a3,int a4){if(a1!=a2||a2!=a3||a3!=a4||a4!=a1)return ;return ;}
bool check(){
for(int i=;i<=;i++)
{
if(equ(d[w].mp[i][],d[w].mp[i][],d[w].mp[i][],d[w].mp[i][]))return ;
if(equ(d[w].mp[][i],d[w].mp[][i],d[w].mp[][i],d[w].mp[][i]))return ;
}
if(equ(d[w].mp[][],d[w].mp[][],d[w].mp[][],d[w].mp[][]))return ;
if(equ(d[w].mp[][],d[w].mp[][],d[w].mp[][],d[w].mp[][]))return ;
return ;
}
int Hash(){//哈希判重
int s=,k=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{s+=d[w].mp[i][j]*k;k*=;}
s%=;
if(!hash[s]){hash[s]=;return ;}
return ;
}
bool pd(int x,int y){
int k=next[t];
if(x>||y>||x==||y==)return ;
else if(d[t].mp[x][y]==k)return ;
return ;
}
void sp(int &a,int &b){int t=a;a=b;b=t;}
void move(int x,int y){
for(int i=;i<;i++)
{
int p=x+xx[i],q=y+yy[i];
if(pd(p,q))
{
for(int j=;j<=;j++)
for(int k=;k<=;k++)
d[w].mp[j][k]=d[t].mp[j][k];
sp(d[w].mp[x][y],d[w].mp[p][q]);
step[w]=step[t]+;
if(check()){cout<<step[w];flag=;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(d[t].mp[i][j]==)move(i,j);
if(flag)return;
}
t++;
}
}
int main(){
char x;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
cin>>x;
if(x=='W')d[].mp[i][j]=d[].mp[i][j]=;
else if(x=='B')d[].mp[i][j]=d[].mp[i][j]=;
}
search();
return ;
}
codevs 1004 四子连棋 BFS、hash判重的更多相关文章
- codevs 1004 四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...
- CODEVS 1004四子连棋
[题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- 【wikioi】1004 四子连棋
题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...
- BFS搜索算法应用_Codevs 1004 四子连棋
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...
- CODEVS——T 1004 四子连棋
http://codevs.cn/problem/1004/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
随机推荐
- Jenkins忘记用户名密码
一.进入C盘.jenkins配置文件中找到config.xml 需要删除一下内容: <useSecurity>true</useSecurity> <authorizat ...
- cocos2d-x 日志...
cocos2d-x 日志... http://blog.csdn.net/themagickeyjianan/article/details/39008297http://blog.csdn.net ...
- docker stack 部署 seafile(http)
=============================================== 2018/5/13_第1次修改 ccb_warlock == ...
- docker stack 部署 rabbitmq 容器
=============================================== 2018/5/13_第1次修改 ccb_warlock == ...
- SQLAlchemy-对象关系教程ORM-create
ORM是建立在SQL语言构造器之上的工具集,用于将Python对象映射到数据库的行,提供了一系列接口用于从数据库中存取对象(行).在ORM 工作时,在底层调用SQL语言构造器的API,这些通用的操作有 ...
- java基础30 List集合下的LinkedList集合
单例集合体系: ---------| collection 单例集合的根接口--------------| List 如果实现了list接口的集合类,具备的特点:有序,可重复 注:集合 ...
- 安装window系统
安装服务器系统,进入windowpe后将iso中sources,bootmgr,和boot拷贝到C盘,执行bootsect.exe /nt60 c:,调试froad13的consle win8 改 ...
- AdvStringGrid 获取值
stringGrid.row stringgrid.col分别为当前行和列 stringGrid.cells[stringgrid.col,stringGrid.row]就是当前cell的值 ---- ...
- TObject、TPersisent 、TComponent、TControl、TGraphicControl、TWinControl 关系图
VCL的类图结构 TObject | TPersisent | ...
- jersey中的405错误 method not allowed