这是我的代码,它完美无缺;但是,每当我提交它时它会给出错误的答案。有人知道为什么吗?
注意:我用2个额外的行和列填充矩阵,这样当我检查第一列的左边或最后一行的底部时,我没有收到错误。
public class Main {
public void convert(char[][] maze, int fieldNum) {
if (fieldNum != 1)
System.out.println();
System.out.println("Field #" + fieldNum + ":");
int n = maze.length;
int m = maze[0].length;
char[][] result = new char[n][];
for (int i = 0; i < n; i++) {
result[i] = new char[m];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
result[i][j] = '0';
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
if (maze[i][j] == '*') {
this.fill(result, i, j);
}
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
System.out.print(result[i][j]);
}
System.out.println();
}
}
private void fill(char[][] maze, int i, int j) {
if (maze[i-1][j-1] != '*')
maze[i-1][j-1] += 1;
if (maze[i][j-1] != '*')
maze[i][j-1] += 1;
if (maze[i+1][j-1] != '*')
maze[i+1][j-1] += 1;
if (maze[i-1][j] != '*')
maze[i-1][j] += 1;
maze[i][j] = '*';
if (maze[i+1][j] != '*')
maze[i+1][j] += 1;
if (maze[i-1][j+1] != '*')
maze[i-1][j+1] += 1;
if (maze[i][j+1] != '*')
maze[i][j+1] += 1;
if (maze[i+1][j+1] != '*')
maze[i+1][j+1] += 1;
}
public static void main(String[] args) throws IOException {
Main sweeper = new Main();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
int fieldNum = 1;
while ((line = reader.readLine()) != null) {
String[] xy = line.trim().split("\\s+");
int n = Integer.parseInt(xy[0]);
int m = Integer.parseInt(xy[1]);
if (n == 0 && m == 0)
break;
n += 2;
m += 2;
char[][] maze = new char[n][];
for (int i = 0; i < n; i++) {
maze[i] = new char[m];
}
for (int i = 1; i < n - 1; i++) {
line = reader.readLine();
for (int j = 1; j < n - 1; j++) {
maze[i][j] = line.charAt(j - 1);
}
}
sweeper.convert(maze, fieldNum);
fieldNum++;
}
}
}
答案 0 :(得分:2)
您可以使用this site为UVa问题的给定输入生成输出,并与您的解决方案进行比较,而不是告诉其他人调试长代码。它支持大约1500个问题。
答案 1 :(得分:0)
同样的问题,Minesweeper,也在URL http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110102&format=html
上如果您收到&#34;演示错误&#34;,请确保输出中的空行仅在字段#n和字段#n + 1之间。之前或之后应该没有空行。
以下是提供的示例输出&#39;必须以bash显示:
$ java Main <input
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
$
注意Field#2之前的单个空行:line。