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. VC++获取计算机Hardware Information (CPU ID, MainBoard Info, Hard Disk Serial, System Information)

    转载:http://blog.csdn.net/yapingxin/article/details/50107799 转载:http://zhidao.baidu.com/link?url=A5K6N ...

  2. 异常详细信息: System.Web.Hosting.HostingEnvironmentException: 访问 IIS 元数据库失败 解决方法

    访问IIS元数据库失败 同理,给操作系统的新建用户赋予IIS操作权限同样可以采用该命令来处理 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错 ...

  3. Eclipse新建工程编译R cannot be resolved to a variable问题

    Eclipse新建工程编译R cannot be resolved to a variable问题 新建工程编译提示R cannot be resolved to a variable 图1 然后打开 ...

  4. GitHub如何在自己的Stars中进行搜索

    默认的搜索结果,仅仅搜索原创的repository 想要搜索的时候,能够搜索到fork出来的repository的话 在关键字后面加上空格 fork:true

  5. PowerShell 4 on win7 sp1

    https://www.microsoft.com/en-hk/download/details.aspx?id=40855 文件太多了,按照这个http://stackoverflow.com/qu ...

  6. FZU 2105 Digits Count(位数计算)

    Description 题目描述 Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation ...

  7. Object-C: 枚举

    摘自:http://coffeeandsandwich.com/?p=8 在 iOS 6 和 Mac OS X 10.8 以前,定义枚举类型的方式如下: typedef enum the_enum_n ...

  8. iOS - OC 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

  9. iOS - Swift Array 数组

    前言 public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContai ...

  10. FFPEG 转码记录------解决了有流,但是没有码率和FPS?

    命令行:(已经测试成功) ffmpeg -i rtmp://localhost/live/S0000_8 -c:v libx264 -b:v 500k -c:a libfdk_aac -b:a 64k ...