题目描述 Description

在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。

 
 
输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description

用最少的步数移动到目标棋局的步数。

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

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 四子连棋的更多相关文章

  1. 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋

    一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...

  2. codevs1004四子连棋[BFS 哈希]

    1004 四子连棋   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold   题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...

  3. 迭代加深搜索[codevs1004 四子连棋]

    迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...

  4. codevs1004四子连棋

    1004 四子连棋  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...

  5. Codevs p1004 四子连棋

                          四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...

  6. codevs 1004 四子连棋

    1004 四子连棋  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...

  7. codevs 1004 四子连棋 BFS、hash判重

    004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...

  8. 【洛谷 P2346】四子连棋(状态压缩,搜索)

    其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...

  9. P2346 四子连棋

    P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...

随机推荐

  1. SAP基本搜索帮助及增强出口

    se11创建基本搜索帮助时,各个参数的含意 选择方法   指定命中列表的数据来源,可以是数据库表,视图,CDS.如果指定了搜索帮助出口函数则该字段可以不输,数据来源可以在出口中自行指定 对话类型: 输 ...

  2. 洛谷 P1462 通往奥格瑞玛的道路(spfa+二分搜索)(4boy)

    原题:http://www.luogu.org/problem/show?pid=1462#sub 4boy: 大意:给出n个城市,有m条路,每经过一个城市都要交钱,每经过一条道路都要扣HP,有HP上 ...

  3. [TJOI2012]桥

    Description 有n个岛屿,m座桥,每座桥连通两座岛屿,桥上会有一些敌人,玩家只有消灭了桥上的敌人才能通过,与此同时桥上的敌人会对玩家造成一定伤害.而且会有一个大Boss镇守一座桥,以玩家目前 ...

  4. JSP/Servlet Web应用中.properties文件的放置与读取

    本地项目 在本地类库中,我经常使用当前目录来放置.properties文件,这时调用方只要引用我的jar,并且将我的.properties放在他的classpath里面即可,比如: p.load(ne ...

  5. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

  6. html5前端杂记

    首先是css的一些知识 毕竟自己懂得不多,但是一看资料.感觉似曾相识 <style> .red-text { color: red; } </style>//这里是css样式的 ...

  7. Python学习日记之中文支持

    解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可

  8. js技巧(三)

    1.检测浏览器,search的用法 if(window.navigator.userAgent.search(/firefox/i)!=-1){ alert('ff'); } else if(wind ...

  9. 【C++】智能指针简述(三):scoped_ptr

    在介绍scoped_ptr之前,我们先回顾一下前两篇文章的内容. 首先,智能指针采用RAII机制,通过对象来管理指针,构造对象时,完成资源的初始化;析构对象时,对资源进行清理及汕尾. auto_ptr ...

  10. 00-IT人士必去的10个网站

    IT人士必去的10个网站 1.Chinaunix 网址:http://www.chinaunix.net/ 简介:中国最大的linux/unix技术社区. 2.ITPub 网址:http://www. ...