帮帮忙编程马踏棋盘(十万火急)
#include stdio.h
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站建设、西宁网络推广、重庆小程序开发、西宁网络营销、西宁企业策划、西宁品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供西宁建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
main()
{
int a[9][9],object[9][9],step[9][3]={{0,0,0},{1,1,2},{2,1,-2},{3,-1,2},{4,-1,-2},
{5,2,1},{6,2,-1},{7,-2,1},{8,-2,-1}};
int i,j,k,x,y,z,m,n,min;
for(i=1;i=8;i++)
for(j=1;j=8;j++)
a[i][j]=0; /* clear data in array */
for(i=1;i=8;i++)
for(j=1;j=8;j++)
for(k=1;k=8;k++)
{
x=i;y=j;
x=x+step[k][1];
y=y+step[k][2];
if(x=1x=8y=1y=8)
a[i][j]++ ; /* initilize array */
} /* start col and row;*/
printf("Please inpute start position x,y\n");
scanf("%d,%d",m,n);
for(z=1;z=64;z++)
{
min =10;
object[m][n]=z;
a[m][n]=0;
for(k=1;k=8;k++)
{
x=m+step[k][1];
y=n+step[k][2];
if(x=1x=8y=1y=8)
if(a[x][y]!=0)
{
--a[x][y];
if(a[x][y]min)
{
min=a[x][y];
i=x;
j=y;
}
}
}
m=i;n=j;
}
for(i=1;i=8;i++)
{
for(j=1;j=8;j++)
printf("%6d",object[i][j]);
printf("\n");
}
}
【Java数据结构马踏棋盘问题】将马随机放在国际象棋的8×8棋盘Board[8][8]的某个方格中
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;
}
}
}
java马踏棋盘设计目的
java马踏棋盘设计目的是解决实际的应用问题,特别是非数值计算类型的应用问题。
马踏棋盘的基本过程:国际象棋的棋盘为8*8的方格棋盘。现将"马"放在任意指定的方格中,按照"马"走棋的规则将"马"进行移动。要求每个方格只能进入一次,最终使得"马"走遍棋盘的64个方格。
马踏棋盘的解决方案:基于深度优先搜索的方法是比较常用的算法,深度优先搜索算法也是数据结构中的经典算法之一,主要是采用递归的思想,一级一级的寻找,最后找到合适的解。
文章标题:优化马踏棋盘java代码 优化马踏棋盘java代码是多少
文章位置:http://scgulin.cn/article/hgecps.html