8 | 1 | 6 |
3 | 5 | 7 |
4 | 9 | 2 |
17 | 24 | 1 | 8 | 15 |
23 | 5 | 7 | 14 | 16 |
4 | 6 | 13 | 20 | 22 |
10 | 12 | 19 | 21 | 3 |
11 | 18 | 25 | 2 | 9 |
package com.zxf.niuke; import org.junit.Test; /** * 幻方 * */ public class MagicSquare { //奇数阶幻方 /** * 算法:1位于第一行中间,后面数字在之前一个数字右上角,若右上角已有数字则在之前数字下方 * */ public int[][] oddMagicSquare(int n,int start){ if(n%2 == 0) { return null; } int[][] result = new int[n][n]; int row = 0; int col = n/2; for(int i=start;i<=n*n+start-1;i++) { result[row][col] = i; int nextrow = (row+n-1)%n; int nextcol = (col+1)%n; if(result[nextrow][nextcol]!=0) { row = (row+1)%n; }else { row = nextrow; col = nextcol; } } return result; } //双偶数阶幻方n=4k /** * 算法:先构建一个顺序矩阵,再切割成4*4矩阵,每个矩阵的对角线上数字换成其互补的数 * */ public int[][] doubleEvenSquare(int n,int start){ if(n%4!=0) { return null; } int[][] result = new int[n][n]; int value = start; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { result[i][j] = value++; } } //换数 for(int i=0;i<n;i++) { for(int j=i%4;j<n;j+=4) { result[i][j] = n*n+2*start-1 - result[i][j]; } for(int k=(n-1-(i%4));k>=0;k-=4) { result[i][k] = n*n+2*start-1 - result[i][k]; } } return result; } //单偶数阶幻方 n=4k+2 /** * 算法:将矩阵均分为4个矩阵,假设为四个象限 * 用奇幻方算法按1/4/2/3象限初始化4个矩阵拼接成大幻方 * 在第一象限中,标记中心格所在行自中心格(包含中心格)起右侧k格和非中心行的中心格(不包含中心格)左侧k格与第三象限交换 * 在第二象限中,标记中心列左侧(包含中心列)k-1列与第四象限交换,若k-1=0;则不标记 * */ public int[][] singleEvenSquare(int n,int start){ if(n%4!=2) { return null; } int k = (n-2)/4;// int m = n/2;//象限矩阵的阶数 int[][] result = new int[n][n]; //创建4个小矩阵 int[][] A = oddMagicSquare(m, start); int[][] B = oddMagicSquare(m, 2*m*m+start); int[][] C = oddMagicSquare(m, 3*m*m+start); int[][] D = oddMagicSquare(m, m*m+start); //初始化大矩阵 for(int i=0;i<m;i++) { for(int x=0;x<m;x++) { result[i][x] = A[i][x]; } for(int y=m;y<n;y++) { result[i][y] = B[i][y-m]; } } for(int j=m;j<n;j++) { for(int x=0;x<m;x++) { result[j][x] = C[j-m][x]; } for(int y=m;y<n;y++) { result[j][y] = D[j-m][y-m]; } } //标记并交换数 int center = m/2; for(int i =0;i<m;i++) { if(center == i) { for(int j=center;j<k+center;j++) { swap(result, i, j, i+m, j); } }else { for(int j=0;j<k;j++) { swap(result, i, j, i+m, j); } } for(int j=center+m;j>center+m-(k-1);j--) { swap(result, i, j, i+m, j); } } return result; } public int[][] createMagicSquare(int n,int start){ if (n<3) { return null; } if(n%2 == 1) { return oddMagicSquare(n, start); }else if (n%4 == 0) { return doubleEvenSquare(n, start); }else { return singleEvenSquare(n, start); } } public void swap(int[][] a,int x1,int y1,int x2,int y2) { int temp = a[x1][y1]; a[x1][y1] = a[x2][y2]; a[x2][y2] = temp; } //打印幻方 public void printSquare(int[][] a) { if (a==null) { System.out.println("不存在"); } int len = a.length; for(int i=0;i<len;i++) { for(int j=0;j<len;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } //测试类 @Test public void test() { printSquare(createMagicSquare(7,1)); } }
vector<vector<int> > MagicMatrix(int n){ vector<vector<int> >result(n, vector<int>(n, 0)); int count=1; int curi=0, curj=n/2; int nexti=curi, nextj=curj; while (count <= n*n){ if (result[nexti][nextj] == 0){ //还没有填入数据时,向右上方向更新curi和curj的值 curi=nexti; curj=nextj; } else //如果已经填入数据了,则向下更新curi的值 curi=curi+1; result[curi][curj] = count++; //注意,curi,curj坐标值保持不变,计算下一个位置坐标nexti和nextj nexti = curi - 1; nextj = curj + 1; if (nexti<0) nexti = n - 1; //如果向上越界,则向下移 if (nextj>n-1) nextj = 0; //如果向右越界,则向左移 } return result; }
#include<iostream>
#include<vector>
using namespace std;
/** 生成矩阵虽然顺序不一样,但是同样满足要求*/ //生成矩阵函数
int **createMatrix(const int n){ int **matrix; int num_matrix = n * n; matrix = new int*[n]; //动态生成 n*n个元素的二维数组 for (int i = 0; i < n; i++) matrix[i] = new int[n]; //memset(matrix, 0, sizeof(matrix)); for (int i = 0; i< n; ++i) for (int j = 0; j < n; ++j) matrix[i][j] = 0; //对数组进行赋值 int first = 0, second = n / 2; for (int i = 1; i < (num_matrix + 1); i++){ if (matrix[first][second] != 0){ first ++; } matrix[first][second] = i; first--; second++; if (first < 0) first = n - 1; if (second > n-1) second = 0; } return matrix; } int main(){ int n; cout << "Input the odd number..."<<endl; cin >> n; while (n % 2 != 1){ cout << "The inputed number is not odd..."<<endl; cin >> n; } cout.setf(std::ios::left); int **a = createMatrix(n); for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ cout.width(5); cout << a[i][j]; } cout << endl; } delete[]a; return 0; }
#include <iostream> void shudu() { const int n=3; int a[n][n]={0}; int i,j; i=0; j=n/2; a[i][j]=1; for(int c=2;c<=n*n;c++) { if (i-1>=0&&j+1<n) { if (a[i-1][j+1]==0) { i=i-1; j=j+1; } else { i=i+1; } } else { if (i-1<0&&j+1>=n) { i=i+1; } else { if (j+1>=n) { i=i-1; j=j+1-n; } else { j=j+1; i=i-1+n; } } } a[i][j]=c; } for (i=0;i<n;i++) { for (j=0;j<n;j++) { std::cout<<a[i][j]<<" "; } std::cout<<std::endl; } }
void sudoku()
{
int array[length][length] = {0};
int
i=0,j = length/2;
for(int k=1;k<=length*length;k++)
{
array[i][j] = k;
i--;
j++;//右上一格
while(i<0 || j>=length || array[i][j] > 0)
{
if(i<0 && j>=length)
{
if(array[length
-1][0] == 0)
{
i = length -1;
j = 0;
}
else
{
i++;
}
}
else if(i<0)
{
i = length -1;
}
else if(j>=length)
{
j =0;
}
else
{
i+=2;
j--;
}
}
}
}
public class OddMagicMatrix { public static void main(String[] args) { // 输入奇数维n System.out.println("输入奇数n:"); Scanner keyIn = new Scanner(System.in); int n = 0; while (keyIn.hasNext()) { n = keyIn.nextInt(); // 初始化二维矩阵 int mat[][] = new int[n][n]; // 一居首行正*** int x = 0, y = n / 2; mat[x][y] = 1; // 依次斜填右上方x-1,y+1 for (int i = 2; i <= n * n; i++) { int newX = x - 1, newY = y + 1; // 右出框时向左写 if (newY > n - 1) { newY = 0; } // 上出框时往下放 if (newX < 0) { newX = n - 1; } // 遇到重合无处填,退居原数正下方 if (mat[newX][newY] != 0) { newX = x + 1; newY = y; } // 填入矩阵 mat[newX][newY] = i; x = newX; y = newY; } // 输出结果 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(mat[i][j] + "\t"); } System.out.println(); } } } }
#include<iostream> #include<cstring> using namespace std; int maze[20][20]; int main() { int n; while(cin>>n) { memset(maze,0,sizeof(maze)); int i,j,k; i=1; j=n/2+1; maze[i][j]=1; for(k=2; k<=n*n; k++) { if(j+1<=n && i-1>=1) { if( maze[i-1][j+1]==0) { i--; j++; } else { i++; } } else if(j+1>n && i-1<=0) { i++; } else if(j+1>n) { j=1; i--; } else { i=n; j++; } maze[i][j]=k; } for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { cout<<maze[i][j]<<" "; } cout<<endl; } } return 0; }