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 It is obvious that this is a recursive problem, just write too slow, write the deletion, delete write, about almost 1 hours. The idea is: at that time g[][] is 1 o'clock, clockwise to find the direction, is 0 is counterclockwise to find the direction, while writing a function to determine each of the walking around
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMax = the;6 intG[max][max];7 intx, y, K, N, M;8 intfx[4] = {-1,0,1,0};9 intfy[4] = {0,1,0, -1};//upper right Down leftTen Charpath[4] = {'L','U','R','D'}; One intChooseCharDirintGRA) A { - if(dir = ='L') - { the if(GRA) - return 0; - return 2; - } + Else if(dir = ='R') - { + if(GRA) A return 2; at return 0; - } - Else if(dir = ='U') - { - if(GRA) - return 1; in return 3; - } to Else if(dir = ='D') + { - if(GRA) the return 3; * return 1; $ } Panax Notoginseng } - voidDfsintXintYCharDirintCNT) the { + if(CNT = =k) A { theprintf"%d%d\n", x, y); + return; - } $ intGID = Choose (dir, g[x][y]);//judge the current direction to Dir, lattice G[x][y]. $ intGX = x +Fx[gid]; - intGy = y +Fy[gid]; - intSt; the for(st =0; PATH[ST]! = dir; st++);//St Find the current dir location - if(G[x][y])Wuyi { the //if it's 1, turn clockwise, and don't forget to change the grid state. -G[x][y] =!G[x][y]; WuDFS (GX, GY, path[(St +1) %4], CNT +1); - } About Else $ { - //if it's 0, the direction is counterclockwise, -G[x][y] =!G[x][y]; -DFS (GX, GY, path[(ST-1+4) %4], CNT +1); A } + the } - $ intMainintargcChar**argv) the { thescanf"%d%d", &n, &m); the for(inti =0; I < n; i++) the { - for(intj =0; J < M; J + +) inscanf"%d", &G[i][j]); the } the Chars; Aboutscanf"%d%d%s%d", &x, &y, &s, &k); the intSt; the for(inti =0; I <4; i++) the if(Path[i] = ='s') +St =i; -DFS (x, y, S,0); the return 0;Bayi}
View Code
Blue Bridge--Langton Ant