Новичок
Профиль
Группа: Участник
Сообщений: 0
Регистрация: 1.6.2008
Репутация: нет Всего: нет
|
Нет ничего проще! Заменить ввод/вывод. Код | package ru.vingrad.chess;
import java.util.Scanner;
public class PuzzleSolver {
private static final int HORIZONTAL_SIZE = 8; private static final int VERTICAL_SIZE = 8;
private static int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; private static int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
private static int accessibility[][] = { { 2, 3, 4, 4, 4, 4, 3, 2 }, { 3, 4, 6, 6, 6, 6, 4, 3 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 3, 4, 6, 6, 6, 6, 4, 3 }, { 2, 3, 4, 4, 4, 4, 3, 2 } };
private static int currentRow, currentColumn, moveNumber, counter = 0; private static int board[][] = new int[HORIZONTAL_SIZE][VERTICAL_SIZE];
/** * */ public PuzzleSolver() { // TODO Auto-generated constructor stub }
/** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a horizontal coordinate(0 - 7): "); while (!sc.hasNextInt()) sc.next(); currentRow = sc.nextInt(); System.out.print("Enter a vertical coordinate(0 - 7): "); while (!sc.hasNextInt()) sc.next(); currentColumn = sc.nextInt(); sc.close(); int mainRow = currentRow, mainColumn = currentColumn; int Row = 0, Column = 0;
for (int i = 1; i <= 64; i++) { board[mainRow][mainColumn] = ++counter;
int minAccessibility = 8; int minA = 8, minB = 8;
for (moveNumber = 0; moveNumber <= 7; moveNumber++) { currentRow = mainRow; currentColumn = mainColumn;
currentRow += horizontal[moveNumber]; currentColumn += vertical[moveNumber];
if (currentRow >= 0 && currentRow <= 7 && currentColumn >= 0 && currentColumn <= 7) { accessibility[currentRow][currentColumn]--;
if (minAccessibility > accessibility[currentRow][currentColumn] && board[currentRow][currentColumn] == 0) { minAccessibility = accessibility[currentRow][currentColumn];
if (board[currentRow][currentColumn] == 0) { Row = currentRow; Column = currentColumn; } int RowA = currentRow, ColumnA = currentColumn;
for (int moveA = 0; moveA <= 7; moveA++) { RowA += horizontal[moveA]; ColumnA += vertical[moveA];
if (RowA >= 0 && RowA <= 7 && ColumnA >= 0 && ColumnA <= 7) { if (minA >= accessibility[RowA][ColumnA] && board[RowA][ColumnA] == 0) minA = accessibility[RowA][ColumnA]; } } }
if (minAccessibility == accessibility[currentRow][currentColumn] && board[currentRow][currentColumn] == 0) { minAccessibility = accessibility[currentRow][currentColumn];
int RowB = currentRow, ColumnB = currentColumn;
for (int moveB = 0; moveB <= 7; moveB++) { RowB += horizontal[moveB]; ColumnB += vertical[moveB];
if (RowB >= 0 && RowB <= 7 && ColumnB >= 0 && ColumnB <= 7) { if (minB >= accessibility[RowB][ColumnB] && board[RowB][ColumnB] == 0) minB = accessibility[RowB][ColumnB]; } }
if (board[currentRow][currentColumn] == 0 && minB < minA) { Row = currentRow; Column = currentColumn; } } } }
mainRow = Row; mainColumn = Column; }
printBoard();
}
private static void printBoard() { System.out.format("\n"); for (int j = 0; j < VERTICAL_SIZE; j++) { for (int i = 0; i < HORIZONTAL_SIZE; i++) { System.out.format("%4d", board[i][j]); } System.out.format("\n\n"); } }
}
|
Этот ответ добавлен с нового Винграда - http://vingrad.com
|