A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15567 Accepted Submission(s): 3886
Problem Description
可 怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃 公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。 现据密探 所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时 空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移 动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES
Source
题目的意思很清楚了,很明显的搜索题,根据题意枚举一下情况就好。本以为很容易过的题,没想到因为一个三维数组而一直TLE!!!!!所以这题目让我感受到了N维数组在效率上的影响是有多大!!!
先上AC代码。
import java.util.Scanner;public class Main { public static boolean flag = false; public static char[][] mg1; public static char[][] mg2; public static void main( String[] args ) { int n, m, t; int q; Scanner sc = new Scanner( System.in ); while( sc.hasNext() ) { q = sc.nextInt(); for( int c = 0; c < q; c++ ) { n = sc.nextInt(); m = sc.nextInt(); t = sc.nextInt(); flag = false; mg1 = new char[ n ][ m ]; mg2 = new char[ n ][ m ]; for( int ni = 0; ni < n; ni++ ) { mg1[ ni ] = sc.next().toCharArray(); } for( int ni = 0; ni < n; ni++ ) { mg2[ ni ] = sc.next().toCharArray(); } search( mg1, new Point2D( 0, 0 ), t ); if( flag ) { System.out.println( "YES" ); } else { System.out.println( "NO" ); } } } } /* * search和seach2 分别是两层迷宫的搜索,逻辑上基本是相同的,注意一下当传送之后的情况,如果是#和*都是死路就行了。 */ private static void search( char[][] mg1, Point2D point, int t ) { if( !constraint( point, mg1, t ) ) return; if( mg1[ point.n ][ point.m ] == '*' ) return; if( mg1[ point.n ][ point.m ] == 'P' ) { flag = true; } if( mg1[ point.n ][ point.m ] == '#' ) { if( mg2[ point.n ][ point.m ] == '#' || mg2[ point.n ][ point.m ] == '*' ) { return; } search2( mg2, new Point2D( point.n, point.m ), t ); } else { //设置为*,防止重复搜索,效果类似于visited标记数组。 mg1[ point.n ][ point.m ] = '*'; search( mg1, new Point2D( point.n, point.m + 1 ), t - 1 ); search( mg1, new Point2D( point.n, point.m - 1 ), t - 1 ); search( mg1, new Point2D( point.n + 1, point.m ), t - 1 ); search( mg1, new Point2D( point.n - 1, point.m ), t - 1 ); mg1[ point.n ][ point.m ] = '.'; } } private static void search2( char[][] mg2, Point2D point, int t ) { if( !constraint( point, mg2, t ) ) return; if( mg2[ point.n ][ point.m ] == '*' ) return; if( mg2[ point.n ][ point.m ] == 'P' ) { flag = true; } if( mg2[ point.n ][ point.m ] == '#' ) { if( mg1[ point.n ][ point.m ] == '#' || mg1[ point.n ][ point.m ] == '*' ) { return; } search( mg1, new Point2D( point.n, point.m ), t ); } else { mg2[ point.n ][ point.m ] = '*'; search2( mg2, new Point2D( point.n, point.m + 1 ), t - 1 ); search2( mg2, new Point2D( point.n, point.m - 1 ), t - 1 ); search2( mg2, new Point2D( point.n + 1, point.m ), t - 1 ); search2( mg2, new Point2D( point.n - 1, point.m ), t - 1 ); mg2[ point.n ][ point.m ] = '.'; } } private static boolean constraint( Point2D point, char[][] mg, int t ) { if( point.n < 0 || point.n >= mg.length || point.m < 0 || point.m >= mg[ 0 ].length ) { return false; } if( t < 0 ) { return false; } return true; }}class Point2D { public int n; public int m; public Point2D( int n, int m ) { this.n = n; this.m = m; }}
However,too young too naive~三维数组的效率相比二维数组真的慢好多好多,以后需要谨记,谨慎使用多维数组!
一下就是TLE的三维数组的代码
(超时代码)
import java.util.Scanner;public class Main { public static boolean flag = false; public static void main( String[] args ) { int n, m, t; int q; Scanner sc = new Scanner( System.in ); while( sc.hasNext() ) { q = sc.nextInt(); for( int c = 0; c < q; c++ ) { n = sc.nextInt(); m = sc.nextInt(); t = sc.nextInt(); flag = false; char[][][] mg = new char[ 2 ][ n ][ m ]; for( int i = 0; i < 2; i++ ) { for( int ni = 0; ni < n; ni++ ) { String s = sc.next(); mg[i][ni] = s.toCharArray(); } } search( mg, new Point( 0, 0, 0 ), t ); if( flag ) { System.out.println( "YES" ); } else { System.out.println( "NO" ); } } } } private static void search( char[][][] mg, Point point, int t ) { if( !constraint( point, mg, t ) ) return; if( mg[ point.c ][ point.n ][ point.m ] == '*' ) return; if( mg[ point.c ][ point.n ][ point.m ] == 'P' ) { flag = true; } if( mg[ point.c ][ point.n ][ point.m ] == '#' ) { if( mg[ ( point.c + 1 ) % 2 ][ point.n ][ point.m ] == '#' || mg[ ( point.c + 1 ) % 2 ][ point.n ][ point.m ] == '*' ) { return; } search( mg, new Point( ( point.c + 1 ) % 2, point.n, point.m ), t ); } else { mg[ point.c ][ point.n ][ point.m ] = '*'; search( mg, new Point( point.c, point.n, point.m + 1 ), t - 1 ); search( mg, new Point( point.c, point.n, point.m - 1 ), t - 1 ); search( mg, new Point( point.c, point.n + 1, point.m ), t - 1 ); search( mg, new Point( point.c, point.n - 1, point.m ), t - 1 ); mg[ point.c ][ point.n ][ point.m ] = '.'; } } private static boolean constraint( Point point, char[][][] mg, int t ) { if( point.n < 0 || point.n >= mg[ 0 ].length || point.m < 0 || point.m >= mg[ 0 ][ 0 ].length ) { return false; } if( t < 0 ){ return false; } return true; }}class Point { public int c; public int n; public int m; public Point( int c, int n, int m ) { this.c = c; this.n = n; this.m = m; }}
Orz~~~~~~~