今天晚上我写了一个代码,它获得一个.txt文件的名称,它从命令行参数中获取。它将文本文件的每个字符放入一个数组中。 这是这些TEXTFILESjava - 检查数组
7 11
###########
# # #
# ### #
# # #
# K ### #
# # #
###########
我想检查是否有可能在K走过去每一个“”,“#”的是墙上的样品。所以,如果我在这里检查此拉特它应该打印假,因为那里有右侧墙壁上,而在这里
5 8
########
# K #
# ### #
# #
########
它应该是真实的。这是多远我得到了(对不起,德国的变量名称和注释):
import java.io.*;
public class unbenannt {
public static void main (String args[]) throws IOException {
FileReader fr = new FileReader(args[0]); //Dateipfad wird übergeben als Kommandozeilenarg.
BufferedReader br = new BufferedReader(fr);
String zeile1 = br.readLine(); //Zeile1 wird gelesen für x und y
String[] xypos = zeile1.split(" ");
int hoehe = Integer.parseInt(xypos[0]); //String=>int
int laenge = Integer.parseInt(xypos[1]);
String[][] spielfeld = new String[hoehe][laenge]; //0. Dim wird Anzahl Zeilen, 1. Dim wird die Länge d. Zeilen aber beides 0-basiert(!)
//Zeile einlesen.
String zeile = "";
int j = 0; //aktuelle zeile
while (zeile != null) {
zeile = br.readLine();
if(zeile != null) {
//System.out.print(j);
for(int i=0;i<laenge;i++){ //2. Schleife für Zeile ist j
spielfeld[j][i] = String.valueOf(zeile.charAt(i));
}
}
j = j +1;
}
//array ist definiert
//algorythmus: wenn an feld 2 weiße = true; ausgenommen wandnähe
String weissfeld = " ";
String schwarzfeld = "#";
String kassiofeld = "K";
//2basiert weil wandproblem:
//Y-Achse invertiert => -1 in höhe für Norden:
for(int posy = 2;posy < hoehe-1;posy = posy+1){
for(int posx = 2;posx < laenge-1;posx = posx+1){
if(false == schwarzfeld.equals(spielfeld[posy][posx])){
boolean resultN = schwarzfeld.equals(spielfeld[posy][posx-1]);
boolean resultO = schwarzfeld.equals(spielfeld[posy+1][posx]);
boolean resultS = schwarzfeld.equals(spielfeld[posy][posx+1]);
boolean resultW = schwarzfeld.equals(spielfeld[posy-1][posx]);
int fehlerzahl = 0;
if(resultN==true){
fehlerzahl = fehlerzahl+1; //fehlerzahl+1
}
if(resultO==true){
fehlerzahl = fehlerzahl+1; //fehlerzahl+1
}
if(resultS==true){
fehlerzahl = fehlerzahl+1; //fehlerzahl+1
}
if(resultW==true){
fehlerzahl = fehlerzahl+1; //fehlerzahl+1
}
if(fehlerzahl > 2){
System.out.println("Not all white spaces reachable.");
break; //
}
}
}
}
System.out.println("No error means success. Script finished.");
br.close();
}
}
我的解决方案是,如果每一个适宜步行的空间,还有其他2个步行的空间附近的北东南或西的所有空格都accessable。但我将不得不在外壁破例对所有的空间直接,因为它连接到空间有一个领域是不够的,例如
5 7
#######
#K #
# # # #
# # # #
#######
这应该是真实的,但它不是,因为在该领域下半部分只是连接到墙上,但仍然可以访问..在我的脚本中我不检查数组在索引[0] [n]和[1] [n]以及不检查[n] [0] & & [ N] [1]所以我不得到这个错误,但我想..也是在我的算法难道不工作,将是这样一个情况:
8 11
###########
# #
# ###### #
# #K # #
# # # #
# ###### #
# #
###########
也许你有这么我的想法如何改善代码..即时通讯初学者,只能说2周的Java ..预先感谢和阅读,直到下面:)
你的代码不能用于像最后一个例子那样具有特定属性的迷宫。使用洪水填充(https://en.wikipedia.org/wiki/Flood_fill)来取得正确的结果 – Paul
我怎样才能将它实现到代码中?有没有一个脚本可以导入或我必须自己做? – siryx
填充填充是这类问题的一个众所周知的解决方案;在SO搜索框中输入“填充”会返回超过一百个问题;应该很容易找到一个可以帮助您在代码中执行下一步的程序。 – m69