扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
编码方式不对,windows默认是GBK,你的eclipse可能是别的编码方式。在eclipse里面改,设置改成GBK;再拷出来就统一了。下次不要考代码,直接把java文件带回去,那个类文件就有你要的代码;
濉溪ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
方框是字体的问题,问号才是字符集的问题,你在 eclipse 参数页中 General Appearance Colors and Fonts 里面把 Console 相关的字体设成一个支持汉字的字体,比如楷体或 Arial MS Unicode.
drawRect
public void drawRect(int x,
int y,
int width,
int height)绘制指定矩形的边框。矩形的左边和右边位于 x 和 x + width。顶边和底边位于 y 和 y + height。使用图形上下文的当前颜色绘制该矩形。
参数:
x - 要绘制矩形的 x 坐标。
y - 要绘制矩形的 y 坐标。
width - 要绘制矩形的宽度。
height - 要绘制矩形的高度。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.Stack;
public class T {
private static final int[][] MOVES = new int[][] { { -2, 1 }, { -1, 2 },
{ 1, 2 }, { 2, 1 }, { 2, -1 }, { 1, -2 }, { -1, -2 }, { -2, -1 } };
private static final int SIZE = 8;
private static final int BASE = SIZE + 4;
private static int[][] board;
private static NeighborComparator neighborComparator = new NeighborComparator();
public static void main(String[] args) {
board = new int[BASE][BASE];
for (int r = 0; r BASE; r++) {
for (int c = 0; c BASE; c++) {
if (r 2 || r BASE - 3 || c 2 || c BASE - 3) {
board[r][c] = -1;
}
}
}
int row = 2 + new Random().nextInt(SIZE);
int col = 2 + new Random().nextInt(SIZE);
solve(row, col);
}
private static void solve(int r, int c) {
StackCell stack = new StackCell();
int count = 1;
Cell cell = new Cell(r, c, neighbors(r, c));
stack.push(cell);
board[r][c] = count++;
while (!stack.isEmpty()) {
if (stack.size() == SIZE * SIZE) {
break;
}
cell = stack.peek();
if (cell.nextNeighbor cell.neighbors.size()) {
int[] neighbor = cell.neighbors.get(cell.nextNeighbor);
r = neighbor[0];
c = neighbor[1];
board[r][c] = count++;
stack.push(new Cell(r, c, neighbors(r, c)));
cell.nextNeighbor++;
} else {
stack.pop();
board[cell.r][cell.c] = 0;
count--;
}
}
if (stack.size() == SIZE * SIZE) {
print();
} else {
System.out.println("无解");
}
}
private static class NeighborComparator implements Comparatorint[] {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
}
private static Listint[] neighbors(int r, int c) {
Listint[] neighbors = new ArrayList();
for (int[] m : MOVES) {
int x = m[0];
int y = m[1];
if (board[r + y][c + x] == 0) {
neighbors.add(new int[] { r + y, c + x, countNeighbors(r + y, c + x) });
}
}
Collections.sort(neighbors, neighborComparator);
return neighbors;
}
private static int countNeighbors(int r, int c) {
int num = 0;
for (int[] m : MOVES) {
if (board[r + m[1]][c + m[0]] == 0) {
num++;
}
}
return num;
}
private static void print() {
for (int i = 2; i board.length - 2; i++) {
for (int j = 2; j board[i].length - 2; j++) {
System.out.printf("%2d ", board[i][j]);
}
System.out.println();
}
System.out.println();
}
private static class Cell {
int r;
int c;
Listint[] neighbors;
int nextNeighbor = 0;
public Cell(int r, int c, Listint[] neighbors) {
this.r = r;
this.c = c;
this.neighbors = neighbors;
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流