Algorithm Learning--Langton 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
The output data is two spaces separated by the integer p Q, respectively, the ant after the K-step, the row number 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
Sample input
7 ·
0 0 0
1 1 1
1 1 1
1 1 U 6
Sample output
0 0

#include <stdio.h>intMain () {intlist[ -][ -]={0}; intM,n,inch; inti,j,l; intx,y,k; CharC; scanf ("%d%d",&m,&N);  for(i=0; i<m;i++)    {         for(j=0; j<n;j++) {scanf ("%d",&inch); LIST[I][J]=inch; }} scanf ("%d%d%c%d",&x,&y,&c,&k);  for(l=0; l<k;l++)    {                if(list[x][y]==0)        {                    Switch(c) { Case 'U': c='L'; Break;  Case 'D': c='R'; Break;  Case 'L': c='D'; Break;  Case 'R': c='U'; Break; } List[x][y]=1; }        Else        {            Switch(c) { Case 'U': c='R'; Break;  Case 'D': c='L'; Break;  Case 'L': c='U'; Break;  Case 'R': c='D'; Break; } List[x][y]=0; }                Switch(c) { Case 'U': x--; Break;  Case 'D': x + +; Break;  Case 'L': y--; Break;  Case 'R': y++; Break; }} printf ("%d%d\n", x, y);return 0;}

Algorithm Learning--Langton 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.