0%

(Original) Nine Palace - Java Implementation - (原创) Java 九宫图 实现

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 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 sum
int total = centerNum * dimension;
int tempSum = 0;
for (int i = 0; i < dimension; i++)
{
// Check horizontal
tempSum = 0;
for (int j = 0; j < dimension; j++)
{
tempSum += paraArea[i][j];
}
if (tempSum != total)
{
return false;
}
// Check vertical
tempSum = 0;
for (int j = 0; j < dimension; j++)
{
tempSum += paraArea[j][i];
}
if (tempSum != total)
{
return false;
}
}
// Check diagonal
int tSumLeftDiagonal = 0;
int tSumRigthDiagonal = 0;
for (int i = 0; i < dimension; i++)
{
tSumLeftDiagonal += paraArea[i][i];
tSumRigthDiagonal += paraArea[(dimension - 1) - i][(dimension - 1)
- i];
}
if (tSumLeftDiagonal != total || tSumRigthDiagonal != total)
{
return false;
}
// All checked
return true;
}
public void createNinePalacePlans()
{
area = new int[dimension][dimension];
// Get the center number
int centerNum = (dimension * dimension + 1) / 2;
// Calculate the each line sum
total = centerNum * dimension;
// Set first number 1 position
int preNumx = (dimension + 1) / 2 - 1;
int preNumy = 0;
area[preNumy][preNumx] = 1;
// Set loop times
int loop = dimension * dimension;
int nextx = preNumx;
int nexty = preNumy;
for (int i = 1; i < loop; i++)
{
// Calculate next number position
nextx += 1;
nexty -= 1;
// nexty position is out-of-range
if (nexty < 0)
{
nexty = dimension - 1;
}
// nextx position is out-of-range
if (nextx >= dimension)
{
nextx = 0;
}
// nextx, nexty position has already had a number
if (area[nexty][nextx] != 0)
{
nextx = preNumx;
nexty = preNumy + 1;
}
// Fill the number
area[nexty][nextx] = i + 1;
// Keep the previous position
preNumx = nextx;
preNumy = nexty;
}
}
public void printNinePalace()
{
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
p1(area[i][j] + "\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 available
while (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.");
}
}
}
坚持原创及高品质技术分享,您的支持将鼓励我继续创作!