如何根据java中的输入创建一个遵循规则的2D字符串数组来创建不同的路径?
我有一个作业,我不知道如何用java来解决。该赋值需要一个程序,该程序需要5行输入,每行输入5个小写字母,中间没有空格。这些行中的每一行都将是数组中的一行。其中一个字母必须是a,一对是b和y之间的字母,其余的是z,被认为是“空”单元格。该程序旨在去除所有的z字母,用字母表中尚未使用的其余字母替换它们,并遵循从a开始的路径,在该路径中,下一个字母必须相邻。所以a必须紧挨着b,b必须紧挨着c。这不会更改输入中已存在的非z字母。例如:如何根据java中的输入创建一个遵循规则的2D字符串数组来创建不同的路径?,java,arrays,string,Java,Arrays,String,我有一个作业,我不知道如何用java来解决。该赋值需要一个程序,该程序需要5行输入,每行输入5个小写字母,中间没有空格。这些行中的每一行都将是数组中的一行。其中一个字母必须是a,一对是b和y之间的字母,其余的是z,被认为是“空”单元格。该程序旨在去除所有的z字母,用字母表中尚未使用的其余字母替换它们,并遵循从a开始的路径,在该路径中,下一个字母必须相邻。所以a必须紧挨着b,b必须紧挨着c。这不会更改输入中已存在的非z字母。例如: input output zzzzm
input output
zzzzm ijklm
zzzzz hgpon
zfzzz ---> efqrs
zzzaz dcbat
zzzzu yxwvu
潜在输入/输出的另一个示例:
如图所示,a后面紧跟着右边的b,依此类推,它环绕着数组,通过先前存在的字母编织出一条路径。我完全不知道该如何诚实地处理这件事。您如何使用预先存在的字母来确定字母通过的正确路径
到目前为止,我只知道:
public class AtoY {
public static void main(String[] args){
int row =-1; int col =-1;
String[][] arr = new String[5][5];
String L1 = "zzzzm";
String L2 = "zzzzz";
String L3 = "zfzzz";
String L4 = "zzzaz";
String L5 = "zzzzu";
for (int x=0; x<5; x++){
for (int y=0; y<5; y++){
if (x==0){
arr[x][y]=L1.substring(y, y+1);
if(arr[x][y]=="a"){
row=x;
col=y;
}
//System.out.println(L1.substring(y, y+1));
}
if (x==1){
arr[x][y]=L2.substring(y, y+1);
if(arr[x][y]=="a"){
row=x;
col=y;
}
//System.out.println(L2.substring(y, y+1));
}if (x==2){
arr[x][y]=L3.substring(y, y+1);
if(arr[x][y]=="a"){
row=x;
col=y;
}
//System.out.println(L3.substring(y, y+1));
}if (x==3){
arr[x][y]=L4.substring(y, y+1);
if(arr[x][y]=="a"){
row=x;
col=y;
}
//System.out.println(L4.substring(y, y+1));
//System.out.println(arr[x][y]);
}if (x==4){
arr[x][y]=L5.substring(y, y+1);
if(arr[x][y]=="a"){
row=x;
col=y;
}
//System.out.println(L5.substring(y, y+1));
}
}
}
}
公共类原子{
公共静态void main(字符串[]args){
int行=-1;int列=-1;
字符串[][]arr=新字符串[5][5];
字符串L1=“zzzzm”;
字符串L2=“zzzzz”;
字符串L3=“zfzzz”;
字符串L4=“zzzaz”;
字符串L5=“zzzzu”;
对于(int x=0;x,我会将您的所有字符放入2D数组:
arr[0]=L1.toCharArray();
arr[1]=L2.toCharArray();
arr[2]=L3.toCharArray();
arr[3]=L4.toCharArray();
arr[4]=L5.toCharArray();
然后我将编写一个方法,在数组中查找给定的字母,例如:
int[] findLetterCoordinate (char c)
最后,我会寻找“a”,找到它附近的第一个可用空间,放置一个“b”,然后递归地尝试。当你被卡住时,删除前一个字母并寻找另一个空间
这些都是想法,你是应该充实它的人。你可能听到你的导师咕哝着“回溯”和“递归”这两个词。我假设
是他们希望你锻炼的工具
您应该在二维char
数组中设置输入。这将允许您对
使用x和y坐标数组,而不必进行混乱的子字符串操作。作为设置的一部分,您可以
应验证输入(检查字符是否合法,检查重复字符,检查每行是否有五个字符
chars,检查是否有五行。)。然后找到a的坐标
您还需要一个支持函数来检查字符放置是否合法
它传递的y坐标在数组的边界内,并且坐标处的字符是z或
匹配候选角色
完成后,将初始状态(数组、起始字符和起始字符的坐标)传递给
递归函数
您的递归函数只需要放置一个字符(它所传递的字符后面的字符)
放置该字符时,它应该调用自己来放置下一个字符。如果它不能放置该字符,它应该
返回指示失败的标志。如果成功放置字符y,则应返回指示失败的标志
已经找到了解决办法
该函数必须循环通过其具有的每个可能的放置选择(输入的四个相邻位置
对于找到的每个合法位置,它需要更新数组并调用自身,传递新状态。如果
递归调用返回“false”,它需要回滚更改并尝试下一个位置(“回溯”位)
代码:
/**
* Enum to encapsulate the four possible directions to step in.
*/
enum Direction {
N(1, 0), S(-1, 0), E(0, 1), W(0, -1);
int dx;
int dy;
Direction(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
}
/**
* Place a character.
*
* @param state the current state of the puzzle
* @param currentChar the last character placed
* @param x the x coordinate of the last character placed
* @param y the y coordinate of the last character placed
*
* @return true if the solution was found
*/
public static boolean step(char[][] state, char currentChar, int x, int y) {
// Increment the character we're placing.
currentChar++;
// Try a step in each direction.
for (Direction direction: Direction.values()) {
// The coordinates to place the character.
int nx = x + direction.dx;
int ny = y + direction.dy;
// Is the placement legal?
if (checkPlacement(state, nx, ny, currentChar)) {
// Remember the old character in case we need to backtrack.
char oldChar = state[nx][ny];
// Record the step.
state[nx][ny] = currentChar;
// Stop if we're done, or recurse to perform the next step.
if (currentChar == 'y' || step(state, currentChar, nx, ny))
return true;
// Didn't find the solution on this branch.
// Backtrack and continue to try the next direction.
state[nx][ny] = oldChar;
}
}
// Tried all directions without success.
return false;
}
一些(重要)旁注:使用arr[x][y]。等于(“a”)
,而不是==a
。更改4(共5个)if
语句改为else if
,因为每个循环只检查一个x
值。也没有必要重复代码:将重复代码块放入一个方法中。为什么要给从未使用过的行和col
赋值?替换的逻辑对我来说不是很清楚:我假设e每一行都依赖于所有其他行,对吗?垂直和水平依赖性我也不清楚。