洛谷P1002
最后2AC 3TLE
第一次刷洛谷Java
这个题给我的教训是:
洛谷中类名应为Main 要不然编译不通过
这个题要把数组多定义2格,目的是防止定义马的控制点时导致数组越界
本题我使用的方法是倒退法,即从结尾出发递归调用函数往前走所以用时较长,看网上的佬都是正这做的
import java.util.Scanner; public class Main{ static int[][] map = new int[24][24]; static int bx,by, house_x,house_y; static long ans; public static void main(String[] args) { Scanner sc = new Scanner(System.in); bx = sc.nextInt() + 2;by=sc.nextInt() + 2; house_x = sc.nextInt() + 2;house_y = sc.nextInt() + 2; ans = 0L; int[][] horse = {{0,0},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}}; for (int i = 0; i < 9; i++) { map[house_x + horse[i][0]][house_y + horse[i][1]] = 1; } f(bx,by); System.out.println(ans); sc.close(); } public static void f(int x,int y){ if(x==2 && y==2){ ans++; } if(map[x][y]==1){ }else{ if(x != 0){ f(x-1,y); } if(y != 0){ f(x,y-1); } } } }