解题思路:
注意要有第二个临时数组实时保存每一天同时进行战斗的,然后重新赋值给初始board,否则按顺序来战斗的话,会影响到初始情况
注意事项:
(我不知道为啥要在day的循环里先进行board2的赋值,而不是在ij里再进行赋值,按理说应该是一个道理,但是在ij里依次赋值不对,我也不知道为啥,希望有兄弟帮我解答)
此时的合理了,按理说board2是跟着board变的,所以不需要在循环里再次为board2赋值,直接最开始复制board即可
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
int col = scanner.nextInt();
int day = scanner.nextInt();
String[][] board = new String[row][col];
String[][] board2 = new String[row][col]; // Initialize for each day
for (int i = 0; i < row; i++) {
String[] line = scanner.next().split("");
System.arraycopy(line, 0, board[i], 0, col);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board2[i][j] = board[i][j];
}
}
for (int d = 0; d < day; d++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (j - 1 >= 0 && win(board[i][j], board[i][j - 1])) {
board2[i][j - 1] = board[i][j];
}
if (j + 1 < col && win(board[i][j], board[i][j + 1])) {
board2[i][j + 1] = board[i][j];
}
if (i - 1 >= 0 && win(board[i][j], board[i - 1][j])) {
board2[i - 1][j] = board[i][j];
}
if (i + 1 < row && win(board[i][j], board[i + 1][j])) {
board2[i + 1][j] = board[i][j];
}
}
}
for (int i = 0; i < row; i++) {
System.arraycopy(board2[i], 0, board[i], 0, col);
}
}
for (String[] strings : board) {
for (String string : strings) {
System.out.print(string);
}
System.out.println();
}
}
public static boolean win(String a, String b) {
return (a.equals("R") && b.equals("S")) || (a.equals("S") && b.equals("P")) || (a.equals("P") && b.equals("R"));
}
}
0.0分
1 人评分
矩阵乘法 (C++代码)浏览:1662 |
【亲和数】 (C语言代码)浏览:530 |
求圆的面积 (C语言代码)浏览:1366 |
蛇行矩阵 (C语言代码)浏览:792 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:562 |
C语言训练-大、小写问题 (C语言代码)浏览:792 |
wu-理财计划 (C++代码)浏览:907 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:609 |
A+B for Input-Output Practice (III) (C语言代码)浏览:594 |
简单的a+b (C语言代码)浏览:683 |