【CodeVS 1004】四子连棋
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】四子连棋的更多相关文章
- codevs 1004 四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- CODEVS 1004四子连棋
[题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- CODEVS——T 1004 四子连棋
http://codevs.cn/problem/1004/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- BFS搜索算法应用_Codevs 1004 四子连棋
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...
- 【wikioi】1004 四子连棋
题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
随机推荐
- PHP中的XML解析的5种方法
[前言]不管是桌面软件开发,还是WEB应用,XML无处不在!然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等.假期有空,于是将PHP中的几种XML解析方法总结如下: ...
- Intent官方教程(4)用Intent构造应用选择框
Forcing an app chooser When there is more than one app that responds to your implicit intent, the us ...
- IIC,RS485,RS232各种协议手册更新中
RS485使用手册与指南.pdf RS232协议标准详解.pdf IIC通信协议.pdf 链接:http://pan.baidu.com/s/1ccBtmA 密码:mwj6 IIC,RS485,R ...
- 如何通过Button获取UITableViewCell
发现一个奇怪的问题: 手机(ios7) 2015-06-17 15:11:29.323 ***[1412:60b] [btn superview] = UITableViewCellContent ...
- BZOJ 3532: [Sdoi2014]Lis (最大流)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3532 题意:给出三个数列ABC,长度均为n.删除A中的某些数字,使得A的最长上升子 ...
- 5.4.1 termios结构,关闭回显功能,一键入字符fgetc立刻返回,不用按下回车键
Linux提供了一组编程接口,用来控制终端驱动程序的行为.这样我们可以更精细的来控制终端. 例如: 回显:允许控制字符的回显,例如读取密码时. 使用termios结构的密码程序 #include &l ...
- Metasploit辅助模块
msf > show auxiliary Auxiliary ========= Name Di ...
- 浏览器被hao.360.cn劫持怎么办
特么的现在互联网太没节操了,一大早发现我的浏览器被hao.360.cn劫持了,弄了好久都没弄好,后来一想可能是因为qvod的原因,这可是哥当年看片的神器啊…… 废话不说: 1,进入:C:\Progra ...
- SqlServer 存储过程分页
适用于2005以上版本 create procedure [dbo].[SP_GetPageList] ( @columns nvarchar(max), --查询字段 @tablename nvar ...
- [SAP ABAP开发技术总结]程序自己以JOB方式运行
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...