DFS

P1002 [NOIP2002 普及组] 过河卒

代码发一下?
by Monkey_Hunter @ 2020-02-27 13:27:04


写错了呗,你不给我们代码,我们怎么知道为什么写错了
by Islauso @ 2020-02-27 13:28:40


~~为什么要写dfs呢~~
by Callous_Murder @ 2020-02-27 13:32:14


~~递推它不香吗~~
by critnos @ 2020-02-27 13:33:03



by team109 @ 2020-02-27 13:33:30


代码呢
by 血色黄昏 @ 2020-02-27 13:57:04


递推呀
by zhjzhmh @ 2020-02-27 14:08:17


这是代码 ```java import java.util.Scanner; public class Main { static int[][] a = new int[50][50]; static int ans = 0, endx, endy; static int[] xx = {1,0}; static int[] yy = {0,1}; public static void main(String[] args) { Scanner in = new Scanner(System.in); endx = in.nextInt(); endy = in.nextInt(); int hx = in.nextInt(); int hy = in.nextInt(); a[hx][hy] = 1; if( hx-1>=0 ) a[hx-1][hy+2] = 1; if( hx-1>=0 && hy-2>=0 ) a[hx-1][hy-2] = 1; if( hx-2>=0 ) a[hx-2][hy+1] = 1; if( hx-2>=0 && hy-1>=0 ) a[hx-2][hy-1] = 1; if( hy-1 >= 0 ) a[hx+2][hy-1] = 1; if( hy-2>=0 ) a[hx+1][hy-2] = 1; a[hx+1][hy+2] = 1; a[hx+2][hy+1] = 1; dfs(0, 0); System.out.println(ans); in.close(); } public static void dfs(int o, int p) { int x, y; if( o==endx && p==endy ) { ans++; return; } else { for( int i=0; i<2; i++ ) { x = o + xx[i]; y = p + yy[i]; if( x>=0 && x<=endx && y>=0 && y<=endy && a[x][y]==0 ) { a[x][y]=1; dfs(x, y); a[x][y]=0; } } } } } ```
by lyd_try @ 2020-03-07 17:36:34


@[自动WA机私信我](/user/249786) ```java import java.util.Scanner; public class Main { static int[][] a = new int[50][50]; static int ans = 0, endx, endy; static int[] xx = {1,0}; static int[] yy = {0,1}; public static void main(String[] args) { Scanner in = new Scanner(System.in); endx = in.nextInt(); endy = in.nextInt(); int hx = in.nextInt(); int hy = in.nextInt(); a[hx][hy] = 1; if( hx-1>=0 ) a[hx-1][hy+2] = 1; if( hx-1>=0 && hy-2>=0 ) a[hx-1][hy-2] = 1; if( hx-2>=0 ) a[hx-2][hy+1] = 1; if( hx-2>=0 && hy-1>=0 ) a[hx-2][hy-1] = 1; if( hy-1 >= 0 ) a[hx+2][hy-1] = 1; if( hy-2>=0 ) a[hx+1][hy-2] = 1; a[hx+1][hy+2] = 1; a[hx+2][hy+1] = 1; dfs(0, 0); System.out.println(ans); in.close(); } public static void dfs(int o, int p) { int x, y; if( o==endx && p==endy ) { ans++; return; } else { for( int i=0; i<2; i++ ) { x = o + xx[i]; y = p + yy[i]; if( x>=0 && x<=endx && y>=0 && y<=endy && a[x][y]==0 ) { a[x][y]=1; dfs(x, y); a[x][y]=0; } } } } } ```
by lyd_try @ 2020-03-07 17:37:24


@[Segment_Tree_](/user/183235) ```java import java.util.Scanner; public class Main { static int[][] a = new int[50][50]; static int ans = 0, endx, endy; static int[] xx = {1,0}; static int[] yy = {0,1}; public static void main(String[] args) { Scanner in = new Scanner(System.in); endx = in.nextInt(); endy = in.nextInt(); int hx = in.nextInt(); int hy = in.nextInt(); a[hx][hy] = 1; if( hx-1>=0 ) a[hx-1][hy+2] = 1; if( hx-1>=0 && hy-2>=0 ) a[hx-1][hy-2] = 1; if( hx-2>=0 ) a[hx-2][hy+1] = 1; if( hx-2>=0 && hy-1>=0 ) a[hx-2][hy-1] = 1; if( hy-1 >= 0 ) a[hx+2][hy-1] = 1; if( hy-2>=0 ) a[hx+1][hy-2] = 1; a[hx+1][hy+2] = 1; a[hx+2][hy+1] = 1; dfs(0, 0); System.out.println(ans); in.close(); } public static void dfs(int o, int p) { int x, y; if( o==endx && p==endy ) { ans++; return; } else { for( int i=0; i<2; i++ ) { x = o + xx[i]; y = p + yy[i]; if( x>=0 && x<=endx && y>=0 && y<=endy && a[x][y]==0 ) { a[x][y]=1; dfs(x, y); a[x][y]=0; } } } } } ```
by lyd_try @ 2020-03-07 17:37:45


| 下一页