Untitled
unknown
plain_text
a year ago
1.9 kB
7
Indexable
class Solution {
static char[][] map;
static int[][] visited;
static int min = 0;
static Queue<PointCs> qt;
static int[] dx = { -1, 0, 1, 0 };
static int[] dy = { 0, 1, 0, -1 };
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T;
int Answer;
T = sc.nextInt();
qt = new Queue<Solution.PointCs>();
for (int test_case = 1; test_case <= T; test_case++) {
int m = sc.nextInt();
int n = sc.nextInt();
int xs = 0, ys = 0, xe = 0, ye = 0;
map = new char[m][n];
visited = new int[m][n];
String str = "";
for (int i = 0; i < m; i++) {
str = sc.next();
for (int j = 0; j < n; j++) {
map[i][j] = str.charAt(j);
if (map[i][j] == 'Y') {
xs = i;
ys = j;
} else if (map[i][j] == 'T') {
xe = i;
ye = j;
}
}
}
bfs(xs, ys);
System.out.println("Case #" + test_case + "\n"
+ (visited[xe][ye] - 1));
map = null;
visited = null;
}
}
private static void bfs(int xs, int ys) throws Exception {
qt.reset();
qt.enQueue(new PointCs(xs, ys));
visited[xs][ys] = 1;
PointCs tmp;
while (!qt.isEmpty()) {
tmp = qt.peek();
qt.deQueue();
for (int i = 0; i < 4; i++) {
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if (isSafe(xx, yy)) {
int level = move(xx, yy);
if (visited[xx][yy] == 0
|| visited[xx][yy] > visited[tmp.x][tmp.y] + level) {
visited[xx][yy] = visited[tmp.x][tmp.y] + level;
qt.enQueue(new PointCs(xx, yy));
}
}
}
}
}
private static int move(int xx, int yy) {
if (map[xx][yy] == 'E' || map[xx][yy] == 'T') {
return 1;
}
if (map[xx][yy] == 'B') {
return 2;
}
return 0;
}
private static boolean isSafe(int xx, int yy) {
if (xx >= 0 && xx < map.length && yy >= 0 && yy < map[0].length) {
if (map[xx][yy] != 'R' && map[xx][yy] != 'S') {
return true;
}
}
return false;
}Editor is loading...
Leave a Comment