Moving letters on the grid

Source: Internet
Author: User
/* Move the letter 2x3 = 6 squares with five letters ABCDE left empty in the lower right corner. 1.
 
 

The letters in the lattice adjacent to the blank lattice can be moved to the space. For example, C and E in the figure can be moved. The following figure shows the moving situation: a B d e ca B CD E for convenience, we use a string to represent the letters in the six grids. For example, the two situations above are represented: the requirements for AB * decabcd * E are as follows: Program The user inputs several strings that indicate the situation. The program outputs whether the initial status can be reached by moving several times. You can achieve output 1; otherwise, output 0. The initial status is ABCDE *. The format entered by the user is: an integer N, indicating that there are n rows following the status. The program output should also be n rows 1 or 0. For example, if the user input is 3abcde * AB * deccaed * B, the program should output: 110 */import Java. util. imports; import Java. util. arraylist; import Java. util. queue; import Java. util. using list; import Java. util. list; public class letter movement {// exchange element public static string swap (string S, int K, Int J) {char [] C = S. tochararray (); char T = C [k]; C [k] = C [J]; C [J] = T; return new string (c );} public static int F (string s) {list <string> Lis = ne W arraylist <string> (); // record all exchange results (check for repeated use) queue <string> queue = new queue list <string> (); // Queue (used to test all possibilities) int [] d = {-1,-3, 1, 3}; // defines the direction (left, top, right, bottom) queue. offer (s); // enter Lis. add (s); // Add record while (queue. size ()> 0) {string firststr = queue. poll (); // If (firststr. equals ("ABCDE *") return 1; // locate the result and exit int K = firststr. indexof ("*"); // locate the "*" position for (INT I = 0; I <4; I ++) {// left, top, right, int J = K + d [I] in the next four directions; If (j> = 0 & J <= 5) {// The current direction does not cross the border string T = swap (firststr, K, J); // exchange element if (! Lis. contains (t) {// Lis does not contain t queue. offer (t); // enter Lis. add (t); // Add record }}} return 0;} public static void main (string [] ARGs) {empty scan = new empty (system. in); system. out. println ("input integer N"); int n = scan. nextint (); scan. nextline (); // receives the carriage return string [] S = new string [N]; for (INT I = 0; I <n; I ++) {s [I] = scan. nextline (); // initial data} For (INT I = 0; I <n; I ++) {system. out. println (f (s [I]); // expected result }}}

 
 

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.