UVa227

I didn't look at the input and output styles of the original title, the questions I saw in the purple book, if there is something wrong, you are welcome to ask questions

import java.util.Scanner;
/*
 * 有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把上,下,左,右移入网格中,
 */
public class Test5 {
 public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  String s = sc.next();
  char c [] = s.toCharArray();
  char cc[][] = new char[5][5]; //存放表格数据
  int m = 0,n = 0;
  for(int i = 0; i <5; i++){ // 输入的数据分配为5*5表格
   cc[0][i] = c[i];
   cc[1][i] = c[i+5];
   cc[2][i] = c[i+10];
   cc[3][i] = c[i+15];
   cc[4][i] = c[i+20];
  }
  for(int i = 0; i < 5; i++) // 寻找到为‘_’的元素
   for(int j = 0; j < 5; j++)
    if(cc[i][j] == '_'){
     m = i;
     n = j;
    }
  for(int i = 25; i < s.length()-1; i++){
   if(c[i] == 'A'){
    char t;
    t = cc[m-1][n];
    cc[m-1][n] = cc[m][n];
    cc[m][n] = t;
    m--; // 进行一次操作后 元素为‘_’的格子已经变了,所以我们要改变这个值
   }
   if(c[i] == 'B'){
    char t;
    t = cc[m+1][n];
    cc[m+1][n] = cc[m][n];
    cc[m][n] = t;
    m++;
   }
   if(c[i] == 'L'){
    char t;
    t = cc[m][n-1];
    cc[m][n-1] = cc[m][n];
    cc[m][n] = t;
    n--;
   }
   if(c[i] == 'R'){
    char t;
    t = cc[m][n+1];
    cc[m][n+1] = cc[m][n];
    cc[m][n] = t;
    n++;
   }
   if(c[i] != 'R' && c[i] != 'L'&& c[i] != 'B'&& c[i] != 'A')
    System.out.println("This puzzle has no final configuration");
  }
  for(int i = 0; i < 5; i++){
   for(int j = 0; j < 5; j++)
          System.out.print(cc[i][j]);
   System.out.println();
  }
 }
}

The topic should not be difficult, if you have any questions, please ask! It ’s a pity to only do one question today.

Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/102539792