ball roll

package IM; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class RollingBall { static int T, n, Ans, x, y; static int[][] map; static int demtren = 0, demduoi = 0, demtrai = 0, demphai = 0; public static int Min(int i, int j){ int min = map[i][j]; if(i+1 <n && map[i+1][j] < map[i][j]){ min = map[i+1][j]; }else if(i-1 >= 0 && map[i-1][j] < map[i][j]){ min = map[i-1][j]; }else if(j+1 < n && map[i][j+1] < map[i][j]){ min = map[i][j+1]; }else if(j-1 >=0 && map[i][j-1] < map[i][j]){ min = map[i][j-1]; } return min; } public static int findDirection(int i, int j) { int dem = 1; int min = Min(i,j); while (true) { if (i+1 < n && map[i + 1][j] == min) { dem++; i++; min = Min(i,j); } if (i -1 >= 0 && map[i - 1][j] == min) { dem++; i--; min = Min(i,j); } if (j-1 >= 0 && map[i][j - 1] == min) { dem++; j--; min = Min(i,j); } if (j+1 < n && map[i][j + 1] == min) { dem++; j++; min = Min(i,j); } if( i < 0 || i >= n || j < 0 || j >= n || map[i][j] == min){ break; } } return dem; } public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File("input_rollingball.txt")); T = sc.nextInt(); for (int tc = 1; tc <= T; tc++) { n = sc.nextInt(); map = new int[n][n]; Ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { map[i][j] = sc.nextInt(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { x =i; y = j; int t = findDirection(x,y); if( t > Ans){ Ans = t; } } } System.out.println("#"+tc+" "+Ans); } } }
3
5
19 57 74 73 94
26 27 32 98 61
40 88 49 38 25
21 66 53 95 46
80 23 58 39 89
7
40 49 56 83 84 31 11
42 95 12 16 21 19 26
98 93 29 68 10 92 82
23 13 24 58 35 25 47
17 66 39 67 70 14 87
22 34 46 94 69 96 89
62 88 50 51 61 71 86
9
90 57 65 18 25 93 64 11 54
95 19 80 37 63 44 15 14 10
89 59 46 70 38 36 21 51 97
53 47 60 88 40 48 79 56 55
83 13 27 86 45 71 75 28 84
30 20 29 35 99 98 61 94 23
85 42 43 22 16 77 31 78 34
74 26 73 92 50 72 87 49 32
68 24 91 12 17 82 69 67 81
===6
10
9

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.