《100分谈论个象棋问题》“马”能遍历整个棋盘吗?《随想--给出证明》

wawaxp 2004-02-01 12:54:57
昨天,和朋友下象棋,
想起这个问题,
现在假设“马”就呆在默认位置,
--没有其他棋子,任你天马行空!
请问它是否能够走遍整个棋盘,
请证明,
------
游戏话题,锻炼一下,
------
...全文
158 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyforlove 2004-02-01
  • 打赏
  • 举报
回复
至于算法,我还没想出来,不过以前有人证明过,确实可以走遍整个棋盘的。
miwoo 2004-02-01
  • 打赏
  • 举报
回复
有点问题,改一下应该就行了
import java.io.*;

public class ChineseChess{

private static int _width=9; //棋盘宽度
private static int _height=10; //棋盘高度
private static int[][] qipan; //棋盘,数值代表第几步跳到这个位置的,
//0代表这个位置还没有跳到过,从1开始

private static int _allPositionCount;//遍历整个棋盘有几步
private static int currPositionIndex=0; //现在是第几步,从0开始
private static int[] dirs; //如何从上一步跳到的这一步,以便走不下去时可以回退到上一步

//-----------------------------------------------------------------
private static final int[] _xOffset= //马可能跳的8个方向
new int[]{1,1, 2, 2,-1,-1,-2,-2};
private static final int[] _yOffset=
new int[]{2,-2,1,-1,2, -2,1, -1};
private static final int _allDirCount=_xOffset.length;//8个方向

private static long jump=0;//总共跳了几次

private static int _printPerJumps=1;//每跳多少次打印出棋盘,<=0表示不打印

//从[x,y]开始跳
private static int x=0;
private static int y=0;

//-----------------------------------------------------------------

//打印棋盘
private static void printQipan(){
System.out.println();
System.out.println("jump: "+jump);
for(int h=0;h<_height;h++){

for(int w=0;w<_width-1;w++){
System.out.print("+----");
}
System.out.print("+----+");
System.out.println();

for(int w=0;w<_width;w++){
if(qipan[w][h]==0){
System.out.print("| ");
}else if(qipan[w][h]<10){
System.out.print("| "+qipan[w][h]+" ");
}else{
System.out.print("| "+qipan[w][h]+"");
}

if(w==_width-1)
System.out.print(" |");
else
System.out.print(" ");
}
System.out.println();
}

for(int w=0;w<_width-1;w++){
System.out.print("+----");
}
System.out.print("+----+");
System.out.println();
}

public static void readLine(){
try{
new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch(IOException ioe){
System.exit(-1);
}
}

//-----------------------------------------------------------------
//处理参数
private static void processArgs(String[] args){
try{
for(int i=0;i<args.length;i++){
if(args[i].equals("-x")){
x=Integer.parseInt(args[++i]);
if(x<0) throw new IllegalArgumentException();

}else if(args[i].equals("-y")){
y=Integer.parseInt(args[++i]);
if(y<0) throw new IllegalArgumentException();

}else if(args[i].equals("-w")){
_width=Integer.parseInt(args[++i]);
if(_width<=0) throw new IllegalArgumentException();

}else if(args[i].equals("-h")){
_height=Integer.parseInt(args[++i]);
if(_height<=0) throw new IllegalArgumentException();

}else if(args[i].equals("-p"))
_printPerJumps=Integer.parseInt(args[++i]);
//if(_printPerJumps<=0) throw new IllegalArgumentException();
}
}catch(Exception e){
System.out.println(e.toString());
printUsage();
System.exit(-1);
}
}

//打印出使用方法
private static void printUsage(){
System.out.println("java ChineseChess -x X -y Y -w Width -h Height -p PrintPerJumps");
}

//主程序
public static void main(String[] args) throws Exception{

processArgs(args);

_allPositionCount=_width*_height;
dirs=new int[_allPositionCount];
qipan=new int[_width][_height];

//System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("c://chess.txt"))));
//-----------------------------------------------------------

currPositionIndex=0;
qipan[x][y]=currPositionIndex+1;

//跳到第一个位置的方向,因为第一个位置是任意给定的,所以赋个无意义的值-1
dirs[currPositionIndex]=-1;

//------------------------
//用于测试
/*printQipan();
if(true) return;*/
//------------------------

//开始跳了...
while(true){
//成功
if(currPositionIndex==_allPositionCount-1){
System.out.println("Find a way!");
printQipan();
break;
}

//跳到下一个位置
boolean found=false;
for(int i=dirs[currPositionIndex+1];i<_allDirCount;i++){
int newX=x+_xOffset[i];
int newY=y+_yOffset[i];

if(newX<0 || newY<0 || newX>=_width || newY>=_height || qipan[newX][newY]!=0){
continue;
}else{
found=true;

x=newX;
y=newY;
currPositionIndex++;
qipan[x][y]=currPositionIndex+1;
dirs[currPositionIndex]=i;
//-------------------------------------
//打印出当前的棋盘
jump++;
if(_printPerJumps>0){
if(jump%_printPerJumps==0){
printQipan();
}
}
//-------------------------------------
break;
}
}

//跳不下去了,回到前一个位置
if(!found){
//找不到这样的路径
if(currPositionIndex==0){
System.out.println("Can not find a way!");
break;
}

qipan[x][y]=0;

x-=_xOffset[dirs[currPositionIndex]];
y-=_yOffset[dirs[currPositionIndex]];
dirs[currPositionIndex]++;

dirs[currPositionIndex+1]=0;

currPositionIndex--;
}
}
}
}
sobingman 2004-02-01
  • 打赏
  • 举报
回复
一定可以,没有马踹不到的地方,只有士象兵将有不可到达的地方
miwoo 2004-02-01
  • 打赏
  • 举报
回复
不知道行不行

import java.io.*;

public class ChineseChess{

private static int _width=9; //棋盘宽度
private static int _height=10; //棋盘高度
private static int[][] qipan=new int[_width][_height]; //棋盘,数值代表第几步跳到这个位置的,
//0代表这个位置还没有跳到过,从1开始

private static final int _allPositionCount=_width*_height;//遍历整个棋盘有几步
private static int currPositionIndex=0; //现在是第几步,从0开始
private static int[] dirs=new int[_allPositionCount]; //如何从上一步跳到的这一步,以便走不下去时可以回退到上一步

//-----------------------------------------------------------------
private static final int[] _xOffset= //马可能跳的8个方向
new int[]{1,1, 2, 2,-1,-1,-2,-2};
private static final int[] _yOffset=
new int[]{2,-2,1,-1,2, -2,1, -1};
private static final int _allDirCount=_xOffset.length;//8个方向

private static long jump=0;//总共跳了几次

private static int _printPerJumps=1;//每跳多少次打印出棋盘,<=0表示不打印

//从[x,y]开始跳
private static int x=0;
private static int y=0;

//-----------------------------------------------------------------

//打印棋盘
private static void printQipan(){
System.out.println();
System.out.println("jump: "+jump);
for(int h=0;h<_height;h++){

for(int w=0;w<_width-1;w++){
System.out.print("+----");
}
System.out.print("+----+");
System.out.println();

for(int w=0;w<_width;w++){
if(qipan[w][h]==0){
System.out.print("| ");
}else if(qipan[w][h]<10){
System.out.print("| "+qipan[w][h]+" ");
}else{
System.out.print("| "+qipan[w][h]+"");
}

if(w==_width-1)
System.out.print(" |");
else
System.out.print(" ");
}
System.out.println();
}

for(int w=0;w<_width-1;w++){
System.out.print("+----");
}
System.out.print("+----+");
System.out.println();
}

public static void readLine(){
try{
new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch(IOException ioe){
System.exit(-1);
}
}

//-----------------------------------------------------------------
//处理参数
private static void processArgs(String[] args){
try{
for(int i=0;i<args.length;i++){
if(args[i].equals("-x")){
x=Integer.parseInt(args[++i]);
if(x<0) throw new IllegalArgumentException();

}else if(args[i].equals("-y")){
y=Integer.parseInt(args[++i]);
if(y<0) throw new IllegalArgumentException();

}else if(args[i].equals("-w")){
_width=Integer.parseInt(args[++i]);
if(_width<=0) throw new IllegalArgumentException();

}else if(args[i].equals("-h")){
_height=Integer.parseInt(args[++i]);
if(_height<=0) throw new IllegalArgumentException();

}else if(args[i].equals("-p"))
_printPerJumps=Integer.parseInt(args[++i]);
//if(_printPerJumps<=0) throw new IllegalArgumentException();
}
}catch(Exception e){
System.out.println(e.toString());
printUsage();
System.exit(-1);
}
}

//打印出使用方法
private static void printUsage(){
System.out.println("java ChineseChess -x X -y Y -w Width -h Height -p PrintPerJumps");
}

//主程序
public static void main(String[] args) throws Exception{

processArgs(args);

//System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("c://chess.txt"))));
//-----------------------------------------------------------

currPositionIndex=0;
qipan[x][y]=currPositionIndex+1;

//跳到第一个位置的方向,因为第一个位置是任意给定的,所以赋个无意义的值-1
dirs[currPositionIndex]=-1;

//------------------------
//用于测试
/*printQipan();
if(true) return;*/
//------------------------

//开始跳了...
while(true){
//成功
if(currPositionIndex==_allPositionCount-1){
System.out.println("Find a way!");
printQipan();
break;
}

//跳到下一个位置
boolean found=false;
for(int i=dirs[currPositionIndex+1];i<_allDirCount;i++){
int newX=x+_xOffset[i];
int newY=y+_yOffset[i];

if(newX<0 || newY<0 || newX>=_width || newY>=_height || qipan[newX][newY]!=0){
continue;
}else{
found=true;

x=newX;
y=newY;
currPositionIndex++;
qipan[x][y]=currPositionIndex+1;
dirs[currPositionIndex]=i;
//-------------------------------------
//打印出当前的棋盘
jump++;
if(_printPerJumps>0){
if(jump%_printPerJumps==0){
printQipan();
}
}
//-------------------------------------
break;
}
}

//跳不下去了,回到前一个位置
if(!found){
//找不到这样的路径
if(currPositionIndex==0){
System.out.println("Can not find a way!");
break;
}

qipan[x][y]=0;

x-=_xOffset[dirs[currPositionIndex]];
y-=_yOffset[dirs[currPositionIndex]];
dirs[currPositionIndex]++;

dirs[currPositionIndex+1]=0;

currPositionIndex--;
}
}
}
}
hermits 2004-02-01
  • 打赏
  • 举报
回复
可以,我好象在数据结构的书上看过的,现在都忘记了!真是对不起!
GameWeaverDummy 2004-02-01
  • 打赏
  • 举报
回复
根据我下起的经验,是可以的

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧