codevs 1004 四子连棋
1004 四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
| ● | ○ | ● | |
| ○ | ● | ○ | ● |
| ● | ○ | ● | ○ |
| ○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
hi
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int map[][];
int move[]={,,,-,};
int bor;
string _;int x[],y[],ans=0x7fffffff;
bool can(int x,int y,int z)
{
if(x>=&&x<=&&y>=&&y<=&&z!=map[x][y])return ;else return ;
}
bool check()
{
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
else return ;
}
bool dfs(int x1,int y1,int who,int x2,int y2,int step)
{
if(step==bor)
{
if(check())return ;
else return ;
}
int next_x1,next_x2,next_y1,next_y2;
for(int i=;i<;i++)
{
next_x1=x1+move[i];
next_y1=y1+move[i+];
next_x2=x2+move[i];
next_y2=y2+move[i+];
if(can(next_x1,next_y1,who))
{
int sssy1;
if(who==)
sssy1=;else sssy1=;
swap(map[x1][y1],map[next_x1][next_y1]);
if(dfs(next_x1,next_y1,sssy1,x2,y2,step+))return ;
swap(map[x1][y1],map[next_x1][next_y1]);
}
if(can(next_x2,next_y2,who))
{
int sssy2;
if(who==)sssy2=;else sssy2=;
swap(map[x2][y2],map[next_x2][next_y2]);
if(dfs(x1,y1,sssy2,next_x2,next_y2,step+))return ;
swap(map[x2][y2],map[next_x2][next_y2]);
}
}
return ;
}
int main()
{
int pppppp=;
for(int i=;i<=;i++)
{
cin>>_;
for(int j=;j<;j++)
{
if(_[j]=='B')map[i][j+]=;
else if(_[j]=='W')map[i][j+]=;
else
{
x[pppppp]=i;
y[pppppp++]=j+;
}
}
}
for(bor=;;bor++)
{
if(dfs(x[],y[],,x[],y[],))break;
if(dfs(x[],y[],,x[],y[],))break;
}
printf("%d",bor);
}
codevs 1004 四子连棋的更多相关文章
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- CODEVS 1004四子连棋
[题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- CODEVS——T 1004 四子连棋
http://codevs.cn/problem/1004/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- BFS搜索算法应用_Codevs 1004 四子连棋
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...
- 【wikioi】1004 四子连棋
题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
随机推荐
- 简单对象List自定义属性排序
<body> <div> sort()对数组排序,不开辟新的内存,对原有数组元素进行调换 </div> <div id="showBox" ...
- ORA-00906 missing left parenthesis括号
Oracle 建表报错:ORA-00906 missing left parenthesis括号 建表语句:create table test(id char,name varchar(1),s ...
- C++面试常见问题
转载:https://zhuanlan.zhihu.com/p/34016871?utm_source=qq&utm_medium=social 1.在C++ 程序中调用被C 编译器编译后的函 ...
- aarch64_l2
libfreehand-devel-0.1.1-5.fc26.aarch64.rpm 2017-05-23 07:16 26K fedora Mirroring Project libfreehand ...
- JVM常用启动参数+常用内存调试工具
一.JVM常用启动参数 -Xms:设置堆的最小值. -Xmx:设置堆的最大值. -Xmn:设置新生代的大小. -Xss:设置每个线程的栈大小. -XX:NewSize:设置新生代的初始值. -XX:M ...
- Windows 10安装MongoDB(安装&启动)
Windows 10家庭中文版,MongoDB 3.6.3, 最近在学习Scrapy,可以却从未将scraped data存储到数据库中.在看过一些文档后,Scrapy会和MongoDB结合使用(还有 ...
- 学习总结——JMeter做http接口功能测试
JMeter对各种类型接口的测试 默认做接口测试前,已经给出明确的接口文档(如,http://test.nnzhp.cn/wiki/index.php?doc-view-59):本地配好了JMeter ...
- 使用插件实现Jenkins参数化构建
一.插件安装 1.打开插件管理,在此界面可以安装插件 二.参数化 1.在“可选插件”中查找如下两个插件然后安装,安装后重启Jenkins Build With Parameters 输入框式的参数 P ...
- SQL数据是否存在(是否有数据)判断,表,存储过程是否存在
判断是否存在数据 if exists( select * from Hong_PageConfig where names='name' ) Begin print '1' End else Begi ...
- LeetCode664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...