九宮圖

創(chuàng)新互聯(lián)致力于做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),成都網(wǎng)站設(shè)計(jì),集團(tuán)網(wǎng)站建設(shè)等服務(wù)標(biāo)準(zhǔn)化,推過(guò)標(biāo)準(zhǔn)化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進(jìn)行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場(chǎng)競(jìng)爭(zhēng)中脫穎而出。 選擇創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!
#includestdio.h
#includetime.h
#includestdlib.h
the function defination
void create(int [][3]);
void show(int [][3]);
void set_value(int [][3]);
void aim_get(int [][3]);
void target(int [][3]);
void judge_x1(int [][3]);
void judge_x2(int [][3]);
void judge_x3(int [][3]);
void judge_x4(int [][3]);
void judge_x5(int [][3]);
void shift_all(int [][3]);
void shift_low_six(int [][3]);
void anti_shift_all(int [][3]);
void shift_low_four(int [][3]);
void last_shift(int [][3]);
void set_x5(int [][3]);
the main function body main()
srand(time(NULL));
int cDiagram[3][3];
create(cDiagram); creat the new array ,set the value are 10;
set_value(cDiagram);
//last_shift(cDiagram);
return 0;
}
建立一個(gè)3*3數(shù)組,初值都設(shè)為10
void create(int array[][3])
{
printf("\n\n**\n\n");
printf("九宮圖算法實(shí)現(xiàn)過(guò)程\n\n");
printf("**\n\n");
int line;
int row;
for(line=0;line3;line++)
{
for(row=0;row3;row++)
{
array[line][row]=10;
}
}
// set_value(array);
//show(array);
}
顯示數(shù)組狀態(tài)
void show(int array[][3])
{
for(int i=0;i3;i++)
{
for(int j=0;j3;j++)
{
printf("%3d",array[i][j]);
}
printf("\n\n");
}
}
產(chǎn)生數(shù)組的初始狀態(tài) void set_value(int array[][3])
{
int i=0;
int rand_num_line;
int rand_num_row;
printf(" \n\n九宮圖的初始值為:\n\n");
while(i=8)
{
rand_num_line=rand()%3;
rand_num_row=rand()%3;
if(array[rand_num_line][rand_num_row]!=i array[rand_num_line][rand_num_row]==10)
{
array[rand_num_line][rand_num_row]=i;
++i;
}
}
show(array);
//printf(" let's begin!!\n");
aim_get(array);
}
judge the initial array get the target or no !
void aim_get(int array[][3])
{
int aim[3][3]={{1,2,3},{8,0,4},{7,6,5}};
int line;
int row;
int judge=0;
for(line=0;line3;line++)
{
for(row=0;row3;row++)
{
if(array[line][row]!=aim[line][row])
{
judge=1;
}
}
}
if(judge==1)
{
judge_x1(array);
}
else
{
target(array);
}
}
the target diagram
void target(int array[][3])
{
printf("\n\n the last diagram is :\n");
show(array);
}
/judge the x1 is 1 or no!
void judge_x1(int array[3][3])
{
//int x1=1;
int temp;
//printf(" \n\n\n the array[0][2]=%d\n\n",array[0][2]);
if(array[0][2]!=1 array[0][2]!=0) // x3!=1 || x3!=0;
{
while(array[0][0]!=1)
{
//printf("i am here!!1");
temp=array[0][0];
array[0][0]=array[0][1];
array[0][1]=array[1][1];
array[1][1]=array[1][2];
array[1][2]=array[2][2];
array[2][2]=array[2][1];
array[2][1]=array[2][0];
array[2][0]=array[1][0];
array[1][0]=temp;
}
}
else
{
if(array[0][2]==0) // x3==0;
{
// printf("\n\n array[0][2]=0\n\n");
temp=array[0][2];
array[0][1]=array[0][2];
array[0][2]=temp;
judge_x1(array);
goto tt;
}
我都看不懂……
/*直接復(fù)制運(yùn)行就可以,每一行的九個(gè)數(shù)字代表一個(gè)九宮格的9個(gè)數(shù)字,從左到右,從上到下*/
import?java.util.ArrayList;
import?java.util.Arrays;
public?class?Test1?{
private?static?ArrayListString?arrangeList?=?new?ArrayListString();?
?public?static?void?main(String[]?args)?{
String?str?=?"123456789";//你要排列組合的字符串
char?list[]?=?str.toCharArray();//將字符串轉(zhuǎn)換為字符數(shù)組
genernateData(list,?0,?list.length?-?1);//參數(shù)為字符數(shù)組和0和字符數(shù)組最大下標(biāo)
int?arr[]=new?int[9];
for(String?str1?:?arrangeList){
for(int?k=0;k9;k++){
arr[k]=Integer.parseInt(str1.substring(k,k+1));
}
if(arr[0]+arr[1]+arr[2]==15arr[3]+arr[4]+arr[5]==15arr[6]+arr[7]+arr[8]==15arr[0]+arr[3]+arr[6]==15arr[1]+arr[4]+arr[7]==15arr[2]+arr[5]+arr[8]==15arr[0]+arr[4]+arr[8]==15arr[2]+arr[4]+arr[6]==15){
System.out.println(Arrays.toString(arr));
}
}
}
?
public?static?void?genernateData(char?list[],?int?k,?int?m)?{
?if?(k??m)?{
?StringBuffer?sb?=?new?StringBuffer();//創(chuàng)建一個(gè)StringBuffer對(duì)象sb
?for?(int?i?=?0;?i?=?m;?i++)?{
?sb.append(list[i]);//循環(huán)將字符數(shù)組值追加到StringBuffer中
?}
?arrangeList.add(sb.toString());
?}?else?{
?for?(int?i?=?k;?i?=?m;?i++)?{
?swapData(list,?k,?i);//將下表為k和i的值調(diào)換位置
?genernateData(list,?k?+?1,?m);
?swapData(list,?k,?i);
?}
?}
}
private?static?void?swapData(char?list[],?int?k,?int?i)?{
char?temp?=?list[k];
list[k]?=?list[i];
list[i]?=?temp;
}
}
int[][]?arr?=?new?int[][]{{1,2,3},{4,5,6},{7,8,9}};
Scanner?input?=?new?Scanner(System.in);
System.out.println("輸入數(shù)字:");
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]);
}
}
}
}
}
}
#include iostream
#include "grid9.h"
using namespace std;
struct notes
{
int n[40];
int top;
}route; // 用于記錄路徑
Grid9 grid; // 九宮格
int minDepth=40; // 記錄最少步驟的步數(shù)
void inputG9();
void findG9(int depth);
int existing(int feature);
void main()
{
route.top=0; // 清空記錄
inputG9(); // 輸入原始數(shù)據(jù)
coutendl"開(kāi)始計(jì)算步驟:"endl;
findG9(0); // 調(diào)用搜索程序
cout"最少需要"minDepth"步"endl; // 輸出最小步驟數(shù)
system("pause");
}
void inputG9()
{
int in[9];
cout"請(qǐng)輸入原始九宮數(shù)據(jù):"endl;
cout"第一行:";
cinin[0]in[1]in[2];
cout"第二行:";
cinin[3]in[4]in[5];
cout"第三行:";
cinin[6]in[7]in[8];
for (int i=0;i9;i++)
grid.setGrid(i,in[i]);
}
void findG9(int depth)
{
int gd=grid.difference();
route.n[route.top++]=gd;
if (gd==0) // 得到一個(gè)解
{
minDepth=depth;
route.top--;
return;
}
if (depth==minDepth) // 已經(jīng)到了最大深度,回溯
{
route.top--;
return;
}
for (int i=0;i12;i++)
{
grid.swap(i); // 交換i位置數(shù)據(jù)
gd=grid.difference();
if (!existing(gd))
findG9(depth+1); // 遞歸查找
grid.swap(i); // 恢復(fù)i位置數(shù)據(jù)
}
route.top--;
}
int existing(int feature)
{
for (int i=route.top-1;i=0;i--)
if (feature==route.n[i])
return 1;
return 0;
}
------解決方案--------------------------------------------------------
Grid9類(lèi)的代碼:
grid9.h
C/C++ codeclass Grid9
{
private:
int grid[9]; // 存儲(chǔ)著九宮狀態(tài)
public:
int readGrid(int x, int y); // 讀出九宮中數(shù)值
int feature(); // 返回當(dāng)前九宮狀態(tài)的特征值
void setGrid(int feature); // 按特征值設(shè)置九宮
void setGrid(int index, int v); // 按索引設(shè)置九宮
int difference(); // 返回當(dāng)前九宮狀態(tài)距離完美九宮的差距,差距為0則意味著已經(jīng)是完美九宮。
void swap(int position); // 交換指定位置上的數(shù)。
};
------解決方案--------------------------------------------------------
為了避免用戶誤以為死機(jī),加一個(gè)進(jìn)程顯示在里面......有點(diǎn)破壞完美的感覺(jué)
C/C++ codevoid findG9(int depth)
{
route.n[route.top++]=grid.feature();
if (grid.difference()==0) // 得到一個(gè)解
{
if (depthminDepth)
cout"最短步驟已經(jīng)下降到"depthendl;
minDepth=depth;
route.top--;
return;
}
if (depth==minDepth) // 已經(jīng)到了最大深度,回溯
{
route.top--;
return;
}
for (int i=0;i12;i++)
{
grid.swap(i); // 交換i位置數(shù)據(jù)
if (!existing(grid.feature()))
findG9(depth+1); // 遞歸查找
grid.swap(i); // 恢復(fù)i位置數(shù)據(jù)
}
route.top--;
}
【轉(zhuǎn)】
/*九宮圖算法說(shuō)明:根據(jù)分析,九宮圖中間一位一定是5,另外關(guān)于中間位置
對(duì)稱的每?jī)蓚€(gè)數(shù)字相加等于10,所以對(duì)于一個(gè)九宮圖只通過(guò)其相鄰位置的數(shù)
字就能計(jì)算出其他所有位置的數(shù)字,而且相鄰位置的數(shù)字必然一個(gè)比5大,一
個(gè)比5小
算法設(shè)計(jì)思想:先根據(jù)選取規(guī)則,從剩下八個(gè)數(shù)里面選一大一小,再使用目
標(biāo)函數(shù)檢測(cè)選取是否正確,如果正確則打印出,否則重新選取,選數(shù)的過(guò)程
使用回溯法*/
#includestdio.h
int place(int*);
void main(void){/*九宮圖算法*/
int a[9]={1,2,3,4,5,6,7,8,9};
int b[9]={0};/*表示數(shù)字的位置狀態(tài)*/
int j,k,i;
for(k=0;k=3;k++){/*對(duì)于所有比5小的數(shù)*/
b[1]=a[k];/*選一比5小的數(shù)放到第一行第二列*/
for(j=8;j=5;j--){/*對(duì)于所有比5大的數(shù)*/
if(k!=(8-j)){/*不選擇前面已選小數(shù)所對(duì)應(yīng)的大數(shù)*/
b[0]=a[j];/*選一比5大的數(shù)放到第一行第一列*/
if(place(b)){/*若得到一解,則打印出*/
printf("\n 九宮圖\n\n");
for(i=0;i=8;i++){
printf(" %d ",b[i]);
if((i+1)%3==0){
printf("\n\n\n");
}
}
getch();
exit(0);
}
} /* 回溯到上一級(jí)*/
} /*回溯到根一級(jí) */
}
}
int place(int*p){ /*計(jì)算出其他位置的數(shù)字并檢測(cè)是否為所求的解*/
p[2]=15-p[0]-p[1];
p[4]=5;
p[6]=10-p[2];
p[3]=15-p[0]-p[6];
p[5]=10-p[3];
p[7]=10-p[1];
p[8]=10-p[0];
if(p[2]+p[5]+p[8]==p[6]+p[7]+p[8]){/*其他行和列已通過(guò)計(jì)算檢測(cè)了,只有第三行,第三列未檢測(cè)*/
return(1);
}
else{
return(0);
}
}
【轉(zhuǎn)】
///////////////////////////////////////
// 九宮圖算法;
//////////////////////////////////////
#includesio.h
#includetime.h
#includeslib.h
//////////////////////////////////
//// the function defination
//////////////////////////////////
void create(int [][3]);
void show(int [][3]);
void set_value(int [][3]);
void aim_get(int [][3]);
void target(int [][3]);
void judge_x1(int [][3]);
void judge_x2(int [][3]);
void judge_x3(int [][3]);
void judge_x4(int [][3]);
void judge_x5(int [][3]);
void shift_all(int [][3]);
void shift_low_six(int [][3]);
void anti_shift_all(int [][3]);
void shift_low_four(int [][3]);
void last_shift(int [][3]);
void set_x5(int [][3]);
///////////////////////////////////////
////// the main function body ////
////////////////////////////////////////
main()
{
srand(time(NULL));
int cDiagram[3][3];
create(cDiagram); /////// creat the new array ,set the value are 10;
set_value(cDiagram);
//last_shift(cDiagram);
return 0;
}
///////////////////////////////////////
/// 建立一個(gè)3*3數(shù)組,初值都設(shè)為10;//
//////////////////////////////////////
void create(int array[][3])
{
printf("\n\n***********************************\n\n");
printf("九宮圖算法實(shí)現(xiàn)過(guò)程\n\n");
printf("***********************************\n\n");
int line;
int row;
for(line=0;line3;line )
{
for(row=0;row3;row )
{
array[line][row]=10;
}
}
// set_value(array);
//show(array);
}
/////////////////////////////////////////
/// 顯示數(shù)組狀態(tài) ////
////////////////////////////////////////
void show(int array[][3])
{
for(int i=0;i3;i )
{
for(int j=0;j3;j )
{
printf("=",array[i][j]);
}
printf("\n\n");
}
}
///////////////////////////////
/// 產(chǎn)生數(shù)組的初始狀態(tài) ///////
///////////////////////////////
void set_value(int array[][3])
{
int i=0;
int rand_num_line;
int rand_num_row;
printf(" \n\n九宮圖的初始值為:\n\n");
while(i=8)
{
rand_num_line=rand()%3;
rand_num_row=rand()%3;
if(array[rand_num_line][rand_num_row]!=i array[rand_num_line][rand_num_row]==10)
{
array[rand_num_line][rand_num_row]=i;
i;
}
}
show(array);
//printf(" let's begin!!\n");
aim_get(array);
}
////////////////////////////////////////////////////////
//// judge the initial array get the target or no ! ///
//////////////////////////////////////////////////////////
void aim_get(int array[][3])
{
int aim[3][3]={{1,2,3},{8,0,4},{7,6,5}};
int line;
int row;
int judge=0;
for(line=0;line3;line )
{
for(row=0;row3;row )
{
if(array[line][row]!=aim[line][row])
{
judge=1;
}
}
}
if(judge==1)
{
judge_x1(array);
}
else
{
target(array);
}
}
/////////////////////////////////////
/////// the target diagram //////////
/////////////////////////////////////
void target(int array[][3])
{
printf("\n\n the last diagram is :\n");
show(array);
}
////////////////////////////////////
///judge the x1 is 1 or no! ///////
////////////////////////////////////
void judge_x1(int array[3][3])
{
//int x1=1;
int temp;
//printf(" \n\n\n the array[0][2]=%d\n\n",array[0][2]);
if(array[0][2]!=1 array[0][2]!=0) // x3!=1 || x3!=0;
{
while(array[0][0]!=1)
{
//printf("i am here!!1");
temp=array[0][0];
array[0][0]=array[0][1];
array[0][1]=array[1][1];
array[1][1]=array[1][2];
array[1][2]=array[2][2];
array[2][2]=array[2][1];
array[2][1]=array[2][0];
array[2][0]=array[1][0];
array[1][0]=temp;
}
}
else
{
if(array[0][2]==0) // x3==0;
{
// printf("\n\n array[0][2]=0\n\n");
temp=array[0][2];
array[0][1]=array[0][2];
array[0][2]=temp;
judge_x1(array);
goto tt;
}
else /// x3==1;
{ //printf("\n\narray[0][2] should is 1, %d",array[0][2]);
if(array[1][1]==0) //// x0==0;
{
temp=array[0][1];
array[0][1]=array[1][1];
array[1][1]=temp;
judge_x1(array);
}
else //// x3==1 x0!=0;
{
shift_all(array);
judge_x1(array);
}
}
}
printf(" 確定了X1位置后,九宮圖為:\n");
網(wǎng)站名稱:九宮圖數(shù)讀java代碼的簡(jiǎn)單介紹
URL分享:http://chinadenli.net/article12/dsepjgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、定制網(wǎng)站、網(wǎng)站制作、自適應(yīng)網(wǎng)站、面包屑導(dǎo)航、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)