这道题是经典的bfs,可以走八个方向,所以定义方向数组,这个题唯一不一样的是初始的x,y 是类似于坐标轴的表示形式,需要先把xy换成平常的。

有几个细节点要注意
1.起点入队的时候记得标记st数组
2.ans初始为1 因为起点也算
剩下的就是bfs的常规操作,在k>0的时候,先判断队列是否不为空,若不为空就取出队头,然后遍历下一个坐标点,符合条件的进行入队,并且ans++;

import java.util.*;
class Main {
private static final int[][]D = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();

        //bfs其实是
        //11变成从下往上的了
        int [][]m = new int [n+1][n+1];
        y = n+1-y;  //比如说33   其实是32   11 其实是 14 
        //下标都从1 开始了
        int [][]st = new int [n+1][n+1];
        List<int []>q = new ArrayList<>();
        q.add(new int[] {x,y});
        st[x][y] = 1;
        int ans=1;
        while(k>0 && !q.isEmpty()) {
            k--;
            List<int []>t = q;
            q = new ArrayList<>();
            for(int []pos :t) {
                for(int []d :D) {
                    int i = pos[0]+d[0];
                    int j = pos[1]+d[1];
                    if(i>=1 && i<n+1 && j>=1 && j<n+1 && st[i][j]==0) {
                        st[i][j]=1;
                        q.add(new int[] {i,j});
                        ans++;
                    }
                }
            }
        }
        System.out.println(ans);
    }
}