莲花楼楼主


私信TA

用户名:dotcpp0719623

访问量:815

签 名:

总有人间一两风,填我十万八千梦

等  级
排  名 2188
经  验 2389
参赛次数 0
文章发表 31
年  龄 88
在职情况 学生
学  校 南京信息工程大学
专  业 海洋科学

  自我简介:

有人在通往牛逼的路上一路狂奔

解题思路:

注意事项:

参考代码:

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 人评分

  评论区

  • «
  • »