题目链接

算法:BFS

//2014-02-05更新

*******************************2013-10-15*******************************

PS:被卡过2天。日期:2013-10-14 ~ 2013-10-15 17:28:21

此题卡了我一天,原因是宽搜时方向搞错= =(汗= =),而这个错误我竟然检查了好久= =!!

(大神不要笑,我用来超多的代码来实现= =是别人提交代码的5倍以上,,,200多行= =)

2014.01.02 PS: 我承认我是脑残了,现在已会写bfs,改天有时间修改下,下面这个程序很好的证明了我不会搜索= =

#include <iostream>
#include <cstring>
#include <queue> using namespace std;
enum pa{NUL, LEFT, UP, RIGHT, DOWN}; //四个方向
enum color{O, W, B}; //白色用W表示,黑色用B表示,NUL就表示空格
struct st
{
color m[4][4];
short d; //次数
short x1, y1, x2, y2; //空格的位置
bool who; //0表示到白棋走,1表示黑棋走
pa p1, p2; //空格之前所在的位置,即以本格子来看从哪个位置是移动之前的状态
st& operator= (const st& s)
{
memcpy(m, s.m, sizeof(m));
d=s.d; who=s.who;
p1=s.p1; p2=s.p2;
x1=s.x1; x2=s.x2;
y1=s.y1; y2=s.y2;
return *this;
}
st(){memset(m, 0, sizeof(m)); d=0; x1=x2=y1=y2=-1; p1=p2=NUL; who=1;}
}state;
queue<st> q;
int i, j; void readin()
{
char chartemp;
for(i = 0; i < 4; i++) for(j = 0; j < 4; j++)
{
cin >> chartemp;
if(chartemp == 'W') state.m[i][j] = W;
else if(chartemp == 'B') state.m[i][j] = B;
else
{
state.m[i][j] = O;
if(state.x1 == -1)
state.x1 = i, state.y1 = j;
else
state.x2 = i, state.y2 = j;
}
}
} void chu()
{
st t = state;
if(t.x1+1<4 && t.m[t.x1+1][t.y1] != O) //第一个空格 向下移
{
t.p1 = UP; t.p2 = NUL;
t.x1++; t.m[t.x1-1][t.y1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++;
t.who = t.m[t.x1-1][t.y1]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.x2+1<4 && t.m[t.x2+1][t.y2] != O) //第二个空格 向下移
{
t.p2 = UP; t.p1 = NUL;
t.x2++; t.m[t.x2-1][t.y2] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++;
t.who = t.m[t.x2-1][t.y2]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.y1+1<4 && t.m[t.x1][t.y1+1] != O) //第一个空格 向右移
{
t.p1 = LEFT; t.p2 = NUL;
t.y1++; t.m[t.x1][t.y1-1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++;
t.who = t.m[t.x1][t.y1-1]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.y2+1<4 && t.m[t.x2][t.y2+1] != O) //第二个空格 向右移
{
t.p2 = LEFT; t.p1 = NUL;
t.y2++; t.m[t.x2][t.y2-1] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++;
t.who = t.m[t.x2][t.y2-1]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.x1 > 0 && t.m[t.x1-1][t.y1] != O) //第一个空格 向上移
{
t.p1 = DOWN; t.p2 = NUL;
t.x1--; t.m[t.x1+1][t.y1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++;
t.who = t.m[t.x1+1][t.y1]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.x2 > 0 && t.m[t.x2-1][t.y2] != O) //第二个空格 向上移
{
t.p2 = DOWN; t.p1 = NUL;
t.x2--; t.m[t.x2+1][t.y2] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++;
t.who = t.m[t.x2+1][t.y2]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.y1 > 0 && t.m[t.x1][t.y1-1] != O) //第一个空格 向左移
{
t.p1 = RIGHT; t.p2 = NUL;
t.y1--; t.m[t.x1][t.y1+1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++;
t.who = t.m[t.x1][t.y1+1]-1; t.who = !t.who;
q.push(t);
}
t = state;
if(t.y2 > 0 && t.m[t.x2][t.y2-1] != O) //第二个空格 向左移
{
t.p2 = RIGHT; t.p1 = NUL;
t.y2--; t.m[t.x2][t.y2+1] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++;
t.who = t.m[t.x2][t.y2+1]-1; t.who = !t.who;
q.push(t);
}
} bool check(const st& s)
{
int x, y; pa p; bool bp = 1;
if(s.p1 != NUL) p = s.p1; else {p = s.p2; bp = 0;}
switch(p)
{
case UP:
x = (bp?s.x1-1:s.x2-1); y = (bp?s.y1:s.y2);
break;
case DOWN:
x = (bp?s.x1+1:s.x2+1); y = (bp?s.y1:s.y2);
break;
case RIGHT:
x = (bp?s.x1:s.x2); y = (bp?s.y1+1:s.y2+1);
break;
case LEFT:
x = (bp?s.x1:s.x2); y = (bp?s.y1-1:s.y2-1);
break;
default:
return 0;
}
if((s.m[x][0]==s.m[x][1]&&s.m[x][1]==s.m[x][2]&&s.m[x][2]==s.m[x][3]) ||
(s.m[0][y]==s.m[1][y]&&s.m[1][y]==s.m[2][y]&&s.m[2][y]==s.m[3][y]) ||
(s.m[0][0]==s.m[1][1]&&s.m[1][1]==s.m[2][2]&&s.m[2][2]==s.m[3][3]) ||
(s.m[0][3]==s.m[1][2]&&s.m[1][2]==s.m[2][1]&&s.m[2][1]==s.m[3][0]) )
return 1;
return 0;
} int main()
{
readin();
chu();
st s, t;
int n = 0; //迭代
while(++n != 10000000)
{
s = q.front(); q.pop();
if(check(s)) break;
t = s;
// 要判断是否这个点是从原来方向移动得到的,如果要向上移,则p域就不能是下面,否则会大大损耗空间和时间
if(t.x1+1<4 && t.p1 != DOWN && t.m[t.x1+1][t.y1] != O && t.m[t.x1+1][t.y1]-1 == t.who) //第一个空格 向下移
{
t.p1 = UP; t.p2 = NUL;
t.x1++; t.m[t.x1-1][t.y1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.x2+1<4 && t.p2 != DOWN && t.m[t.x2+1][t.y2] != O && t.m[t.x2+1][t.y2]-1 == t.who) //第二个空格 向下移
{
t.p2 = UP; t.p1 = NUL;
t.x2++; t.m[t.x2-1][t.y2] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.y1+1<4 && t.p1 != RIGHT && t.m[t.x1][t.y1+1] != O && t.m[t.x1][t.y1+1]-1 == t.who) //第一个空格 向右移
{
t.p1 = LEFT; t.p2 = NUL;
t.y1++; t.m[t.x1][t.y1-1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.y2+1<4 && t.p2 != RIGHT && t.m[t.x2][t.y2+1] != O && t.m[t.x2][t.y2+1]-1 == t.who) //第二个空格 向右移
{
t.p2 = LEFT; t.p1 = NUL;
t.y2++; t.m[t.x2][t.y2-1] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.x1 > 0 && t.p1 != UP && t.m[t.x1-1][t.y1] != O && t.m[t.x1-1][t.y1]-1 == t.who) //第一个空格 向上移
{
t.p1 = DOWN; t.p2 = NUL;
t.x1--; t.m[t.x1+1][t.y1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.x2 > 0 && t.p2 != UP && t.m[t.x2-1][t.y2] != O && t.m[t.x2-1][t.y2]-1 == t.who) //第二个空格 向上移
{
t.p2 = DOWN; t.p1 = NUL;
t.x2--; t.m[t.x2+1][t.y2] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.y1 > 0 && t.p1 != LEFT && t.m[t.x1][t.y1-1] != O && t.m[t.x1][t.y1-1]-1 == t.who) //第一个空格 向左移
{
t.p1 = RIGHT; t.p2 = NUL;
t.y1--; t.m[t.x1][t.y1+1] = t.m[t.x1][t.y1];
t.m[t.x1][t.y1] = O; t.d++; t.who = !t.who;
q.push(t);
}
t = s;
if(t.y2 > 0 && t.p2 != LEFT && t.m[t.x2][t.y2-1] != O && t.m[t.x2][t.y2-1]-1 == t.who) //第二个空格 向左移
{
t.p2 = RIGHT; t.p1 = NUL;
t.y2--; t.m[t.x2][t.y2+1] = t.m[t.x2][t.y2];
t.m[t.x2][t.y2] = O; t.d++; t.who = !t.who;
q.push(t);
}
} cout << s.d << endl;
return 0;
}

*******************************2014-02-05*******************************

算法:BFS+Hash判重

裸BFS后,看了题解,有 Hash判重 知识,可算长姿势了。

Hash判重:将图看成一串数字,我们可以发现,这可以当作n进制的数看待,那么hash就有着落了。。我们将这个n进制数转换为十禁止,再开个数组,就可以hash了

这题的图元素只有3个,那么我们就可以将它看成3进制数来做hash。

注意:做hash的时候的mod要用质数(自己想为什么),因为这个图的长度为16,有7个2,那么经过粗略计算,这个hash的mod在40700000的后面,自己写了个小prime判定,找出了一个质数 40700017,就用它了。

代码:

#include <iostream>
#include <cstring>
using namespace std; struct Map {
int m[4][4], x1, x2, y1, y2;
int ans, who;
Map& operator= (Map& a) { memcpy(m, a.m, sizeof(a.m)); x1=a.x1; x2=a.x2; y1=a.y1; y2=a.y2; ans=a.ans; who=a.who; return *this; }
}q[600009]; //我们模拟队列,开那么多应该够了吧。。不够就用循环队列
Map t; //作为全局temp const int mod = 40700017;
bool Hash[mod]; bool check(int f) {
//判断行和列
for(int i = 0; i < 4; ++i)
if( (q[f].m[i][0]==q[f].m[i][1] && q[f].m[i][1]==q[f].m[i][2] && q[f].m[i][2]==q[f].m[i][3]) ||
(q[f].m[0][i]==q[f].m[1][i] && q[f].m[1][i]==q[f].m[2][i] && q[f].m[2][i]==q[f].m[3][i]) )
return true;
//判断斜边
if( (q[f].m[0][0]==q[f].m[1][1] && q[f].m[1][1]==q[f].m[2][2] && q[f].m[2][2]==q[f].m[3][3]) ||
(q[f].m[0][3]==q[f].m[1][2] && q[f].m[1][2]==q[f].m[2][1] && q[f].m[2][1]==q[f].m[3][0]) )
return true;
return false;
} void swap(int x, int y, int xx, int yy, int w) {
t.m[x][y] = t.m[xx][yy];
t.m[xx][yy] = 0;
if(w == 1) t.x1 = xx, t.y1 = yy;
else t.x2 = xx, t.y2 = yy;
t.who = t.m[x][y];
} bool hash(Map& s) {
//k是3^x次方的结果
int i, j, k = 1, h = 0;
for(i = 0; i < 4; ++i)
for(j = 0; j < 4; ++j) {
h += s.m[i][j] * k;
k *= 3;
}
h %= mod;
if(Hash[h]) return false;
Hash[h] = 1;
return true;
} void bfs() {
int f = 0, l = 1;
int x1, y1, x2, y2;
while(f != l) {
if(check(f)) { cout << q[f].ans; break; }
q[f].ans++;
x1 = q[f].x1; y1 = q[f].y1; x2 = q[f].x2; y2 = q[f].y2;
//8种情况。。我承认可以简略代码的,但是算了。这样看起来壮观 >_<
if(x1 > 0 && q[f].m[x1-1][y1] != q[f].who) { t = q[f]; swap(x1, y1, x1-1, y1, 1); if(hash(t)) q[l++] = t; }
if(x1 < 3 && q[f].m[x1+1][y1] != q[f].who) { t = q[f]; swap(x1, y1, x1+1, y1, 1); if(hash(t)) q[l++] = t; }
if(y1 > 0 && q[f].m[x1][y1-1] != q[f].who) { t = q[f]; swap(x1, y1, x1, y1-1, 1); if(hash(t)) q[l++] = t; }
if(y1 < 3 && q[f].m[x1][y1+1] != q[f].who) { t = q[f]; swap(x1, y1, x1, y1+1, 1); if(hash(t)) q[l++] = t; }
if(x2 > 0 && q[f].m[x2-1][y2] != q[f].who) { t = q[f]; swap(x2, y2, x2-1, y2, 2); if(hash(t)) q[l++] = t; }
if(x2 < 3 && q[f].m[x2+1][y2] != q[f].who) { t = q[f]; swap(x2, y2, x2+1, y2, 2); if(hash(t)) q[l++] = t; }
if(y2 > 0 && q[f].m[x2][y2-1] != q[f].who) { t = q[f]; swap(x2, y2, x2, y2-1, 2); if(hash(t)) q[l++] = t; }
if(y2 < 3 && q[f].m[x2][y2+1] != q[f].who) { t = q[f]; swap(x2, y2, x2, y2+1, 2); if(hash(t)) q[l++] = t; }
f++;
}
} int main() {
char t; bool ok = 1;
for(int i = 0; i < 4; ++i) for(int j = 0; j < 4; ++j) {
cin >> t;
switch(t) {
case 'B': q[0].m[i][j] = 2; break;
case 'W': q[0].m[i][j] = 1; break;
case 'O': q[0].m[i][j] = 0;
if(ok) q[0].x1 = i, q[0].y1 = j, ok = 0;
else q[0].x2 = i, q[0].y2 = j;
break;
}
}
bfs();
return 0;
}

  

【wikioi】1004 四子连棋的更多相关文章

  1. codevs 1004 四子连棋

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

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

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

  3. BFS搜索算法应用_Codevs 1004 四子连棋

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...

  4. CODEVS——T 1004 四子连棋

    http://codevs.cn/problem/1004/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  5. CODEVS 1004四子连棋

    [题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...

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

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

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

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

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

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

  9. codevs1004四子连棋

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

随机推荐

  1. HDU 1708 简单dp问题 Fibonacci String

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 【云计算】基于Ansible的自动部署平台化思路

    目标: 1.自动化—支持命令行.webui.api调用 2.支持基础命令.脚本.复杂任务编排.满足生产环境各类模块自动化部署需求 3.满足生产环境(开发.测试.线上)性能.可靠性.安全性要求 4.流程 ...

  3. Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. AES 推荐文章

    链接如下,写得很好!http://blog.csdn.net/a00553344/article/details/4002507

  5. (转)使用SQLCMD在SQLServer执行多个脚本

    概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司,十几个客 ...

  6. 用php计算行列式

    因为有课程设计要计算多元一次方程组,所以想编个程序实现,多元一次方程组的计算最系统的方法就是利用克拉默法则求解方程组,所以只需要编写一个类或者方法求出多元一次方程组系数行列式的值和和其他几个行列式,如 ...

  7. myeclipse高版本对应tomcat低版本解决办法

    今天在帮同事调试程序的时候,冒出来一个异常,网上搜搜,结果如下: 将项目部署好后,启动tomcat后报错,java.lang.NoClassDefFoundError: org/apache/juli ...

  8. Myeclipse 安装svn插件

    安装subclipse,  SVN插件1.从官网下载site-1.8.22.zip文件  访问不了可点我网盘2.从中解压出features与 plugins文件夹,复制到MyEclipse\MyEcl ...

  9. Cocos2dx实现象棋之布局

    开始界面 #ifndef SCENESTART_H #define SCENESTART_H #include "cocos2d.h" #include "SceneGa ...

  10. struts2中各种值栈问题

    struts2中OGNL和 ValueStack(一) 收藏 学习的时候,总分不清楚在struts2中页面的传值和取值是怎么来完成的,所以从网上搜了很多资料,现在把这些资料总结写,留着以后参考..看完 ...