Blue Bridge Cup all the questions of the Rand ant

Source: Internet
Author: User

Problem description

Langton Ant, which was introduced by Chris Langton in 1986, belongs to a kind of cellular automata.

The square lattice on the plane is filled with black or white. There is an "ant" in one of the squares.
The head of the ant is facing: up and down on one side.

The rules for moving ants are very simple:
If the ant is in Haig, turn right 90 degrees, change the lattice to white, and move forward one cell;
If the ant is in white, turn left 90 degrees, change the grid to black, and move forward one cell.

Although the rules are simple, the behavior of ants is very complex. The route left at the beginning will be nearly symmetrical, like repeating, but no matter what the starting state is, the ants will open up a regular "freeway" after a lengthy chaotic activity.

The ant's route is difficult to predict beforehand.

Your task is to use a computer to simulate the position of the Langton Ant after the nth Walk, based on the initial state. Input format the first line of input data is m n two integers (3 < m, n < 100), representing the number of rows and columns of the square lattice.
Next is the M-row data.
Each row of data is n a number separated by a space. 0 means white and 1 for Haig.

Next is a row of data: x y S K, where x y is an integer, which indicates the ant's line number and column number (row numbers grow from top to bottom, column numbers grow from left to right, and are numbered starting with 0). S is a capital letter, indicating the head of the ant, we agreed: up and down respectively with: Udlr said. K indicates the number of steps the ant walks. Output format output data is two spaces separated by the integer p Q, respectively, the ant after the K-step, the row and column number of the lattice. Sample Input 5 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
2 3 L 5 Sample Output 1 3 example input 3 3
0 0 0
1 1 1
1 1 1
1 1 U 6 sample output 0 0
Basically no difficulty, according to the simulation conditions have been given to write code can be
Import Java.util.scanner;public class Main {public static int n,m,x,y,k,temp;public static String s;public static int[][] A;public static void Main (string[] args) {Scanner sc = new Scanner (system.in); n = sc.nextint (); m = Sc.nextint (); a = new int N [M];for (int i = 0; i < n; i++) {for (int j = 0; J < m; J + +) {A[i][j] = Sc.nextint ();}} x = Sc.nextint (); y = Sc.nextint (); s = Sc.next (); k = Sc.nextint (); temp = 0;move (x, y);} 0 means white lattice, 1 means black lattice//If the ant in Haig, turn right 90 degrees, the lattice will be changed to white, and move forward a grid;//If the ant is in white, turn left 90 degrees, change the lattice to black, and move forward a grid. Line numbers grow from top to bottom, column numbers grow from left to right, public static void move (int x,int y) {if (temp = = k) {System.out.println (x + "" + y);} Else{if (S.equals ("U")) {if (a[x][y]! = 0) {s = "R"; a[x][y] = 0;y++;} Else{s = "L"; a[x][y] = 1;y--;} temp++;} else if (s.equals ("D")) {if (a[x][y]! = 0) {s = "L"; a[x][y] = 0;y--;} Else{s = "R"; a[x][y] = 1;y++;} temp++;} else if (s.equals ("L")) {if (a[x][y]! = 0) {s = "U"; a[x][y] = 0;x--;} Else{s = "D"; a[x][y] = 1;x++;} temp++;} else if (s.equals ("R")) {if (a[x][y]! = 0) {s = "D"; a[x][y] = 0;x++;} Else{s = "U"; A[x][y]  = 1;x--;} temp++;} Move (x, y);}}}


Blue Bridge Cup all the questions of the Rand ant

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.