Blue Bridge cup jumping grasshopper

Source: Internet
Author: User
Tags static class
Topic


Title Description:


There are 9 plates, lined up in 1 circles. 8 of the plates contained 8 grasshoppers, one of which was empty. We numbered these grasshoppers clockwise to 1~8 each grasshopper can jump to an adjacent empty disk, or it can use a little force to jump over an adjacent grasshopper to the empty disk. Please calculate if you want to change the grasshopper's formation to counter-clockwise, and keep the position of the empty disk unchanged (that is, 1-8 transposition, 2-7 transposition,... ), at least how many hops to go through. Input: Output: sample Input

Sample output
No



Ideas


A wide search with empty plates, resulting in the answer

Code

Package text;
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.Set;

Import Java.util.TreeSet;
		public class Main {public static class loc {int x;
		int step;
		int pos[] = new INT[9];
    LOC () {}} static int forward[] = {-2,-1, 1, 2};
    	public static void Main (string[] args) {set<string> vis = new TreeSet ();
    	Loc Zero =new loc ();
    	zero.x = 8;    
    	Zero.step = 0;
    	for (int i=0;i<9;i++) {zero.pos[i]=i;
		} string W=new string ();
		for (int j=0;j<9;j++) {w+=integer.tostring (zero.pos[j]);
    	} vis.add (W);
    	queue<loc> Queue =new linkedlist<loc> ();
    	Queue.add (zero);
    		while (!queue.isempty ()) {loc s=queue.remove ();//System.out.println (step); if (s.pos[0]==8&&s.pos[1]==7&&s.pos[2]==6&&s.pos[3]==5&&s.pos[4]==4&&
    			s.pos[5]==3&&s.pos[6]==2&&s.pos[7]==1&&s.pos[8]==0) {System.out.println (s.step); REturn;
    				} else {for (int i=0;i<4;i++) {int tx=s.x+forward[i];
    				Loc T = new Loc ();
    				if (tx<0) {tx=tx+9;
    				} else if (tx>8) {tx=tx-9;
    				} for (int j=0;j<9;j++) {t.pos[j]=s.pos[j];
    				} int TEMP=T.POS[TX];
    				T.pos[tx]=t.pos[s.x];
    				T.pos[s.x]=temp;
    				String str= "";
    				for (int j=0;j<9;j++) {str+=integer.tostring (t.pos[j]);
    					} if (!vis.contains (str)) {t.step=s.step+1;
T.X=TX;
    					System.out.println (str);
    					Queue.add (t);
    				Vis.add (str);
 }
    				}
    		}
    	}
    
    }
}

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.