Seventh session Waterloo Cup final JAVAA group third question _ Pawn transposition __java

Source: Internet
Author: User
The pieces are replaced with n pieces a,n a piece B, in a row on the board. There is an empty space between them, with the "." Say, for example: AAA. BBB now needs all the A and B pieces to swap positions. The rules for moving pieces are: 1. A piece can only move to the right, B pieces can only move to the left. 2. Each piece can be moved to an adjacent vacancy. 3. Each piece can skip a different piece and fall into the empty space (a skip B or b skip a). Aaa. BBB can go: Move a ==> AA. ABBB move B ==> Aaab. BB jumped off the example: AA. ABBB ==> Aaba. BB below the program completed AB transposition function, please read the analysis of the source code, fill out the missing content of the underlined part.

public class Main {static void Move (char[] data, int from, int to) {Data[to] = Data[from];
	Data[from] = '. ';
		Static Boolean valid (char[] data, int k) {if (k<0 | | | k>=data.length) return FALSE;
	return true;
			static void F (char[] Data {while (true) {Boolean tag = false;
				for (int i=0; i<data.length; i++) {int dd = 0;//move direction if (data[i]== '. ') continue;
				if (data[i]== ' A ') dd = 1;
				
				if (data[i]== ' B ') dd =-1; if (valid (data, I+DD) && valid (DATA,I+DD+DD) && data[i+dd]!=data[i] && data[i+dd+dd]== '. ')
					{//If can jump ... move (data, I, I+DD+DD);
					System.out.println ("Hop:" +new String (data));
					Tag = true;
				Break
			
			} if (tag) continue;
				for (int i=0; i<data.length; i++) {int dd = 0;//' Move direction if (data[i]== '. ') continue;
				if (data[i]== ' A ') dd = 1;			
					 
				if (data[i]== ' B ') dd =-1; if (valid (data, I+DD) && data[i+dd]== '. ') {//If you can move ... if (valID (DATA,I+DD+DD) && (valid (data,i+dd*-1) && data[i+dd+dd]==data[i+dd*-1)) continue;
					Fill in the blank position move (data, I, I+DD);
					System.out.println ("Move:" +new String (data));
					Tag = true;
				Break					
		} if (tag==false) break; } public static void Main (string[] args) {char[] data = "AAA."	
		BBB ". ToCharArray ();
	f (data);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.