CODEVS 1004 四子连棋
来源:互联网 发布:中国农业大学网络教育 编辑:程序博客网 时间:2024/05/15 03:25
题目描述 Description
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
● ○ ● 空格
○ ● ○ ●
● ○ ● ○
○ ● ○ 空格
输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description
用最少的步数移动到目标棋局的步数。
没什么好说的就是爆搜呗….
对于每一个状态我们记录是白走还是黑走,记录空格的位置,枚举交换,然后hash判重。
我们需要BFS两遍,分别是白先和黑先,取步数的min
#include<cstring>#include<string>#include<cstdio>#include<queue> #include<map>using namespace std;struct meico{ int d; int kx1,ky1; int kx2,ky2; bool flag; int qp[6][6];};map<string,bool>used;int xx[]={0,0,1,0,-1};int yy[]={0,1,0,-1,0};int dt[6][6];char hahaa[6];queue <meico> q;int ans=0;bool check(meico ha){ for(int i=1;i<=4;i++) if(ha.qp[i][1]==ha.qp[i][2]&&ha.qp[i][1]==ha.qp[i][3]&&ha.qp[i][1]==ha.qp[i][4]) return true; for(int i=1;i<=4;i++) if(ha.qp[1][i]==ha.qp[2][i]&&ha.qp[1][i]==ha.qp[3][i]&&ha.qp[1][i]==ha.qp[4][i]) return true; if(ha.qp[1][1]==ha.qp[2][2]&&ha.qp[1][1]==ha.qp[3][3]&&ha.qp[1][1]==ha.qp[4][4]) return true; if(ha.qp[1][4]==ha.qp[2][3]&&ha.qp[1][4]==ha.qp[3][2]&&ha.qp[1][4]==ha.qp[4][1]) return true; return false;}int bfs(){ while(!q.empty()) {// printf("%d\n",q.size()); meico hah=q.front(); q.pop(); if(check(hah)) { return hah.d; } bool f=hah.flag; int x1=hah.kx1,y1=hah.ky1; int x2=hah.kx2,y2=hah.ky2; for(int i=1;i<=4;i++) { int x=x1+xx[i],y=y1+yy[i]; if(f) { if(hah.qp[x][y]==1)//白走 { meico push=hah; swap(push.qp[x][y],push.qp[x1][y1]); string a=""; for(int j=1;j<=4;j++) for(int k=1;k<=4;k++) a+=push.qp[j][k]-'0'; if(!used[a]) { used[a]=1; push.kx1=x,push.ky1=y; push.d++; push.flag=0; q.push(push); } } } else { if(hah.qp[x][y]==2)//黑走 { meico push=hah; swap(push.qp[x][y],push.qp[x1][y1]); string a=""; for(int j=1;j<=4;j++) for(int k=1;k<=4;k++) a+=push.qp[j][k]-'0'; if(!used[a]) { used[a]=1; push.kx1=x,push.ky1=y; push.d++; push.flag=1; q.push(push); } } } } for(int i=1;i<=4;i++) { int x=x2+xx[i],y=y2+yy[i]; if(f) { if(hah.qp[x][y]==1) { meico push=hah; swap(push.qp[x][y],push.qp[x2][y2]); string a=""; for(int j=1;j<=4;j++) for(int k=1;k<=4;k++) a+=push.qp[j][k]-'0'; if(!used[a]) { used[a]=1; push.kx2=x,push.ky2=y; push.d++; push.flag=0; q.push(push); } } } else { if(hah.qp[x][y]==2) { meico push=hah; swap(push.qp[x][y],push.qp[x2][y2]); string a=""; for(int j=1;j<=4;j++) for(int k=1;k<=4;k++) a+=push.qp[j][k]-'0'; if(!used[a]) { used[a]=1; push.kx2=x,push.ky2=y; push.d++; push.flag=1; q.push(push); } } } } }}int main(){ for(int i=1;i<=4;i++) { scanf("%s",hahaa); for(int j=0;j<4;j++) { if(hahaa[j]=='B') dt[i][j+1]=1; else if(hahaa[j]=='W') dt[i][j+1]=2; else dt[i][j+1]=0; } } meico hah; hah.d=0,hah.flag=0; bool flag=1; for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { hah.qp[i][j]=dt[i][j]; if(!dt[i][j]&&flag) { hah.kx1=i,hah.ky1=j; flag=0; continue; } if(!dt[i][j]) hah.kx2=i,hah.ky2=j; } } q.push(hah); ans=bfs(); while(!q.empty()) q.pop(); hah.flag=1; used.clear(); q.push(hah); ans=min(ans,bfs()); printf("%d\n",ans); return 0;}
0 0
- 【codevs 1004】四子连棋
- 【CodeVS 1004】四子连棋
- CODEVS 1004 四子连棋
- Codevs 1004 四子连棋
- CodeVS 1004 四子连棋
- codevs 1004 四子连棋
- codevs 1004 四子连棋
- codevs 1004 四子连棋
- Codevs 1004 四子连棋
- 【codevs 1004】四子连棋
- 【codevs 1004】四子连棋
- codevs 1004 四子连棋(极难)
- codevs 1004 四子连棋 迭代加深搜索
- Codevs P1004 四子连棋
- codevs天梯 四子连棋
- codevs四子连棋--搜索
- Codevs
- Luogu 2346/codevs 1004(IDDFS)
- java笔记004之 简单计算器
- js formData对象
- 动态网站加速,CDN义不容辞
- OSG 官方文档
- ROS-----rostest tool使用
- CODEVS 1004 四子连棋
- Hadoop YARN架构设计要点
- recovery如何传递启动参数
- glOrtho的理解
- 离散余弦变换DCT
- 【Python_Demo_1】使用Python实现C语言中sprintf()格式化字符串写入函数
- PointsDist.js
- SpringDataJPA 查询封装
- Android开发-Retrofit-AndroidStudio(四)simplexml解析XML