我的问题如下:
我有一个大小为n x m的二维数组,输入一行。在接下来的n行中,有m个元素填充数组。到现在为止还挺好。
场上有一个棋子总是从场上唯一的0开始(假设总有一个0)。它可以上下左右移动。它总是移动到具有大多数硬币的相邻单元格,并且在每次移动时收集1个硬币(=>将被访问的单元格清空1)。 pawn执行此操作直到它周围只有0并且它不再收集任何东西。我需要找到收集的所有硬币的总和。
以下是Paint中第一步的表示:
硬币收集第一步:
样本输入:4 3,3 2 4,2 0 3,1 1 5,2 2 5 - >输出:22
这是我到目前为止的代码:我对targetCell有一些未完成的工作(我仍然想知道如何在循环中动态获取其坐标,以便每个具有比前一个值更大的值的单元格变为targetCell。)另外我是坚持使用我刚创建的方向。任何提示对我进一步发展任务都很有用。
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] my_array = input.split(" ");
int[] array = Arrays.stream(my_array).mapToInt(Integer::parseInt).toArray();
int n = array[0]; // rows of matrix
int m = array[1]; // cols of matrix
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
String[] numbers = line.split(" ");
matrix[i] = new int[m];
for (int j = 0; j < m; j++) {
matrix[i][j] = Integer.parseInt(numbers[j]);
}
}
int startPoint = 0;
int currentRow = 0;
int currentCol = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
startPoint = matrix[i][j];
currentRow = i;
currentCol = j;
}
}
}
int target1 = 0;
int target2 = 0;
int targetCell = 0;
target1 = Math.max(matrix[currentRow - 1][currentCol], matrix[currentRow + 1][currentCol]);
target2 = Math.max(matrix[currentRow][currentCol - 1], matrix[currentRow][currentCol + 1]);
targetCell = Math.max(target1, target2);
System.out.println(targetCell);
int hDirection = 1;
if (targetCol < currentCol) {
hDirection = -1;
}
int vDirection = 1;
if (targetRow < currentRow) {
vDirection = -1;
}
}
}
}
(不能评论所以现在会用答案。抱歉)
我的第一个想法是为运行保留一个全局变量,以便在收集硬币时将其添加到其当前值;类似于你在俄罗斯方块游戏中如何保持得分。那是假设我读得对。
所以类似于:
private static int current_score = 0; //Assuming no use of objects so using static
无法理解此示例中的示例输入。如果你能给出最终得分的三回合情景,我可以给你更好的洞察力。