當前位置:首頁 » 操作系統 » 圖的演算法實現

圖的演算法實現

發布時間: 2024-10-21 00:05:44

『壹』 九宮圖演算法的實現步驟

第一步:菱形斜填寫
第二步:菱形四角的3和7,1和9交換,如下圖
第三步:9和1插隊進去,如圖

先將1—9九個數按如下圖排列
1
4 b 2
7 c 5 a 3
8 d 6
9
然後將a用7代替,同理1換d,3換c,9換昌咐弊b
便可得如下排列:
4 9 2
3 5 7
8 1 6
此方法也可推導至所有的基數的平方宮圖進行排列。
方法2:以中下格為起點,按右下為方向順序填寫(想像格子上下相連,左右相連),遇到右耐族下格已佔,填入正上方格內。
以25格為例: 11↘ 18↘ 25 2↘ 9↘
10填左方簡型 10↑
11填上方 12↘ 19↘ 21↘ 3↘
4填左方 4↘ 6↘ 13↘ 20↑ 11填上方 22↘
23填左方 23↘ 5↑
6填上方 7↘ 14↘ 16↘
17填左方 17↘
18填上方 24↘
25填上方 1↘
2填上方 8↘
9填上方 15↑ 16填上方

『貳』 九宮圖演算法的演算法實現

// 九宮圖演算法;
//////////////////////////////////////
#include<sio.h>
#include<time.h>
#include<slib.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;
}
///////////////////////////////////////
/// 建立一個3*3數組,初值都設為10;//
//////////////////////////////////////
void create(int array[][3])
{
printf(" *********************************** ");
printf("九宮圖演算法實現過程 ");
printf("*********************************** ");
int line;
int row;
for(line=0;line<3;line )
{
for(row=0;row<3;row )
{
array[line][row]=10;
}
}
// set_value(array);
//show(array);
}
/////////////////////////////////////////
/// 顯示數組狀態 ////
////////////////////////////////////////
void show(int array[][3])
{
for(int i=0;i<3;i )
{
for(int j=0;j<3;j )
{
printf("=",array[i][j]);
}
printf(" ");
}
}
///////////////////////////////
/// 產生數組的初始狀態 ///////
///////////////////////////////
void set_value(int array[][3])
{
int i=0;
int rand_num_line;
int rand_num_row;
printf(" 九宮圖的初始值為: ");
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!! ");
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;line<3;line )
{
for(row=0;row<3;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(" the last diagram is : ");
show(array);
}
////////////////////////////////////
///judge the x1 is 1 or no! ///////
////////////////////////////////////
void judge_x1(int array[3][3])
{
//int x1=1;
int temp;
//printf(" the array[0][2]=%d ",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(" array[0][2]=0 ");
temp=array[0][2];
array[0][1]=array[0][2];
array[0][2]=temp;
judge_x1(array);
goto tt;
}
else /// x3==1;
{ //printf(" array[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位置後,九宮圖為: ");

熱點內容
phpshell腳本 發布:2024-10-21 07:33:10 瀏覽:480
安卓手機的圖標如何變成蘋果的 發布:2024-10-21 07:33:10 瀏覽:428
我的世界伺服器顏色 發布:2024-10-21 07:31:06 瀏覽:538
c語言字元串中查找字元 發布:2024-10-21 07:29:02 瀏覽:952
蘋果5s的id密碼是多少 發布:2024-10-21 07:28:13 瀏覽:734
優酷會員卡密碼怎麼來 發布:2024-10-21 07:11:11 瀏覽:283
c51經過編譯還是匯編運行 發布:2024-10-21 07:07:34 瀏覽:326
華為演算法倉庫 發布:2024-10-21 07:06:56 瀏覽:716
阿宗訪問 發布:2024-10-21 06:54:02 瀏覽:686
eclipse如何源碼下載 發布:2024-10-21 06:53:55 瀏覽:215