codevs1004四子连棋
1004 四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
| ● | ○ | ● | |
| ○ | ● | ○ | ● |
| ● | ○ | ● | ○ |
| ○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
#include<iostream>
#include<cstdio>
#include<cstdio> using namespace std;
int map[][],ans,flag;
int dx[]={,-,,,},dy[]={,,,-,};
inline void dfs(int ch, int deep); inline void swap(int &a,int &b)
{
int t = a;
a = b;
b = t;
} bool check()//判断是否符合条件
{
for (int k=;k<=;k++)
{
if (map[k][]==map[k][]&&map[k][]==map[k][]&&map[k][]==map[k][])return ;
if (map[][k]==map[][k]&&map[][k]==map[][k]&&map[][k]==map[][k])return ;
}
if (map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if (map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
return ;
} void move(int ch,int deep,int x,int y) //ch表示下一个颜色
{
for(int i=;i<=;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(map[xx][yy]==ch&&xx>&&xx<&&yy>&&yy<)
{
swap(map[x][y],map[xx][yy]);
dfs(ch,deep+);
swap(map[x][y],map[xx][yy]);
}
}
} void dfs(int ch,int deep)
{
int next=!ch;
if(flag) return;
if(ans==deep)
{
if(check()) flag=;
return;
}
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(map[i][j]==-) move(next,deep,i,j);
} int main()
{
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
char a;
cin>>a;
if(a == 'B') map[i][j] = ;
if(a == 'O') map[i][j] = -;
}
for(ans = ; flag == ; ans++)
{
dfs(, );
if(flag) break;
dfs(, );
if(flag) break;
}
printf("%d\n",ans);
}
dfs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxn 5001 using namespace std;
struct node
{
int map[][];
int f;//表示这一步走的棋子是黑还是白
};node e[];
int mm[][],tep[maxn],head=,tail=,ans=maxn,flag;
int dx[]={,,,,-};
int dy[]={,,-,,};
map<string,bool> hash; bool equ(int a1,int a2,int a3,int a4){if(a1!=a2||a2!=a3||a3!=a4||a4!=a1)return ;return ;}
bool judge(int w)
{
for(int i=;i<=;i++)
{
if(equ(e[w].map[i][],e[w].map[i][],e[w].map[i][],e[w].map[i][]))return ;
if(equ(e[w].map[][i],e[w].map[][i],e[w].map[][i],e[w].map[][i]))return ;
}
if(equ(e[w].map[][],e[w].map[][],e[w].map[][],e[w].map[][]))return ;
if(equ(e[w].map[][],e[w].map[][],e[w].map[][],e[w].map[][]))return ;
return ;
} void copy()
{
for(int k=;k<=;k++)
for(int l=;l<=;l++)
e[tail].map[k][l]=e[head].map[k][l];
} bool check(node x)//hash判重
{
string s="";
for(int i=;i<=;i++)
for(int j=;j<=;j++)
s+=x.map[i][j]+'';
if(hash[s])return true;
hash[s]=;return false;
} void Bfs()
{
while(head<tail)
{
head++;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
if(e[head].map[i][j]==)
{
for(int cnt=;cnt<=;cnt++)
{
int x=i+dx[cnt],y=j+dy[cnt];
if(x>&&x<&&y>&&y<&&e[head].map[x][y]!=e[head].f&&e[head].map[x][y]!=)
{
++tail;
copy();
e[tail].map[x][y]=;
if(e[head].f==)
{
e[tail].f=-;
e[tail].map[i][j]=-;
}
else
{
e[tail].f=;
e[tail].map[i][j]=;
}
tep[tail]=tep[head]+;
if(judge(tail))
{
flag=;
ans=min(ans,tep[tail]);
break;//手残打成return...
}
if(check(e[tail])) tail--;
}
}
}
if(flag)break;
}
}
} int main()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
char c;
cin>>c;
if(c=='W')mm[i][j]=;
else if(c=='B')mm[i][j]=-;
else mm[i][j]=;
}
for(int k=;k<maxn;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
e[].map[i][j]=mm[i][j];
e[k].f=;
tep[k]=;
}
flag=;ans=maxn;
e[].f=-;
Bfs();
hash.clear();
for(int k=;k<maxn;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
e[].map[i][j]=mm[i][j];
e[k].f=;
tep[k]=;
}
head=;tail=;flag=;
e[].f=;
Bfs();//因为一开始走黑棋白棋结果可能不一样,所以两遍
printf("%d\n",ans);
return ;
}
bfs
codevs1004四子连棋的更多相关文章
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- codevs1004 四子连棋
题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双 ...
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- 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颗白色棋 ...
- 【洛谷 P2346】四子连棋(状态压缩,搜索)
其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...
- P2346 四子连棋
P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
随机推荐
- UVA - 10603 Fill(BFS求最小值问题)
题目: 给出三个杯子(没有刻度线)的容量,起初之后第三个杯子是满的,其他的两个杯子是空的,容量分别是a.b.c.问最少需要倒多少升水才能让某一个杯子中的水有d升?如果不能恰好做到d升,就让某一个杯子里 ...
- python lambda简易使用
基本格式 lambda 变量名:函数表达式 ①直接使用 f=lambda x:x**2 f(3) ②设置函数列表 l=[lambda x:x**2, lambda x:x**3, lambda x:x ...
- 关于 CMSIS 标准 及 STM32F10x的固件库
CMSIS 标准英文全称是Cortex MicroController Software Interface Standard,翻译为中文意思就是 ARM Cortex 微控制器软件接口标准. 由于基 ...
- noip模拟赛 天天和不可描述
分析:直接就这么翻肯定是不行的,换一种想法:有括号就是把括号里的字符串倒着输出,如果在括号里又遇到了括号就继续倒着输出,相当于递归. 我们可以用递归直接做,也可以用一层循环搞定,每次从左括号跳到右括号 ...
- android获取年月日时分秒
Calendar calendar=Calendar.getInstance(); //获取当前时间,作为图标的名字 String year=calendar.get(Calendar.YEAR)+& ...
- Eddy's mistakes
Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for ...
- Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)
ClassPathXmlApplicationContext 在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...
- RDLC报表总结
这2天纠结的报表基本上已近完成大部分功能.现在总结一下自己近期的学习成果 首先制作微软RDLC报表由以下三部分构成:1.制作自己的DateSet集合(就是报表的数据集):2.制作自己的报表文件.rdl ...
- postgresql vacuum table
2down vote according to Documentation VACUUM reclaims storage occupied by dead tuples. But according ...
- 你有必要知道的 25 个 JavaScript 面试题
1.使用 typeof bar === "object" 推断 bar 是不是一个对象有神马潜在的弊端?怎样避免这样的弊端? 使用 typeof 的弊端是显而易见的(这样的弊端同使 ...