📄 sweepmineconfuse.java
字号:
/*
* @(#)SweepMineConfuse.java 1.0 03/08/22
* Copyright 2003 Entao Zhang, All rights reserved.
*/
/**
* 扫雷的基本数据生成算法.会放置有雷为-1,无雷则放置周围的雷数.
*/
public class SweepMineConfuse extends ConfuseArray {
private int swap=0,temp=0;
public int runConfuse(int[] grids, int x, int y){
return runConfuse(grids, x, y, x*y/5);
}
//对于任一个有雷的grid,对其周围8个grids中所有不是雷的grid加1.
public int runConfuse(int[] grids, int x, int y, int num){
if (num==0) num=x*y/5;
int arrayLength=x*y;
if (arrayLength < 2 || arrayLength!=grids.length){
throw new ArrayIndexOutOfBoundsException("SrcArray very small or srcArray's length not equals x*y!!!");
}
for (int i=0; i<arrayLength; i++)
grids[i]=0;
for (int i=0; i<num; i++)
grids[i]=-1;
randomConfuse(grids,arrayLength,arrayLength);
int locX,locY;
for (int i=0; i<arrayLength; i++){
if (grids[i]==-1){
locX=i%x; locY=i/x;
if (locY!=0){
swap=i-x;
if (grids[swap]!=-1) grids[swap]++;
if (locX!=0){
swap=i-x-1;
if (grids[swap]!=-1) grids[swap]++;
}
if (locX!=x-1){
swap=i-x+1;
if (grids[swap]!=-1) grids[swap]++;
}
}
if (locY!=y-1){
swap=i+x;
if (grids[swap]!=-1) grids[swap]++;
if (locX!=0){
swap=i+x-1;
if (grids[swap]!=-1) grids[swap]++;
}
if (locX!=x-1){
swap=i+x+1;
if (grids[swap]!=-1) grids[swap]++;
}
}
if (locX!=0){
swap=i-1;
if (grids[swap]!=-1) grids[swap]++;
}
if (locX!=x-1){
swap=i+1;
if (grids[swap]!=-1) grids[swap]++;
}
}
}
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -