java九宫格棋代码 java九宫格程序( 四 )


Sleep(300);
}
char msg[100];
sprintf(msg , "^_^ ! 搞定了!\r\n计算步骤用时%d毫秒" , pGird-m_iTime);
MessageBox(NULL , msg , "~_~" , 0);
pGird-m_bAutoRun = false;
return 0L;
}
最后介绍一下搜索函数的原理,首先得到源数组 , 将其转换成DWORD型,与目标比较,如果相同完成,不同就交换一下数据和空格位置,加入二叉树,搜索下一个结果,直到没有步可走了,在搜索刚刚搜索到的位置的子位置 , 这样直到找到目标结果为止,函数:
bool CNineGird::ComputeFeel()
{
unsigned char *array = m_iChess;
UINT i;
const int MAXSIZE = 362880;
unsigned char temparray[9];
DWORD target , fountain , parent , parentID = 0 , child = 1;
ArrayToDword(m_iTargetChess , target);
ArrayToDword(array , fountain);
if (fountain == target)
{
return false;
}
if (m_pScanbuf != NULL)
{
delete[] m_pScanbuf;
}
m_pScanbuf = new Scanbuf[MAXSIZE];
AddTree(fountain ,m_pPlaceList);
m_pScanbuf[ 0 ].Place = fountain;
m_pScanbuf[ 0 ].ScanID = -1;
clock_t tim = clock();
while(parentIDMAXSIZEchildMAXSIZE)
{
parent = m_pScanbuf[parentID].Place;
for ( i = 0 ; i4 ; i ++) // 0 :UP , 1:Down ,2:Left,3:Right
{
DwordToArray(parent , temparray);
if (MoveChess(temparray,i)) //是否移动成功
{
ArrayToDword(temparray , fountain);
if (AddTree(fountain, m_pPlaceList)) //加入搜索数
{
m_pScanbuf[ child ].Place = fountain;
m_pScanbuf[ child ].ScanID = parentID;
if (fountain == target) //是否找到结果
{
m_iTime = clock() - tim;
GetPath(child);//计算路径
FreeTree(m_pPlaceList);
delete[] m_pScanbuf;
m_pScanbuf = NULL;
return true;
}
child ++;
}
}
} // for i
parentID++;
}
m_iTime = clock() - tim;
FreeTree(m_pPlaceList);
delete[] m_pScanbuf;
m_pScanbuf = NULL;
return false;
}
重要函数的介绍结束;下面是程序的运行结果和运算结果:
用java实现,得到九宫格1-9的任意数,怎么得到这个数周围的数int[][] arr = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
Scanner input = new Scanner(System.in);
System.out.println("输入数字:");
int k = input.nextInt();
for(int i=0;i3;i++){
for(int j=0;j3;j++){
if(arr[i][j]==k){
for(int m=i-1;m=i+1;m++){
for(int n=j-1;n=j+1;n++){
if(m=0m=2n=0n=2){
if(m!=i||n!=j)
System.out.print(arr[m][n]);
}
}
}
}
}
}
java编程九宫格问题要求:根据输入的数字n,如:3,5 , 7...以矩阵显示n行n列数,这些数由1~n*n构成,要求矩阵的每行每列及对角线上n个数之和相等 预备知识: 在距阵中,1在第一行正中,随后的数字应放到上一个数字的右上方方格中,如果向上不行 , 就放到该列的最下方格子;如果向右不行,就放到该行的最左边;如果都不行 , 就放到上一个数字的正下方;如果目标格子中已经有数字 , 也放到上一个数字的正下方 思路: 1) 使用2维数组预备存储1~n*n这些数字 2) 1是放到第一行正中的,所以其索引号是:[0][(n-1)/2] 3) 随后的数字,其索引号原则如下 1 num的行索引为 num-1 的 (行索引-1) , num的列索引为 num-1 的 (列索引+1) 2如果发现num的行,列索引都越位(-1或n),则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 3如果发现num的行,列索引指向的位置已经有数字,则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 4如果发现num的行越位(-1) , 则 num的行索引为n-1 5如果发现num的列越位(n),则 num的列索引为0import java.util.Scanner; public class JiuGong { public static void main(String[] args) { Scanner s = new Scanner(System.in); int x = s.nextInt(); //输入长度 int h = 0; //行 //在距阵中,1在第一行正中 int l = x / 2; //列 int[][] a = new int[x][x]; for (int i = 1; i = x * x; i++) { a[h][l] = i; //运行提示溢出 //随后的数字应放到上一个数字的右上方方格中 h--; l++; //3.如果都不行,就放到上一个数字的正下方 if (h0l = x) { //先返回上一个数字 h++; l--; //再下移一行 h++; } //1.如果向上不行 , 就放到该列的最下方格子 else if (h0) { h = x - 1; } //2.如果向右不行 , 就放到该行的最左边 else if (l = x) { l = 0; } //4.如果目标格子中已经有数字,也放到上一个数字的正下方 else if (a[h][l]0) { //先返回上一个数字 h++; l--; //再下移一行 h++; } } //打印九宫格 for (int j = 0; jx; j++) { for (int k = 0; kx; k++) { System.out.print(a[j][k] + " "); } //换行 System.out.println(); } } }

推荐阅读