兰顿蚂蚁这题,是个模拟题,也可以是DFS。
关键注意两点:
一、无论用String还是char[]来表示方向组,ULDR的顺序不是随便乱写的
二、是先转向,再向前走,反过来就错了
//这段代码,系统提示有运行时错误......
//尚未发现是什么原因,醉了,后面的测试数据也不让看...
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n= sc.nextInt();
int[][] a = new int[m][n];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
a[i][j] = sc.nextInt();
}
}
int x = sc.nextInt();
int y = sc.nextInt();
char s = sc.next().charAt(0);
int k = sc.nextInt();
char[] direction = {'U', 'R', 'D', 'L'};
int flag = 0; //表示第几个字符
for (int i=0;i<direction.length;i++){
if (s == direction[i]){
flag = i;
}
}
for (int i=0; i<k; i++){
if (a[x][y] == 1){
a[x][y] = 0;
flag = flag + 1;
}else{
a[x][y] = 1;
flag = flag - 1;
}
s = direction[(flag+4)%4];
switch (s){
case 'L':
y--;
break;
case 'R':
y++;
break;
case 'U':
x--;
break;
case 'D':
x++;
break;
}
}
System.out.println(x +" " +y);
}
}
//AC代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[] lz = { 'L', 'D', 'R', 'U' }; // 左转方向数组
char[] rz = { 'L', 'U', 'R', 'D' }; // 右转方向数组
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] maps = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
maps[i][j] = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
char s = in.next().trim().charAt(0);
int k = in.nextInt();
for (int j = 0, i = 0; j < k; j++) {
if (maps[x][y] == 1) { // 黑色格子 右转90度
maps[x][y] = 0; // 置为白色格子
for (i = 0; i < 4; i++)
if (rz[i] == s)
break;
s = rz[(i + 1) % 4];
switch (s) {
case 'L':
y--;
break;
case 'R':
y++;
break;
case 'U':
x--;
break;
case 'D':
x++;
break;
}
} else { // 白色格子 左转90度
maps[x][y] = 1; // 置为黑色格子
for (i = 0; i < 4; i++)
if (lz[i] == s)
break;
s = lz[(i + 1) % 4];
switch (s) {
case 'L':
y--;
break;
case 'R':
y++;
break;
case 'U':
x--;
break;
case 'D':
x++;
break;
}
}
}
System.out.println(x + " " + y);
}
}
//AC代码
import java.util.*;
class Main
{
public static void main(String[] arge)
{
Scanner cin = new Scanner(System.in);
int x = cin.nextInt();
int y = cin.nextInt();
//方向
String go = "ULDR";
//地图
int[][] map = new int[x][y];
//输入
for(int i = 0;i<x;i++)
for(int j = 0;j<y;j++)
map[i][j] = cin.nextInt();
//初始位置及方向
x = cin.nextInt();
y = cin.nextInt();
char d = cin.next().charAt(0);
//将方向用下标表示,便于旋转计算下一步的方向
int god = go.indexOf(d);
int num = cin.nextInt();
int temp = 0;
//爬起来
for(int i = 0;i<num;i++)
{
//改变地图,旋转方向
if(map[x][y] == 0)
{
map[x][y] = 1;
temp = -1;
}
else
{
map[x][y] = 0;
temp = 1;
}
//爬一步
god = (god-temp+4)%4;
switch(go.charAt(god))
{
case 'U':x--;break;
case 'L':y--;break;
case 'D':x++;break;
case 'R':y++;break;
}
}
sop1(x+" "+y);
}
public static void sop1(Object obj)
{
System.out.println(obj);
}
}