您的位置:首页 > 职场人生

当智力游戏遇到程序员(一 )三皇后

2015-06-23 22:59 627 查看
手机上下载了一个智力游戏的软件,里面界面还不错,所以就留下来一直在玩。但其中有一些关卡确实很难,我有时也会跳过。但是突然就有个想法,想通过写写这些游戏的破解方案来锻炼自己写代码的能力,然后发现还附带了一些装B的能力,简直让我沉溺其中,所以可能会不断更新哦。这一次,先写一下国际象棋中三皇后的小游戏的破解。

游戏规则

在下面的棋盘中放置三个皇后棋子,让所有空白方块都可以被攻击,就这么简单。



破解思路

其实看到这关的时候,我连国际象棋怎么玩都不知道。但是通过上网查阅资料,发现皇后的攻击范围是:其所在的行,列,及两条对角线,一般来讲,就是其身边的八个方向。

于是乎,我就有了一点点想法,我要在这个 8*8 的棋盘的 64 个方块中随意选出三个方块来进行判断,也就是从 64 取 3 种可能性,再在这 7410 种情况中进行判断,找出符合游戏规则的解。虽然人脑来进行这么庞大的运算是不太可能的,但是程序完全可以啊,也就有了解决方案。

具体实现

在我的程序中,除了构造方法和main方法外,主要的方法有三个:一个用来将皇后身边的八个方向的空格全都“杀掉”,一个用来遍历出那 7410 种可能性,还有一个用来进行判断,满足条件则输出这三个皇后的位置。

程序如下:

public class ChessGame {
public static final int N=6;
public static final int Q=3;
public static int [] rowQ = new int[Q];
public static int [] colQ = new int[Q];
public static int [][] chessBoard = new int

;

public ChessGame(){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
chessBoard[i][j]=0;
}
}
}
public void queenKill(int x, int y){
//走出八个方向
int row=x, col=y;
int x1=x, y1=y;
int x2=x, y2=y;
int x3=x, y3=y;
int x4=x, y4=y;
for(int i=0; i<N; i++){//行
chessBoard[i][col]=1;
}
for(int j=0; j<N; j++){//列
chessBoard[row][j]=1;
}
while(x1>=0&&y1>=0){//左上角对角线
chessBoard[x1][y1]=1;
x1--;
y1--;
}
while(x2>=0&&y2<=5){//右上角对角线
chessBoard[x2][y2]=1;
x2--;
y2++;
}
while(x3<=5&&y3<=5){//右下角对角线
chessBoard[x3][y3]=1;
x3++;
y3++;
}
while(x4<=5&&y4>=0){//左上角对角线
chessBoard[x4][y4]=1;
x4++;
y4--;
}

}

public void findQueens(){//判断的方案
//		int count = 1;
for(int i0=0; i0<N; i0++){//三个皇后可能的落点
for(int j0=0; j0<N; j0++){
if(i0==5&&j0>3) break;
rowQ[0] = i0;
colQ[0] = j0;

int j1=j0+1;
for(int i1=i0;i1<N;i1++){
while(j1<N){
if(i1==5&&j1>4) break;
rowQ[1] = i1;
colQ[1] = j1;

int j2=j1+1;
for(int i2=i1;i2<N;i2++){
while(j2<N){
if(i2==5&&j0>5) break;
rowQ[2] = i2;
colQ[2] = j2;
//								System.out.print(count+"  ");
//								count++;
// 必须先初始化
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
chessBoard[i][j]=0;
}
}
for(int k=0; k<3; k++){
queenKill(rowQ[k], colQ[k]);
//									System.out.print(rowQ[k]);
//									System.out.print(colQ[k]);
//									System.out.print("  ");
}
//								System.out.println();
//								for(int i=0; i<N; i++){
//								for(int j=0; j<N; j++){
//									System.out.print(chessBoard[i][j]);
//									System.out.print("  ");
//								}
//								System.out.println();
//							}
solveTheProblem();
j2++;
}
j2=0;
}
j1++;
}
j1=0;

}
}
}
}

public void solveTheProblem(){
boolean b=true;
for(int iSTP=0; iSTP<N; iSTP++){
for(int jSTP=0; jSTP<N; jSTP++){
if(chessBoard[iSTP][jSTP]==0) {
b=false;
return;
}
}
}
if(b){
for(int k=0; k<3; k++){//输出所要的三个皇后
System.out.print(rowQ[k]);
System.out.print(colQ[k]);
System.out.print("  ");
}
System.out.println();
}
}

public static void main(String[] args) {
ChessGame cg = new ChessGame();
//		queenKill(0, 0);
//		queenKill(0, 1);
//		queenKill(2, 0);
//		for(int i=0; i<N; i++){
//			for(int j=0; j<N; j++){
//				System.out.print(chessBoard[i][j]);
//				System.out.print("  ");
//			}
//			System.out.println();
//		}
cg.findQueens();
}

}
程序跑出来是这个样子:



于是乎,游戏成功破解(下面分别是第二种和第三种解法):



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: