codevs1004 四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
| ● | ○ | ● | |
| ○ | ● | ○ | ● |
| ● | ○ | ● | ○ |
| ○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ; struct member{
int last,step;
int board[][];//[y][x]
};
member first,q[maxn];
int tot;
int dx[] = {,-,,};
int dy[] = {-,,,}; bool judge(member temp){
int r1 = ,r2 = ;
for(r1 = ;r1 <= ;r1++){
bool sign = true;
for(r2 = ;r2 <= ;r2++){
if(temp.board[r1][r2] != temp.board[r1][r2+]) sign = false;
}
if(sign) return true;
}
for(r1 = ;r1 <= ;r1++){
bool sign = true;
for(r2 = ;r2 <= ;r2++){
if(temp.board[r2][r1] != temp.board[r2+][r1]) sign = false;
}
if(sign) return true;
}
if(temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][]) return true;
if(temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][]) return true;
return false;
} int bfs(){
int h = ,t = ,nx,ny;
member tailer;
while(h <= t){
if(judge(q[h])) return q[h].step;
int f1 = ,f2 = ,ya,xa,yb,xb,sign = ;
for(f1 = ;f1 <= ;f1++){
for(f2 = ;f2 <= ;f2++){
if(q[h].board[f1][f2] == ){
if(sign == ){
sign = ;
ya = f1;
xa = f2;
}else if(sign == ){
yb = f1;
xb = f2;
}
}
}
}
f1 = f2 = ;
for(f1 = ;f1 < ;f1++){
nx = xa + dx[f1];
ny = ya + dy[f1];
if(nx <= && nx >= && ny <= && ny >= && q[h].board[ny][nx] != q[h].last){
t++;
tailer = q[h];
tailer.last = tailer.board[ny][nx];
swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
tailer.step++;
q[t] = tailer; }
nx = xb + dx[f1];
ny = yb + dy[f1];
if(nx <= && nx >= && ny <= && ny >= && q[h].board[ny][nx] != q[h].last){
t++;
tailer = q[h];
tailer.last = tailer.board[ny][nx];
swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
tailer.step++;
q[t] = tailer; } }
h++;
} } int main(){
char cmd;
int r1,r2,ans;
for(r1 = ;r1 <= ;r1++){
for(r2 = ;r2 <= ;r2++){
cin>>cmd;
if(cmd == 'O') first.board[r1][r2] = ;
if(cmd == 'B') first.board[r1][r2] = ;
if(cmd == 'W') first.board[r1][r2] = ;
}
}
first.step = ;
first.last = ;
q[] = first;
ans = bfs();
cout<<ans;
return ;
}
codevs1004 四子连棋的更多相关文章
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- codevs1004四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有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颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
随机推荐
- cookie使用详解
cookie是用来保存客户资料的好方法,与同样可以用来保存客户资料的 session不同的是,session是把资料保存在服务器端,而cookie是把资料保存在客户端,我们平常接触的最多的cookie ...
- P2973 [USACO10HOL]赶小猪
跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...
- spring Cache /Redis 缓存 + Spring 的集成示例
spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...
- Android 性能优化(5)网络优化 (1) Collecting Network Traffic Data 用Network Traffic tool :收集传输数据
Collecting Network Traffic Data 1.This lesson teaches you to Tag Network Requests 标记网络类型 Configure a ...
- 页面置换算法-LRU(Least Recently Used)c++实现
最近最久未使用(LRU)置换算法 #include <iostream> #include <cstdio> #include <cstring> #include ...
- mysql若干问题
一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...
- CF814B An express train to reveries
思路: 模拟,枚举. 实现: #include <iostream> using namespace std; ; int a[N], b[N], cnt[N], n, x, y; int ...
- Django--1、MTV及基本应用
web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,以避免重复造轮子. 所有的Web应用,本质上是一个socket服务 ...
- python自动化--语言基础三字典、函数、全局/局部变量
字典 dict1 = {,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values()) #打印所有的values值 pri ...
- sp_Msforeachtable与sp_Msforeachdb详解
一.简要介绍: 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程.从mssql6.5开始,存放在SQL Server的MASTER数据 ...