http://blog.csdn.net/u013598409/article/details/43924465

相比于一年半前,代码的掌控能力强了许多。

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;

#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define per(i,a,b) for (int i=(a);i>=(b);i--)

const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};

char s[4][4];

int dep;
map<int,int> mp;

inline int cid(char c)
{
    if (c=='W') return 0;
    else if (c=='B') return 1;
    else if (c=='O') return 2;
}

inline int ST(void)
{
    int sum=0;
    rep(i,0,3)
        rep(j,0,3)
            sum=sum*3+cid(s[i][j]);
    return sum;
}

inline int Check(void)
{
    char t;
    rep(i,0,3)
    {
        t=s[i][0];
        if (t==s[i][1]&&t==s[i][2]&&t==s[i][3]) return 1;
    }

    rep(i,0,3)
    {
        t=s[0][i];
        if (t==s[1][i]&&t==s[2][i]&&t==s[3][i]) return 1;
    }

    t=s[0][0];
    if (t==s[1][1]&&t==s[2][2]&&t==s[3][3]) return 1;

    t=s[3][0];
    if (t==s[2][1]&&t==s[1][2]&&t==s[0][3]) return 1;

    return 0;
}

inline int Legal(int x,int y)
{
    return 0<=x&&x<=3&&0<=y&&y<=3;
}

int DFS(int dir,int tms,int lim)
{
    int st=ST();
    if (mp.count(st)) return 0;
    mp[st]=1;

    if (tms==lim)
    {
        int t=Check();
        return t;
    }

    rep(x,0,3) rep(y,0,3) if (cid(s[x][y])==dir)
        rep(k,0,3)
        {
            int nx=x+dx[k],ny=y+dy[k];
            if (Legal(nx,ny)&&cid(s[nx][ny])==2)
            {
                swap(s[x][y],s[nx][ny]);
                int t=DFS(dir^1,tms+1,lim);
                swap(s[x][y],s[nx][ny]);
                if (t) return 1;
            }
        }
    return 0;
}

int main(void)
{
//  freopen("codevs1004.in","r",stdin);
//  freopen("codevs1004.out","w",stdout);

    rep(i,0,3)
        scanf("%s",s[i]);

    int t1,t2; dep=-1;
    do
    {
        dep++;
        mp.clear();
        t1=DFS(0,0,dep);
        mp.clear();
        t2=DFS(1,0,dep);
    }while (!t1&&!t2);
    printf("%d\n",dep);

    return 0;
}

【CodeVS 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. CODEVS 1004四子连棋

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

  4. Codevs p1004 四子连棋

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

  5. CODEVS——T 1004 四子连棋

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

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

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

  7. 【wikioi】1004 四子连棋

    题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...

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

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

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

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

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

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

随机推荐

  1. 零零碎碎写的shell脚本(二):一键修改网络配置信息脚本

    #!/bin/bash ip_mod=$ mask_mod=$ gw_mod=$ dns_mod=$ hostname_mod=$ hosts=/etc/hosts ifcfg=/etc/syscon ...

  2. C++实现的屏幕截图软件 v1.0

    之前用win32做的屏幕截图软件,使用了好久,非常好用. 在2.0版本中增加了屏幕尺和颜色拾取功能,并且改用屏幕和内存DC双缓冲,彻底消灭了闪屏,可惜代码丢失了 好不容易找到1.0版本的代码,以后再重 ...

  3. HDU2112 HDU Today 最短路+字符串哈希

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. MySQL(四) —— 操作数据表中的记录

    插入记录 INSERT [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... //法二: ...

  5. Scanner

    /* next()与nextLine()区别 next(): 1.一定要读取到有效字符后才可以结束输入. 2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉. 3.只有输入有效字符后才将 ...

  6. CALayer总结(三)

    CPU VS GPU 动画和屏幕上组合的图层实际上被一个单独的进程管理,而不是你的应用程序.这个进程就是所谓的渲染服务.在iOS5和之前的版本是SpringBoard进程(同时管理着iOS的主屏).在 ...

  7. zoj 2107&&hdu 1007最近点对问题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds   ...

  8. Python基础学习笔记(一)入门

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  9. C++中关于new及内存地址的思考

    OJ题刷多了,每次都是直接分配内存,那么,你还记得怎么动态分配内存吗? ———————————————————————————————————— 我们知道,使用malloc/calloc等分配内存的函 ...

  10. js的事件处理程序

    js事件处理程序一般有三种: 1.HTML事件处理程序 <body> <input type="button" value="点击" oncl ...