zoukankan      html  css  js  c++  java
  • CODEVS——T 1004 四子连棋

    http://codevs.cn/problem/1004/

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
     
     
    题目描述 Description

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

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

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

    样例输入 Sample Input

    BWBO
    WBWB
    BWBW
    WBWO

    样例输出 Sample Output

    5

    数据范围及提示 Data Size & Hint

    hi

    迭代加深、用空白格与该移动的格子交换

     1 #include <cstdio>
     2 
     3 char map[6][6];
     4 int ans,x1,x2,y1,y2;
     5 int fx[4]={0,1,0,-1};
     6 int fy[4]={1,0,-1,0};
     7 
     8 #define swap(a,b) {char tmp=a;a=b;b=tmp;}
     9 
    10 bool judge()
    11 {
    12     for(int i=1; i<5; i++)
    13     {
    14         if(map[i][1]==map[i][2]&&map[i][1]==map[i][3]&&map[i][1]==map[i][4]) return 1;
    15         if(map[1][i]==map[2][i]&&map[1][i]==map[3][i]&&map[1][i]==map[4][i]) return 1;
    16     }
    17     if(map[1][1]==map[2][2]&&map[1][1]==map[3][3]&&map[1][1]==map[4][4]) return 1;
    18     if(map[4][1]==map[3][2]&&map[4][1]==map[2][3]&&map[4][1]==map[1][4]) return 1;
    19     return false;
    20 }
    21 bool DFS(int nx1,int ny1,int nx2,int ny2,char pre,int step)
    22 {
    23     if(step>=ans) return judge();
    24     int tx1,tx2,ty1,ty2;
    25     for(int i=0; i<4; ++i)
    26     {
    27         tx1=nx1+fx[i],ty1=ny1+fy[i];
    28         tx2=nx2+fx[i],ty2=ny2+fy[i];
    29         if(tx1>0&&tx1<5&&ty1>0&&ty1<5&&map[tx1][ty1]!=pre)
    30         {
    31             swap(map[nx1][ny1],map[tx1][ty1]);
    32             if(DFS(tx1,ty1,nx2,ny2,(pre=='W'?'B':'W'),step+1)) return 1;
    33             swap(map[nx1][ny1],map[tx1][ty1]);
    34         }
    35         if(tx2>0&&tx2<5&&ty2>0&&ty2<5&&map[tx2][ty2]!=pre)
    36         {
    37             swap(map[nx2][ny2],map[tx2][ty2]);
    38             if(DFS(nx1,ny1,tx2,ty2,(pre=='W'?'B':'W'),step+1)) return 1;
    39             swap(map[nx2][ny2],map[tx2][ty2]);
    40         }
    41     }
    42     return 0;
    43 }
    44 
    45 int AC()
    46 {
    47     for(int i=1; i<5; ++i)
    48     {
    49         scanf("%s",map[i]+1);
    50         for(int j=1; j<5; ++j)
    51           if(map[i][j]=='O')
    52             if(!x1) x1=i,y1=j;
    53             else x2=i,y2=j;
    54     }
    55     for(ans=1; ans<1e7; ++ans)
    56     {
    57         if(DFS(x1,y1,x2,y2,'W',0)) break;
    58         if(DFS(x1,y1,x2,y2,'B',0)) break;
    59     }
    60     printf("%d
    ",ans);
    61     return 0;
    62 }
    63 
    64 int Aptal=AC();
    65 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    Map
    Enumeration输出
    iterator的基本用法
    Annotation整合工厂设计模式
    自定义Annotation
    Annotation
    动态代理设计模式
    静态代理设计模式
    自定义ClassLoader
    获取类的类对象的几种方式
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7506210.html
Copyright © 2011-2022 走看看