我有像
这样的空间矩阵|....
..|..
.....
||...
其中.
为空格,|
为非空格。
我有3个像
的形状TTT E.E ..L
..E L.L
EEE
.
也空了。现在我想使用java代码在空间矩阵中拟合这些形状。
预计空间看起来像
|.ELE
.L|LE
..EEE
||TTT
可能是其他合适的可能性
输入文件代码" problem1.txt"
SPACE
|.......
..|||..|
..|.|...
........
SHAPE T
TTTT
SHAPE e
..e
eee
SHAPE t
.t.
ttt
SHAPE r
rr
rr
SHAPE i
i..
iii
SHAPE s
.ss
ss.
这是我在数组中读取文件的代码
File f = new File("problem1.txt");
FileReader fr = new FileReader(f);
int pos = 0;
Scanner s = new Scanner(fr);
// space class
SpaceClass space = new SpaceClass();
List<Shapeclass> shapeClasses = new ArrayList<Shapeclass>();
Shapeclass shapeClass = null;
boolean spaceStated = false;
while(s.hasNext()) {
//read trim line
String line = s.nextLine().trim();
if("".equals(line)){
continue;
}
//CHECK For Space
if("SPACE".equals(line.trim().toUpperCase())) {
pos = -1;
spaceStated=true;
continue;
}
//check for space
if(line.trim().toUpperCase().startsWith("SHAPE")) {
pos = -1;
spaceStated=false;
shapeClass = new Shapeclass();
shapeClasses.add(shapeClass);
continue;
}
pos++;
//adding for space
if(spaceStated) {
space.addRow(pos, line);
}else{
//adding for shape
shapeClass.addRow(pos, line);
}
}
SpaceClass看起来像
public class SpaceClass{
private int height=0;
private int width=0;
private char[][] s = new char[50][50];
public SpaceClass() {}
public void addRow(int rowNo, String row) throws Exception {
if(this.width > 0 && this.width != row.length()) {
throw new Exception("Invalid Input for SPACE at row : " + rowNo + " and row :- " + row);
} else {
char[] ch = row.toCharArray();
for(int i=0,j=ch.length;i<j;i++) {
s[rowNo][i] = ch[i];
}
this.width = ch.length;
this.height += 1;
}
}
}
和ShapeClass看起来像
public class Shapeclass{
private int height;
private int width;
private char[][] s = new char[50][50];
int rotate=0;
public void addRow(int rowNo, String row) throws Exception {
if(this.width > 0 && this.width != row.length()) {
throw new Exception("Invalid Input for SPACE at row : " + rowNo + " and row :- " + row);
} else {
char[] ch = row.toCharArray();
for(int i=0,j=ch.length;i<j;i++) {
s[rowNo][i] = ch[i];
}
this.width = ch.length;
this.height += 1;
}
}
}
现在我想要一个适合所有形状的新Space类