Mirrors_nga

package IM; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReflectingMirrors { static int T, n, Ans; static int[][] map; static final int LEFT = 1, RIGHT = 2, TOP = 3, DOWN = 4; public static int runRobot(int x, int y, int rotate) { int mirrors = 0; while ((x != 0 && rotate != TOP) || (x != n - 1 && rotate != DOWN) || (y != 0 && rotate != LEFT) || (y != n - 1 && rotate != RIGHT)) { if (y < n && rotate == RIGHT) { if (map[x][y] == 2 ) { mirrors++; x++; rotate = DOWN; } else if ( map[x][y] == 1) { mirrors++; x--; rotate = TOP; } else y++; } else if (y > 0 &&rotate == LEFT) { if ( map[x][y] == 2) { mirrors++; x--; rotate = TOP; } else if (map[x][y] == 1) { mirrors++; x++; rotate = DOWN; } else y--; } else if (x > 0 && rotate == TOP) { if ( map[x][y] == 2) { mirrors++; y--; rotate = LEFT; } else if (map[x][y] == 1) { mirrors++; y++; rotate = RIGHT; } else x--; } else if ( x< n- 1 && rotate == DOWN) { if ( map[x][y] == 2 ) { mirrors++; y++; rotate = RIGHT; } else if ( map[x][y] == 1 ) { mirrors++; y--; rotate = LEFT; } else x++; } if((x == 0 && rotate == TOP) || (x == n && rotate == DOWN) || (y == 0 && rotate == LEFT) || (y == n && rotate == RIGHT)) break; } return mirrors; } public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new File("C:\\Users\\Truong Duy Thai\\Desktop\\input\\Mirrors_input.txt")); T = sc.nextInt(); for (int tc = 1; tc <= T; tc++) { n = sc.nextInt(); map = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { map[i][j] = sc.nextInt(); } } Ans = runRobot(0, 0, RIGHT); System.out.println(Ans); System.out.println("#"+tc+" "+Ans); } } }
4
10
0 0 0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 1 0 0
1 0 0 0 2 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
5
0 2 0 0 0
0 0 0 0 0
0 2 0 1 0
2 0 0 1 0
0 2 0 0 0
10
0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
0 0 2 1 0 0 0 2 0 0
0 0 0 0 0 1 0 1 0 0
0 0 0 0 2 0 0 0 1 0
0 2 0 0 0 0 1 0 0 1
0 0 0 0 0 0 2 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 2 0 0 0 0 0 0 1
10
0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 2 0 1
0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 2 2 0 0 1
0 0 0 0 0 0 0 2 0 1
0 0 0 2 1 0 2 0 2 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 0
====================
#1 7
#2 3
#3 3
#4 4

2 Responses

package IM;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReflectingMirrors {
static int T, n, Ans;
static int[][] map;
static final int LEFT = 1, RIGHT = 2, TOP = 3, DOWN = 4;

public static int runRobot(int x, int y, int rotate) {
int mirrors = 0;
while ((x != 0 && rotate != TOP) || (x != n - 1 && rotate != DOWN)
|| (y != 0 && rotate != LEFT)
|| (y != n - 1 && rotate != RIGHT)) {
if (y < n && rotate == RIGHT) {
if (map[x][y] == 2 ) {
mirrors++;
x++;
rotate = DOWN;
}
else if ( map[x][y] == 1) {
mirrors++;
x--;
rotate = TOP;
}
else
y++;

} else if (y > 0 &&rotate == LEFT) {

if ( map[x][y] == 2) {
mirrors++;
x--;
rotate = TOP;
}
else if (map[x][y] == 1) {
mirrors++;
x++;
rotate = DOWN;
}
else
y--;

} else if (x > 0 && rotate == TOP) {

if ( map[x][y] == 2) {
mirrors++;
y--;
rotate = LEFT;
}
else if (map[x][y] == 1) {
mirrors++;
y++;
rotate = RIGHT;
}
else
x--;
} else if ( x< n- 1 && rotate == DOWN) {

if ( map[x][y] == 2 ) {
mirrors++;
y++;
rotate = RIGHT;
}
else if ( map[x][y] == 1 ) {
mirrors++;
y--;
rotate = LEFT;
}
else
x++;
}
if((x == 0 && rotate == TOP) || (x == n && rotate == DOWN)
|| (y == 0 && rotate == LEFT)
|| (y == n && rotate == RIGHT))
break;
}
return mirrors;
}

public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("C:\\Users\\Truong Duy Thai\\Desktop\\input\\Mirrors_input.txt"));
T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
n = sc.nextInt();
map = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = sc.nextInt();
}
}
Ans = runRobot(0, 0, RIGHT);
System.out.println(Ans);
System.out.println("#"+tc+" "+Ans);
}
}

}

Write a 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.