[Blue Bridge Cup] Langton's Ant (dfs simple template title)

Title Description

Langton's Ant, was in 1986, presented by Chris Langton come, 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.

Entry

The 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.

Export

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

Sample Output

1 3

Problem-solving ideas

A simple search dfs, pay attention to the ranks of the matrix number is zero-based

Code

package 兰顿蚂蚁;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int m=in.nextInt();
		int n=in.nextInt();
		int [][]a=new int[110][110];
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				a[i][j]=in.nextInt();
			}
		}
		int x,y,k;
		x=in.nextInt();
		y=in.nextInt();
		char s = in.next().charAt(0);
		k=in.nextInt();
		//System.out.println(x+" "+y+" "+k);
		dfs(x,y,s,k,a);
		
	}
	public static void dfs(int x,int y,char s,int k,int a[][])
	{
		if(k==0)
		{
			System.out.println(x+" "+y);
			return ;
		}
		//System.out.println(a[x][y]);
		//System.out.println(x+" "+y);
		if(a[x][y]==1)
		{
			a[x][y]=0;
			if(s=='U')
			{
				dfs(x,y+1,'R',k-1,a);
			}
			if(s=='R')
			{
				dfs(x+1,y,'D',k-1,a);
			}
			if(s=='D')
			{
				dfs(x,y-1,'L',k-1,a);
			}
			if(s=='L')
			{
				dfs(x-1,y,'U',k-1,a);
			}
		}
		else
		{
			a[x][y]=1;
			if(s=='U')
			{
				dfs(x,y-1,'L',k-1,a);
			}
			if(s=='R')
			{
				dfs(x-1,y,'U',k-1,a);
			}
			if(s=='D')
			{
				dfs(x,y+1,'R',k-1,a);
			}
			if(s=='L')
			{
				dfs(x+1,y,'D',k-1,a);
			}
		}
		
	}

}

Published 20 original articles · won praise 0 · Views 3859

Guess you like

Origin blog.csdn.net/weixin_44544406/article/details/104293762