📄 tiaoma.java~1~
字号:
package tiaoma;
import java.io.*;
public class tiaoma {
static int n;
static int s[][];
static int cmq = 1;
public tiaoma() {
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("请输入棋盘大小:");
str = buf.readLine();
n = Integer.parseInt(str);
s = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
s[i][j] = 0;
}
}
}
catch (IOException e) {
e.toString();
}
catch (ArrayIndexOutOfBoundsException evt) {
evt.toString();
}
}
static void traceback(int x, int y) {
if (1 <= x - 2 && y + 1 <= n && s[x - 2][y + 1] == 0) {
s[x - 2][y + 1] = ++cmq;
traceback(x - 2, y + 1);
}
if (1 <= x - 1 && y + 2 <= n && s[x - 1][y + 2] == 0) {
s[x - 1][y + 2] = ++cmq;
traceback(x - 1, y + 2);
}
if (x + 1 <= n && y + 2 <= n && s[x + 1][y + 2] == 0) {
s[x + 1][y + 2] = ++cmq;
traceback(x + 1, y + 2);
}
if (x + 2 <= n && y + 1 <= n && s[x + 2][y + 1] == 0) {
s[x + 2][y + 1] = ++cmq;
traceback(x + 2, y + 1);
}
if (x + 2 <= n && 1 <= y - 1 && s[x + 2][y - 1] == 0) {
s[x + 2][y - 1] = ++cmq;
traceback(x + 2, y - 1);
}
if (x + 1 <= n && 1 <= y - 2 && s[x + 1][y - 2] == 0) {
s[x + 1][y - 2] = ++cmq;
traceback(x + 1, y - 2);
}
if (1 <= x - 1 && 1 <= y - 2 && s[x - 1][y - 2] == 0) {
s[x - 1][y - 2] = ++cmq;
traceback(x - 1, y - 2);
}
if (1 <= x - 2 && 1 <= y - 1 && s[x - 2][y - 1] == 0) {
s[x - 2][y - 1] = ++cmq;
traceback(x - 2, y - 1);
}
}
static void shuchu() {
System.out.print('t');
for (int i1 = 1; i1 <= n; i1++) {
System.out.print(i1+'t');
}
for (int i = 1; i <= n; i++) {
System.out.println();
System.out.print(i+"行"+'t');
for (int j = 1; j <= n; j++) {
System.out.print(s[i][j]+'t');
}
System.out.println();
}
}
public static void main(String args[]) {
int x = 1, y = 1;
s[x][y] = 1;
traceback(x, y);
shuchu();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -