问题描述
import java.util.Scanner;
public class Main {
public static int n,m,x,y,k,temp;
public static String s;
public static int[][] a;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
a = new int[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
a[i][j] = sc.nextInt();
}
}
x = sc.nextInt();
y = sc.nextInt();
s = sc.next();
k = sc.nextInt();
temp = 0;
move(x,y);
}
//0 表示白格,1 表示黑格
//若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格;
//若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格。
//行号从上到下增长,列号从左到右增长
public static void move(int x,int y)
{
if(temp == k)
{
System.out.println(x + " " + y);
}
else
{
if(s.equals("U"))
{
if(a[x][y] != 0)
{
s = "R";
a[x][y] = 0;
y++;
}
else
{
s = "L";
a[x][y] = 1;
y--;
}
temp++;
}
else if(s.equals("D"))
{
if(a[x][y] != 0)
{
s = "L";
a[x][y] = 0;
y--;
}
else
{
s = "R";
a[x][y] = 1;
y++;
}
temp++;
}
else if(s.equals("L"))
{
if(a[x][y] != 0)
{
s = "U";
a[x][y] = 0;
x--;
}
else
{
s = "D";
a[x][y] = 1;
x++;
}
temp++;
}
else if(s.equals("R"))
{
if(a[x][y] != 0)
{
s = "D";
a[x][y] = 0;
x++;
}
else
{
s = "U";
a[x][y] = 1;
x--;
}
temp++;
}
move(x, y);
}
}
}
原文:http://blog.csdn.net/l243225530/article/details/45542997