解题思路:
思路很简单,按着题目的要求书写代码即可
注意事项:
使用数组时要注意列和行的问题,不要把列行弄混。
参考代码:import java.util.Scanner;
public class Main {
//变换蚂蚁头的方向
public static char getHead(char fx,int color){
char head = 0;
if(color == 1){
switch (fx) {
case 'U':
head='R';
break;
case 'D':
head='L';
break;
case 'R':
head='D';
break;
case 'L':
head='U';
break;
default:
break;
}
}
else{
switch (fx) {
case 'U':
head='L';
break;
case 'D':
head='R';
break;
case 'R':
head='U';
break;
case 'L':
head='D';
break;
default:
break;
}
}
return head;
}
public static int getX(char fx, int color, int x){
char c = getHead(fx, color);
if(c=='U')
return x-1;
else if(c=='D')
return x+1;
else
return x;
}
public static int gety(char fx, int color, int y){
char c = getHead(fx, color);
if(c=='L')
return y-1;
else if(c=='R')
return y+1;
else
return y;
}
public static void showMap(int a[][],int m,int n){
for(int i=0; i
for(int j=0; j
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("________________________");
}
public static void test(){
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
// int a[][] = {
// {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}};
int a[][] = new int[m][n];
for(int i=0; i
for(int j=0; j
a[i][j]=scan.nextInt();
}
}
int x = scan.nextInt(); //蚂蚁坐标x,y
int y = scan.nextInt();
String s = scan.next();
char fx = s.charAt(0); //方向
int k = scan.nextInt(); //步数
int newx,newy;
for(int i=0; i
//得到下一步
newx = getX(fx, a[x][y], x);
newy = gety(fx, a[x][y], y);
//变换头方向
fx = getHead(fx, a[x][y]); //得到下一步之后切记要更新fx,
//不然fx一直是最初赋予的值
//将当前方块颜色变色
if(a[x][y]==1)
a[x][y]=0;
else
a[x][y]=1;
//移动蚂蚁
x=newx;
y=newy;
// showMap(a, m, n);
}
System.out.println(x+" "+y);
}
public static void main(String[] args){
test();
}
}