20,200,310 of Langton's Ant (analog)

Title Description
Here Insert Picture Description
Langton's Ant, was in 1986, by Chris Langton proposed, belonging to cellular automata.
A square lattice plane is black or white fill. In which a grid square has an "ant."
Ants head orientation is: wherein the left and right vertical side.
Ants move rule is very simple:
if ants Haig, 90 degrees right turn, the white cell to cell, and a forward cell;
if ants white cells, 90 degrees left, to the black grid grid, and move forward one space.
Rules are simple, but very complex behavior of ants. I left when just starting line, there will be nearly symmetrical, like a repeat, but regardless of the initial state,
the ants after a long chaotic activity, the rules would open up a "highway."
Ants route is very difficult to predict in advance.
Your job is the initial state, the Langton ant computer simulation in which the position of the left foot in the n.

Input
first line of input data lines is mn two integers (3 <m, n <100 ), the number of rows and columns of a square lattice.
Next is the m rows of data.
Each row of data is divided into n number of spaces. 0 represents a white cell, Haig 1 represents.
Next is the row of data: xysk, where xy is an integer, the ant to the row and column number (row number grew from top to bottom, left to right column number growth are numbered from 0). s is a capital letter, indicating the orientation of the head ants, we agreed: up and down respectively: expressed UDLR. k represents the number of steps for ants.

Outputting
output data is a space-separated integers PQ, respectively ant after k steps, in which the lattice row and column numbers.

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. 5 L

Sample Output
13

#include <bits/stdc++.h>
using namespace std;
int mapp[101][101];
//顺时针方向
int dir[4][2]={-1,0,0,1,1,0,0,-1};
//-1 0||0 1||1 0||0 -1 举例-1 0(行-1,列不变) 
//上移1右移1下移1左移1 
int main(){ 
	int n,m; //格子的行数和列数
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf("%d",&mapp[i][j]); //0表示白格,1表示黑格 
	int x,y,k;   //x,y是蚂蚁所在行、列  k蚂蚁走了几步 
	char s[10];  //蚂蚁头朝向,UDLR上下左右 
	scanf("%d %d %s %d",&x,&y,&s,&k);
	int base;
	//蚂蚁顺时针转的话就是base+1
	//逆时针base-1相当于+3
	if(s[0]=='U')
		base=0;
	else if(s[0]=='D')
		base=2;
	else if(s[0]=='L')
		base=3;
	else
		base=1;
	while(k--){
		if(mapp[x][y]==0) 	//白格 
			base=(base+3)%4;//向左90度 
		else
			base=(base+1)%4;//黑格向右90度 
		mapp[x][y]=!mapp[x][y]; //改格子颜色
		x+=dir[base][0];    //行移动
		y+=dir[base][1];    //列移动	
	}
	printf("%d %d\n",x,y);
	return 0; 
}

After this code I do not want to say anything on this code is a two-hour liver has also been slow to understand the place is that change color plaid =! Written! After = be careful ah!

Published 37 original articles · won praise 0 · Views 374

Guess you like

Origin blog.csdn.net/weixin_45351699/article/details/104783190