yhz61010 发表于 2013-2-4 20:21:09

(Original) Nine Palace

Some days ago, my girlfriend wanted me to write a program about Nine Palace in C#. Although I was extremely busy during that time, I still wrote for her in C# and Java(I like Java too). Because I can't stand her lovely begging.

    OK, let's get down to business.
    There're two method in this class:
    1: Type a dimension, one of the solutions will be displayed.
    2: Give your solution, algorithm will tell you whether your solution is correct.

    Here is the Java source code. You can copy it to your Java IDE and run it directly.

    I'll appreciate your reply with your better algorithm.

/* * Michael Leo * 2009/05/11/1 */package com.NinePalacePlans;import java.io.BufferedReader;import java.io.InputStreamReader;public class NinePalacePlans{public static void p(){System.out.println();}public static void p1(Object o){System.out.print(o);}public static void p(Object o){System.out.println(o);}public NinePalacePlans(){dimension = 3;}public NinePalacePlans(int dimension){this.dimension = dimension;}private int dimension;private int[][] area;private int total;public boolean manualNinePalacePlans(int[][] paraArea){int dimension = paraArea.length;int centerNum = (dimension * dimension + 1) / 2;// Calculate the each line sumint total = centerNum * dimension;int tempSum = 0;for (int i = 0; i < dimension; i++){// Check horizontaltempSum = 0;for (int j = 0; j < dimension; j++){tempSum += paraArea;}if (tempSum != total){return false;}// Check verticaltempSum = 0;for (int j = 0; j < dimension; j++){tempSum += paraArea;}if (tempSum != total){return false;}}// Check diagonalint tSumLeftDiagonal = 0;int tSumRigthDiagonal = 0;for (int i = 0; i < dimension; i++){tSumLeftDiagonal += paraArea;tSumRigthDiagonal += paraArea[(dimension - 1) - i][(dimension - 1)- i];}if (tSumLeftDiagonal != total || tSumRigthDiagonal != total){return false;}// All checkedreturn true;}public void createNinePalacePlans(){area = new int;// Get the center numberint centerNum = (dimension * dimension + 1) / 2;// Calculate the each line sumtotal = centerNum * dimension;// Set first number 1 positionint preNumx = (dimension + 1) / 2 - 1;int preNumy = 0;area = 1;// Set loop timesint loop = dimension * dimension;int nextx = preNumx;int nexty = preNumy;for (int i = 1; i < loop; i++){// Calculate next number positionnextx += 1;nexty -= 1;// nexty position is out-of-rangeif (nexty < 0){nexty = dimension - 1;}// nextx position is out-of-rangeif (nextx >= dimension){nextx = 0;}// nextx, nexty position has already had a numberif (area != 0){nextx = preNumx;nexty = preNumy + 1;}// Fill the numberarea = i + 1;// Keep the previous positionpreNumx = nextx;preNumy = nexty;}}public void printNinePalace(){for (int i = 0; i < dimension; i++){for (int j = 0; j < dimension; j++){p1(area + "\t");}p();}}public int getTotal(){return total;}public void setTotal(int total){this.total = total;}public int getDimension(){return dimension;}public void setDimension(int dimension){this.dimension = dimension;}public static void main(String[] args){NinePalacePlans nine = new NinePalacePlans();p1("Please input the dimension(odd number): ");BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String inStr;int num = 0;try{inStr = in.readLine();num = Integer.valueOf(inStr);// Only odd number is availablewhile (num % 2 == 0){p("Dimension must be odd number.");p1("Please input the dimension(odd number): ");inStr = in.readLine();num = Integer.valueOf(inStr);}nine.setDimension(num);}catch (Exception e){nine.setDimension(3);p("Entered dimension error. Using default dimesion: 3.");}nine.createNinePalacePlans();nine.printNinePalace();p();p("The total sum of each line: " + nine.getTotal());p();p("=== Manual NinePalacePlans ===");int[][] paraArea = { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } };if (nine.manualNinePalacePlans(paraArea)){p("All checked.");}else{p("Not correct.");}}}
页: [1]
查看完整版本: (Original) Nine Palace