Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
适用于可用空间的矩阵的Java搜索算法_Java_Data Structures - Fatal编程技术网

适用于可用空间的矩阵的Java搜索算法

适用于可用空间的矩阵的Java搜索算法,java,data-structures,Java,Data Structures,我有一个类似于矩阵的空间 |.... ..|.. ..... ||... 其中,为空空间,|为非空空间 我有3个形状像 TTT E.E ..L ..E L.L EEE 也为空。现在我想用java代码在空间矩阵中拟合这些形状 预期的外部空间看起来像 |.ELE .L|LE ..EEE ||TTT 可能是其他适合

我有一个类似于矩阵的空间

|....
..|..
.....
||...
其中,
为空空间,
|
为非空空间

我有3个形状像

TTT                 E.E                   ..L
                    ..E                   L.L
                    EEE
也为空。现在我想用java代码在空间矩阵中拟合这些形状

预期的外部空间看起来像

|.ELE
.L|LE
..EEE
||TTT
可能是其他适合的可能性

输入文件代码“problem1.txt”

这里是我读取数组中文件的代码

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);
       }
      }
File f=新文件(“problem1.txt”);
FileReader fr=新的FileReader(f);
int pos=0;
扫描仪s=新扫描仪(fr);
//太空舱
SpaceClass space=新的SpaceClass();
List shapeClasses=新建ArrayList();
Shapeclass Shapeclass=null;
布尔空格=假;
而(s.hasNext()){
//读取修剪线
字符串行=s.nextLine().trim();
如果(“.”等于(行)){
继续;
}
//检查空间
if(“SPACE”.equals(line.trim().toUpperCase())){
pos=-1;
spaceStated=true;
继续;
}
//检查空间
if(line.trim().toUpperCase().startsWith(“形状”)){
pos=-1;
空格=假;
shapeClass=新的shapeClass();
添加(shapeClass);
继续;
}
pos++;
//增加空间
如果(已声明){
空格.addRow(位置,行);
}否则{
//添加形状
shapeClass.addRow(位置、行);
}
}
太空舱看起来像

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;
        }
    }

}
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;
        }
    }

}
公共类SpaceClass{
私有整数高度=0;
私有整数宽度=0;
私有字符[][]s=新字符[50][50];
公共空间类(){}
public void addRow(int rowNo,String row)引发异常{
如果(this.width>0&&this.width!=row.length()){
抛出新异常(“行:+rowNo+”和行:-”+row处的空格输入无效);
}否则{
char[]ch=row.toCharArray();
for(inti=0,j=ch.length;i0&&this.width!=row.length()){
抛出新异常(“行:+rowNo+”和行:-”+row处的空格输入无效);
}否则{
char[]ch=row.toCharArray();

对于(int i=0,j=ch.length;i)您的问题是什么?您需要处理的最大可能的矩阵空间是什么?只有3个固定的形状还是可能有更多的形状?我有空格char[][]数组。和3个Shape char[][]数组。现在我想创建一个新数组,以适应空间array@user3437460可能更适合形状,但适合空间矩阵大小始终为4 x 5?
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;
        }
    }

}