java兰顿蚂蚁解题思路_[蓝桥杯][2014年第五届真题]兰顿蚂蚁 (Java代码)

解题思路:

思路很简单,按着题目的要求书写代码即可

注意事项:

使用数组时要注意列和行的问题,不要把列行弄混。

参考代码: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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值