题解 | 棋盘
棋盘
https://www.nowcoder.com/practice/b8e7884e2cba4646806a90c4a26a65c5
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
int[][] grid =new int[n][n];
for(int i = 0; i < n;i++) {
for(int j = 0; j < n;j++) {
grid[i][j] = in.nextInt();
}
}
int q = in.nextInt();
in.nextLine();
while(in.hasNextLine()) {
int left1 = in.nextInt();
int left2 = in.nextInt();
int right1 = in.nextInt();
int right2 = in.nextInt();
int[] ans = {-1,-1};
dfs(left1 - 1,left2 - 1,grid,
left1 - 1,left2 - 1,right1 - 1,right2 - 1,ans);
System.out.print(ans[0] + 1 + " ");
System.out.println(ans[1] + 1);
in.nextLine();
}
}
public static void dfs(int x, int y, int[][] grid,
int left1, int left2, int right1, int right2,int[] ans) {
int state = grid[x][y];
int nx = x + 1 - state;
int ny = y + state;
if(nx < 0 || nx >= grid.length
|| ny < 0|| ny >= grid[0].length ||
nx < left1 || nx > right1 ||
ny < left2 || ny > right2) {
ans[0] = x;
ans[1] = y;
return;
}
dfs(nx, ny, grid, left1,left2,right1,right2,ans);
}
}