解题思路:
注意事项:
参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n=scanner.nextInt(); int[][] offset={{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2}};//8个方向的坐标偏移量,从右上逆时针开始 while(n-->0) { String s=scanner.next(); int row=(int)s.charAt(0)-'a'+1;//坐标从1开始算 int col=(int)s.charAt(1)-'0'; int lattices=0; for(int i=0;i<8;i++) {//遍历8个方向,判断是否出界 if(inChessBoard(row+offset[i][0],col+offset[i][1])) {//当前坐标加上偏移量等于跳转后的坐标 lattices++; } } System.out.println(lattices); } } //判断该点坐标是否在棋盘内 public static boolean inChessBoard(int row,int col) { if(row>=1 && row<=8 && col>=1 && col<=8) {//棋盘为8*8大小 return true; } return false; } }
0.0分
0 人评分