Previous questions Langton ant time limit: 1.0s memory limit: 256.0MBProblem 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
Simulate every step of the ant, see the code implementation
AC Code:
#include <cstdio> #include <cstring> #include <string> #include <algorithm>using namespace std; int m, n;int map[105][105];int xx[4] = {-1, 0, 1, 0}, Yy[4] = {0, 1, 0,-1}; int main () {while (scanf ("%d%d", &m, &n)! = EOF) {for (int i = 0; i < m; i++) for (int j = 0; J < N; j + +) scan F ("%d", &map[i][j]), int x, y, K;char s[5];scanf ("%d%d%s%d", &x, &y, S, &k), int po;//toward if (s[0] = = ' U ') PO = 0; else if (s[0] = = ' R ') po= 1;else if (s[0] = = ' D ') po= 2;else if (s[0] = = ' L ') po= 3;while (k) {if (map[x][y] = = 0) {if (PO = = 0) PO = 3;else po--;} else {po = (PO + 1)% 4;} Map[x][y] = map[x][y]^1;x + = xx[po];y + = yy[po];k--; printf ("%d%d\n", x, y);} return 0;}
Blue Bridge Cup-Langton Ant (analogue)