题目大意

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

  总体思路很简单,Bfs即可,只是需要注意以下几点:

  • memcmp的返回值不一定是-1, 0, 1,而是<0, =0, >0的某个数。这在windows和linux上的效果不一样。
  • 注意:黑白双方交替走棋。
  • 任意一方都必须走一步。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <cassert>
using namespace std; const int MAX_N = 10;
const int N = 4;
const int Dir[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; struct Node
{
char A[MAX_N][MAX_N];
int Level;
char NextColor; Node()
{
memset(A, 0, sizeof(A));
Level = 0;
} Node operator = (const Node& a)
{
memcpy(A, a.A, sizeof(A));
Level = a.Level;
NextColor = a.NextColor;
return *this;
} bool operator < (const Node& a) const
{
if (NextColor != a.NextColor)
return NextColor == 'B';
else
return memcmp(A, a.A, sizeof(A)) < 0;
} bool operator == (const Node& a) const
{
return NextColor == a.NextColor && memcmp(A, a.A, sizeof(A)) == 0;
} void OPos1(int &oRow1, int &oCol1)
{
for (int row = 1; row <= N; row++)
for (int col = 1; col <= N; col++)
if (A[row][col] == 'O')
{
oRow1 = row;
oCol1 = col;
return;
}
} void OPos2(int &oRow2, int &oCol2)
{
int oRow1, oCol1;
OPos1(oRow1, oCol1);
for (int col = oCol1 + 1; col <= N; col++)
if (A[oRow1][col] == 'O')
{
oRow2 = oRow1;
oCol2 = col;
return;
}
for (int row = oRow1 + 1; row <= N; row++)
for (int col = 1; col <= N; col++)
if (A[row][col] == 'O')
{
oRow2 = row;
oCol2 = col;
return;
}
assert(0);
} bool CanMove1(const int dRow, const int dCol)
{
int oRow1, oCol1;
OPos1(oRow1, oCol1);
int nextRow = oRow1 + dRow, nextCol = oCol1 + dCol;
return A[nextRow][nextCol] == NextColor && nextRow <= N && nextRow >= 1 && nextCol <= N && nextCol >= 1;
} Node GetMove1(int dRow, int dCol)
{
int oRow1, oCol1;
OPos1(oRow1, oCol1);
Node ans = *this;
swap(ans.A[oRow1][oCol1], ans.A[oRow1 + dRow][oCol1 + dCol]);
return ans;
} bool CanMove2(const int dRow, const int dCol)
{
int oRow2, oCol2;
OPos2(oRow2, oCol2);
int nextRow = oRow2 + dRow, nextCol = oCol2 + dCol;
return A[nextRow][nextCol] == NextColor && nextRow <= N && nextRow >= 1 && nextCol <= N && nextCol >= 1;
} Node GetMove2(int dRow, int dCol)
{
int oRow2, oCol2;
OPos2(oRow2, oCol2);
Node ans = *this;
swap(ans.A[oRow2][oCol2], ans.A[oRow2 + dRow][oCol2 + dCol]);
return ans;
} bool Ok()
{
for (int row = 1; row <= N; row++)
{
char st = A[row][1];
bool ok = true;
for (int col = 2; col <= N; col++)
if (A[row][col] != st)
{
ok = false;
break;
}
if (ok)
return true;
}
for (int col = 1; col <= N; col++)
{
char st = A[1][col];
bool ok = true;
for (int row = 2; row <= N; row++)
if (A[row][col] != st)
{
ok = false;
break;
}
if (ok)
return true;
}
char st = A[1][1];
bool ok = true;
for (int i = 2; i <= N; i++)
if (A[i][i] != st)
{
ok = false;
break;
}
if (ok)
return true;
st = A[1][N];
ok = true;
for (int row = 2, col = N - 1; row <= N; row++, col--)
if (A[row][col] != st)
{
ok = false;
break;
}
return ok;
}
};
Node Start; int Bfs()
{
static queue<Node> q;
static set<Node> cache;
Node s1 = Start, s2 = Start;
s1.NextColor = 'B';
s2.NextColor = 'W';
q.push(s1);
q.push(s2);
cache.insert(s1);
cache.insert(s2);
while (!q.empty())
{
Node cur = q.front();
q.pop();
if (!(cur == s1 || cur == s2) && cur.Ok())
return cur.Level;
for (int i = 0; i < 4; i++)
{
if (cur.CanMove1(Dir[i][0], Dir[i][1]))
{
Node next = cur.GetMove1(Dir[i][0], Dir[i][1]);
next.NextColor = (cur.NextColor == 'B' ? 'W' : 'B');
if (!cache.count(next))
{
next.Level = cur.Level + 1;
cache.insert(next);
q.push(next);
}
}
if (cur.CanMove2(Dir[i][0], Dir[i][1]))
{
Node next = cur.GetMove2(Dir[i][0], Dir[i][1]);
next.NextColor = (cur.NextColor == 'B' ? 'W' : 'B');
if (!cache.count(next))
{
next.Level = cur.Level + 1;
cache.insert(next);
q.push(next);
}
}
}
}
return -1;
} int main()
{
for (int i = 1; i <= 4; i++)
scanf("%s", Start.A[i] + 1);
printf("%d\n", Bfs());
return 0;
}

  

luogu2346 四子连棋的更多相关文章

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

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

  2. Codevs p1004 四子连棋

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

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

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

  4. codevs 1004 四子连棋

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

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

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

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

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

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

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

  8. codevs1004四子连棋

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

  9. P2346 四子连棋

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

随机推荐

  1. Alpha项目测试

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/3338 这个作业要求在哪里 htt ...

  2. HDU多校Round 6

    Solved:2 rank:452 I. Werewolf 没有铁人 找铁狼 如果一个环中只有一条狼人边那个人就是铁狼 说铁狼是好人的人也是铁狼 #include <bits/stdc++.h& ...

  3. linux cp复制文件 直接覆盖

    命令: \cp -rf aaaa/* bbbb 复制aaa下的文件到bbb目录

  4. Leetcode 498:对角线遍历Diagonal Traverse(python3、java)

    对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. Given a matrix of M x N elemen ...

  5. 洛谷——P1342 请柬

    P1342 请柬 题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计划.许多学 ...

  6. 使用scrapy 爬取酷狗音乐歌手及歌曲名并存入mongodb中

    备注还没来得及写,共爬取八千多的歌手,每名歌手平均三十首歌曲算,大概二十多万首歌曲 run.py #!/usr/bin/env python # -*- coding: utf-8 -*- __aut ...

  7. LINUX-字符设置和文件格式转换

    dos2unix filedos.txt fileunix.txt 将一个文本文件的格式从MSDOS转换成UNIX unix2dos fileunix.txt filedos.txt 将一个文本文件的 ...

  8. 51nod1128 正整数分组V2

    [题解] 二分一个最大值,check一下分出来的组数是否小于等于k即可. #include<cstdio> #include<algorithm> #define LL lon ...

  9. SCOI2010第一场

    NOI2010全国青少年信息学奥林匹克竞赛 四川代表队选拔赛 第一场 题目名称 幸运数字 游戏 股票交易 英文代号 luckynumber game trade 时限 2秒 2秒 2秒 输入文件 lu ...

  10. Ubuntu 16.04错误:The update information is outdated this may be caused by network...的问题解决

    说明:这个问题没有最终的解决方案,只有不断的尝试. 错误: The update information is outdated this may be caused by network probl ...