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\n",ans);
61     return 0;
62 }
63 
64 int Aptal=AC();
65 int main(){;}

 

转载于:https://www.cnblogs.com/Shy-key/p/7506210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值