Codevs p1004 四子连棋
四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
| ● | ○ | ● | |
| ○ | ● | ○ | ● |
| ● | ○ | ● | ○ |
| ○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
hi
思路分析: bfs+hash判重,记录每次走过的棋局,进行每条直线与对角线的判断(类似于八皇后的判断),记录下一步该走黑棋还是白棋。
程序:
#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 四子连棋的更多相关文章
- codevs 1004 四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- CODEVS 1004四子连棋
[题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
- 【洛谷 P2346】四子连棋(状态压缩,搜索)
其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...
- codevs1004四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...
- P2346 四子连棋
P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
随机推荐
- 在python多进程中使用manager和Barrier
注意:Barrier是PYTHON3才有的功能,在2中无法测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessin ...
- makefile基础(GNU)
makefile的核心 targets : prerequisites ; commands... //不分行的情况 targets : prerequisites ...
- php重修
阅读顺序: http://www.laruence.com/2008/08/11/147.html 深入浅出php http://www.laruence.com/2008/06/18/221.ht ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- <转>ORA-06413 连接未打开错误
ORA-06413 Connection not open.Cause: Unable to establish connection.Action: Use diagnostic procedure ...
- VS2015 Preview Secondary Installer 离线安装
VS2015 Preview Secondary Installer 离线安装 天朝的原因orz, 装过vs2015 preview 的人都懂的,第二阶段安装会失败.假公济私的研究了下VS2015,摸 ...
- loj 1379(最短路变形)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087 思路:题目的意思是求S->T的所有路径中花费总和小于 ...
- JavaScript 之 document对象
对象属性document.title //设置文档标题等价于HTML的title标签document.bgColor //设置页面背景色document.fgColor //设置前景色(文本颜色)do ...
- Android 编程下 DP、SP 以及屏幕像素密度
有时需为视图属性指定大小尺寸值(通常以像素为单位,但有时也用点.毫米或英寸).最常见的属性有: 文字大小(Text Size),指设备上显示的文字像素高度: 边距(Margin),指定视图组件间的距离 ...
- ADB常用的几个命令
1. 查看设备 adb devices 查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示 2. 安装软件 adb install [-r] [-s] <file> ...