目录
题目
4797. 移动棋子 - AcWing题库https://www.acwing.com/problem/content/4800/
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int x = 0;
int y = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
int map = input.nextInt();
if(map == 1){
x = i;
y = j;
}
}
}
System.out.println(Math.abs(x - 2) + Math.abs(y - 2));
}
}
AC结果
思路
棋盘固定大小是5×5的,所以中心点的坐标很容易确定就是[2,2]。然后读取数值到一个二维数组中,记录值为“1”的下标。然后让让下标的两个值分别与“2”作差,并取绝对值然后相加。这个就是最段距离。