Waterloo Cup--the seventh annual final: Flag transposition

Source: Internet
Author: User
Pawn Transposition

There are 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 you need 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 by:
Move a ==> AA. abbb
Move b ==> Aaab. Bb
Example of skipping:

Aa. ABBB ==> Aaba. Bb

found that before doing a problem must understand the meaning of the topic, and I always confused his meaning before the fill in the blank position directly filled the tag also feel quite simple to see the other people's Operation results and I do not know that they do not understand the problem

I'm sorry, I misled you.

Aa. abbb
Aaba. Bb
AAB. Abb
A.baabb
. Abaabb
BA. AABB
B.aaabb This is wrong the final question is not so simple ....

------------------------

Aa. abbb
Aaba. Bb
Aabab. B A can move, but not move.
AAB. BAB
A.babab
. Ababab
BA. ABAB
BABA. Ab
Bababa. . No letters in the back so a moves directly
Babab. A
BAB. BAA
B.babaa
Bb. Abaa
Bbba. Aa
Bbb. Aaa

The answers to the blanks are:

Valid (data, I-DD) && DATA[I-DD]!=DATA[I]&&I+DD!=DATA.LENGTH-1&&DATA[I+DD+DD]==DATA[I-DD]
It means that when there is no jump, the piece can be moved before it moves to determine if the next letter is the same as if it were the same. "And then there's the letter. If there is a judge with the same flag next to the piece if the same if the same is not the flag moved continue past.

public class Pawn Replacement {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 (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 can move ... if (valid (data, I-DD) &&  DATA[I-DD]!=DATA[I]&AMP;&AMP;I+DD!=DATA.LENGTH-1&AMP;&AMP;DATA[I+DD+DD]==DATA[I-DD]) continue;
					Fill in the blank position move (data, I, I+DD);
					System.out.println (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.