Magnet

package IM; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Magnet { static int T, N, M, D, x, y, Ans; static int[][] map; static int[] arrDraw; static final int UP = 1, RIGHT = 2, DOWN = 3, LEFT = 4; public static int checkRotate(int d){ if(arrDraw[d] == 1){ return UP; }else if(arrDraw[d] == 2){ return RIGHT; }else if(arrDraw[d] == 3){ return DOWN; }else return LEFT; } public static int magnet(int x, int y, int rotate){ int moves = 0; for(int k = 1; x+k < N; k++){ if(rotate == DOWN && map[x+k][y] == 0){ moves++; }else break; } for(int k = 1; x-k>=0; k++){ if(rotate == UP && map[x-k][y] == 0){ moves++; }else break; } return moves; } public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File("input_magnet.txt")); T = sc.nextInt(); for(int tc = 1; tc <= 1; tc++){ N = sc.nextInt(); M = sc.nextInt(); D = sc.nextInt(); map = new int[N][M]; arrDraw = new int[D]; Ans = 0; // Doc data vao mang Draw for(int i = 0; i < D; i++){ arrDraw[i] = sc.nextInt(); } // Doc data vao mang MN for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ map[i][j] = sc.nextInt(); System.out.print(map[i][j]+" "); if(map[i][j] == 2){ x = i; y = j; } } System.out.println(); } // Xu ly for(int d = 0; d < D; d++){ Ans += magnet(x,y,checkRotate(arrDraw[d])); } //In ket qua System.out.println("Case#"+tc+" "+Ans); } } }
50
4 5 6
3 4 2 3 2 1
2 0 0 0 0
0 1 0 0 1
0 1 0 0 1
0 0 0 0 0
6 7 8
4 1 4 2 1 2 4 3
0 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 2 0 0 1
1 0 0 0 0 0 1
0 1 0 0 1 0 0
0 0 0 0 0 0 0
8 9 6
1 2 4 3 2 4
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 2
0 0 1 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0
1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0
9 10 8
1 2 1 4 1 4 2 1
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 1 0
0 0 1 0 0 0 0 0 0 1
0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
==================
Case#1 7
Case#2 19
Case#3 16
Case#4 11
Case#5 45
Case#6 49
Case#7 28
Case#8 32
Case#9 47
Case#10 135
Case#11 143
Case#12 133
Case#13 179
Case#14 89
Case#15 135
Case#16 145
Case#17 211
Case#18 182
Case#19 161
Case#20 171
Case#21 214
Case#22 103
Case#23 83

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.