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颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
随机推荐
- blender源代码编译
blender源码路径(svn):https://svn.blender.org/svnroot/bf-blender/trunk/blender/ 依赖外部Lib(svn):https://svn. ...
- Java 初学记录之一 快速输入
1. sysout 按回车 System.out.println();
- 初识VSTO Addin开发
这篇博客将简单介绍一些VSTO Addin开发的知识. 1. VSTO是什么?我们可以用VSTO做什么? VSTO全称Visual Studio Tool for Office,是可以让我们针对现有的 ...
- hdu 4044 2011北京赛区网络赛E 树形dp ****
专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...
- [转]svn常用命令
谢谢原作者:http://blog.sina.com.cn/s/blog_963453200101eiuq.html 1.检出svn co http://路径(目录或文件的全路径) [本地目录全路 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees DP
C. Coloring Trees ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...
- 小甲鱼PE详解之IMAGE_NT_HEADERS结构定义即各个属性的作用(PE详解02)
PE Header 是PE相关结构NT映像头(IMAGE_NT_HEADER)的简称,里边包含着许多PE装载器用到的重要字段.下边小甲鱼将为大家详细讲解哈~ (视频教程:http://fishc.co ...
- 如何将Android Studio项目提交(更新)到github
转载:http://blog.csdn.net/jinrall/article/details/45787477 前言 在写这篇文章之前首先我假设你已经安装了Android Studio 并已经会用A ...
- loadrunner取出关联数组中的所有元素
方法一: int num; char nameVar[100]; char nameValue[100]; lr_save_string("AAA","name_1&qu ...
- [转载]void及void*的深度剖析
void的含义 void即"无类型",void *则为"无类型指针",可以指向任何数据类型. void指针使用规范 ①void指针可以指向任意类型的数据,亦即可 ...