读取一个 n x n 网格的输入文件,并读取空白位置。

huangapple go评论58阅读模式
英文:

Reading an input file n x n grid and reading empty space

问题

这是您提供的代码和问题的中文翻译:

我正在尝试为一个 n x n 的十五数码滑动拼图求解器读取输入文件目前实际的解决方法并不重要因为我甚至无法通过输入文件的读取部分

将会发送一些拼图板的输入文件它们看起来像这样

3
 5  1  2
 8     3
 4  6  7

顶部的数字是 nn x n 网格)。不能更改输入文件所以无法通过将空格表示为 0 然后正常读取来简化问题我想要能够将其读入代码中形成一个类似这样的二维数组

5 1 2
8 0 3
4 6 7

0 代表代码中的 (1, 1) 处为空白格

输入文件中数字之间的空格数也有点奇怪有时是 2 个空格有时是 1 个空格例如这是另一个较大的输入文件):

9
38 51 15 17 11 27  8 36  7
19 40 28 10 14  4 77 26  9
43  2  5 18 71 22 20 32 13
37 34 76  6 48 25 35 44 53
 1 56 65 39 70 24 62 79 42
30 74 68    58 55 45 60 33
67 29 75  3 41 59 73 63 80
47 12 31 49 50 61 57 72 16
66 23 64 21 78 54 69 52 46

我尝试了这个

```java
File input = new File(args[0]);
Scanner scanner = new Scanner(input);

int n = scanner.nextInt();
scanner.nextLine();
int[][] board = new int[n][n];
int emptyRow = -1;
int emptyCol = -1;

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (scanner.hasNextInt()) {
            board[i][j] = scanner.nextInt();
        } else {
            // 空格
            board[i][j] = 0;
            emptyRow = i;
            emptyCol = j;
        }
    }
}

对于上面的 3x3 示例,它输出如下:

5 1 2
8 3 4
6 7 0

因此,它甚至不知道空格位于什么位置;我不确定在这里该怎么做。

希望这有助于解决您的问题。如果您需要进一步的帮助,请随时告诉我。

英文:

I am trying to read an input file for something for an n x n fifteen sliding puzzle solver. The actual solving is not important right now because I can't even get through the input file reading part.

A number of board input files will be sent in and they look like this:

3
5  1  2
8     3
4  6  7

the top number is n (n x n grid). The input files cannot be changed so I can't make my life easier by representing the space with a 0 and then reading normally. I want to be able to read it into the code as a 2d array like so:

5 1 2
8 0 3
4 6 7

(0 representing the empty space at (1, 1) in the code)

The exact number of spaces between the numbers are also kind of weird in the input files because sometimes it's 2 spaces and sometimes it's 1 space (e.g. here's another larger input file):

9
38 51 15 17 11 27  8 36  7
19 40 28 10 14  4 77 26  9
43  2  5 18 71 22 20 32 13
37 34 76  6 48 25 35 44 53
1 56 65 39 70 24 62 79 42
30 74 68    58 55 45 60 33
67 29 75  3 41 59 73 63 80
47 12 31 49 50 61 57 72 16
66 23 64 21 78 54 69 52 46

I tried this:

File input = new File(args[0]);
Scanner scanner = new Scanner(input);

int n = scanner.nextInt();
scanner.nextLine();
int[][] board = new int[n][n];
int emptyRow = -1;
int emptyCol = -1;

for (int i = 0; i < n; i++) {
	for (int j = 0; j < n; j++) {
		if (scanner.hasNextInt()) {
			board[i][j] = scanner.nextInt();
		} else {
			// Empty space
			board[i][j] = 0;
			emptyRow = i;
			emptyCol = j;
		}
	}
}

It outputs this for the example 3x3 above:

5 1 2 
8 3 4
6 7 0

So, it doesn't even know at what position the empty space is; I'm not sure what to do here.

答案1

得分: 0

如果固定数量(spaceNum)的空格分隔输入数据的每一行,你可以读取每行n次,并将每行分成spaceNum个空格。

这里是一个示例:

for (int i = 0; i < n; i++) {
    int[] row = Arrays.stream(scanner.nextLine().split(" {"+spaceNum+"}")).mapToInt(s -> {
        try{
            return Integer.parseInt(s.trim());
        }catch (NumberFormatException e){
            return 0;
        }
    }).toArray();

    board[i] = row;
}
英文:

If fixed number (spaceNum) spaces split each row of the input data, you can read each line n times and divide each line by the spaceNum spaces.

Here is a sample:

for (int i = 0; i &lt; n; i++) {
    int[] row = Arrays.stream(scanner.nextLine().split(&quot; {&quot;+spaceNum+&quot;}&quot;)).mapToInt(s -&gt; {
        try{
            return Integer.parseInt(s.trim());
        }catch (NumberFormatException e){
            return 0;
        }
    }).toArray();

    board[i] = row;
}

答案2

得分: 0

这应该适用于你的示例。思路是分别处理每一行,找到其中的所有数字。

如果数字不足,那么该行包含一个空单元格。在这种情况下,我们应该找到它的位置并跳过它。

英文:

This should work for your examples. The idea is to process each line separately, find all numbers in it.

If there are not enough numbers, then a line contains an empty cell. In this case we should find the location of it and skip it.

public static int[][] main(String[] args) throws FileNotFoundException {
    File input = new File(args[0]);
    Scanner scanner = new Scanner(input);

    int n = scanner.nextInt();
    scanner.nextLine();
    int[][] board = new int[n][n];
    int currentRow = 0;

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        var digits = Arrays.stream(line.split(&quot; &quot;))
                .filter(d -&gt; !Objects.equals(d, &quot;&quot;))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        int emptyCellIndex = -1;
        // found empty cell
        if (digits.size() != n) {
            emptyCellIndex = (line.indexOf(&quot;   &quot;) + 2) / 3;
        }
        int iter = 0;
        for (int i = 0; i &lt; n; i++) {
            if (i == emptyCellIndex) {
                continue;
            }
            board[currentRow][i] = digits.get(iter);
            iter++;
        }
        currentRow++;
    }
    return board;
}

huangapple
  • 本文由 发表于 2023年4月11日 12:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定