国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

馬踏棋盤算法 Java實現

2019-11-11 06:54:16
字體:
來源:轉載
供稿:網友

馬在某個點最多可能有8種走法,用遞歸和回溯實現。

注:代碼中,查找下一個可走坐標是從右下第一個開始的,也就是圖中的4。可以通過修改a,b...h的值來改變順序。

/** * 馬踏棋盤算法  * 遞歸和回溯 * */public class HorseStep {		public static int X = 8;	public static int Y = 8;		public static int returnCount = 0;		/**	 * 棋盤	 */	public static int chess[][] = new int[X][Y];			/**	 * 找到基于(x,y)位置的下一個可走位置	 * @param x	 * @param y	 * @param count	 * @return	 */	public static int nextxy(XY xy,int count){				final int a=0,				b=1,				c=2,				d=3,				e=4,				f=5,				g=6,				h=7;				int x = xy.getX();		int y = xy.getY();				int returnInt = 0;				switch (count) {		//		從以x,y為軸心的 右下 開始				case a:			if( x+2<=X-1 && y+1<=Y-1 && chess[y+1][x+2]==0){				x +=2;				y +=1;				returnInt = 1;			}						break;					case b:			if( x+1<=X-1 && y+2<=Y-1 && chess[y+2][x+1]==0){				x +=1;				y +=2;				returnInt = 1;			}						break;					case c:			if( x-1>=0 && y+2<=Y-1 && chess[y+2][x-1]==0){				x -=1;				y +=2;				returnInt = 1;			}						break;					case d:			if( x-2>=0 && y+1<=Y-1 && chess[y+1][x-2]==0){				x -=2;				y +=1;				returnInt = 1;			}						break;				case e:			if( x-2>=0 && y-1>=0 && chess[y-1][x-2]==0){				x -=2;				y -=1;				returnInt = 1;			}						break;					case f:			if( x-1>=0 && y-2>=0 && chess[y-2][x-1]==0){				x -=1;				y -=2;				returnInt = 1;			}						break;					case g:			if( x+1<=X-1 && y-2>=0 && chess[y-2][x+1]==0){				x +=1;				y -=2;				returnInt = 1;			}						break;					case h:			if( x+2<=X-1 && y-1>=0 && chess[y-1][x+2]==0){				x +=2;				y -=1;								returnInt = 1;			}			break;					default:			break;		}				if(returnInt == 1){			xy.setX(x);			xy.setY(y);						return 1;		}		return 0;	}		/**	 * 打印棋盤	 */	public static void PRint(){		for(int i=0;i<X;i++){			for(int j=0;j<Y;j++){								if(chess[i][j]<10)					System.out.print(chess[i][j]+"  ");				else					System.out.print(chess[i][j]+" ");							}			System.out.println();		}			}		/**	 * 深度優先遍歷棋盤	 * @param x	 * @param y	 * @param tag	 * @return	 * (x,y)為位置坐標	 * tag是標記變量,每走一步 tag+1。	 */	public static int TravelChessBoard(XY xy,int tag){		//		馬在某個點有八種可能的方向,用來約束查找小于八種的變量		Integer count = 0;		//		馬所在位置是否可以再跳向下一個位置,0有,1無(條件:1,不出邊界,2.沒有走過)		int haveNextXy = 0;				int x = xy.getX();		int y = xy.getY();		//		x是橫軸,y是豎軸,左上角為0,0點,往右和往下遞增		chess[y][x] = tag;		//		最后一步,遞歸的終止條件		if(X*Y == tag){//			打印棋盤			print();			return 1;		}		//		找到馬的下一個可走坐標(x1,y1),如果找到為1,否則為0.		haveNextXy = nextxy(xy, count);				while( 0==haveNextXy && count<7){			count ++;			haveNextXy = nextxy(xy, count);		}				while(haveNextXy==1){			if(TravelChessBoard(xy, tag+1)==1){				return 1;			}			//			回退后,把當前點也設置為回退后的位置			xy.setX(x);			xy.setY(y);						count++;			//			找到馬的下一個可走坐標(x1,y1),如果找到flag=1,否則為0.			haveNextXy = nextxy(xy, count);						while( 0==haveNextXy && count<7){				count ++;				haveNextXy = nextxy(xy, count);			}		}		//		回退		if(haveNextXy==0){			chess[y][x]=0;			returnCount++;		}				return 0 ;	}		public static void main(String[] args) {		long begin = System.currentTimeMillis();		//		馬所在位置的坐標,x是橫軸,y是豎軸,左上角為0,0點,往右和往下遞增		XY xy = new XY();		xy.setX(1);		xy.setY(0);				if(TravelChessBoard(xy, 1)==0){			System.out.println("馬踏棋盤失敗");		}				long time = System.currentTimeMillis()-begin;				System.out.println("耗時"+time+"毫秒");		System.out.println(returnCount);	}	}class XY{	private int x;	private int y;	public int getX() {		return x;	}	public void setX(int x) {		this.x = x;	}	public int getY() {		return y;	}	public void setY(int y) {		this.y = y;	}		}

結果:

如果從(0,0)開始的話


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 剑川县| 山东省| 南阳市| 石景山区| 卢龙县| 芮城县| 孝昌县| 梨树县| 荔波县| 鸡泽县| 岫岩| 锦州市| 舒城县| 时尚| 晋宁县| 华安县| 海门市| 伊金霍洛旗| 陆川县| 松原市| 哈密市| 德庆县| 汪清县| 合水县| 灵山县| 寻甸| 青田县| 白玉县| 延庆县| 华坪县| 迭部县| 平安县| 阳朔县| 珠海市| 余江县| 山东| 汶上县| 砚山县| 梁河县| 田林县| 高州市|