1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| public class Migong { public static void main ( String[] args ) { int[][] map = new int[ 8 ][ 7 ]; for ( int i = 0 ; i < 7 ; i++ ) { map[ 0 ][ i ] = 1; map[ 7 ][ i ] = 1; } for ( int i = 1 ; i < 7 ; i++ ) { map[ i ][ 0 ] = 1; map[ i ][ 6 ] = 1; } map[ 3 ][ 1 ] = 1; map[ 3 ][ 2 ] = 1; list ( map ); list ( map ); setWay2 ( map, 1, 1 );
list ( map );
}
private static void list ( int[][] map ) { System.out.println ("地图情况" ); for ( int i = 0 ; i < 8 ;i++) { for ( int j = 0 ; j < 7 ; j++ ) { System.out.print ( map[i][j]+" "); } System.out.println ( ); } }
public static boolean setWay ( int[][] map, int i, int j ) { if ( map[ 5 ][ 5 ] == 2 ) { return true; }else { if ( map[ i ][ j ] == 0 ) { map[ i ][ j ] = 2; if ( setWay ( map, i+1, j ) ) { return true; } else if ( setWay ( map, i, j + 1 ) ) { return true; }else if ( setWay ( map, i-1, j ) ){ return true; } else if ( setWay ( map, i, j - 1 ) ) { return true; } else { map[ i ][ j ] = 3; return false; } }else { return false;
} } }
public static boolean setWay2 ( int[][] map, int i, int j ) { if ( map[ 5 ][ 5 ] == 2 ) { return true; }else { if ( map[ i ][ j ] == 0 ) { map[ i ][ j ] = 2; if ( setWay2 ( map, i-1, j ) ) { return true; } else if ( setWay2 ( map, i, j + 1 ) ) { return true; }else if ( setWay2 ( map, i+1, j ) ){ return true; } else if ( setWay2 ( map, i, j - 1 ) ) { return true; } else { map[ i ][ j ] = 3; return false; } }else { return false;
} } }
}
|