网易笔试
static Road road;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for (int i = 0; i < N; i++) {
int L = in.nextInt();
int W = in.nextInt();
char[][] arr = new char[W][L];
int start1 = 0, start2 = 0;
char[] c;
String s;
for (int j = 0; j < W; j++) {
s = in.next();
c = s.toCharArray();
for (int k = 0; k < L; k++) {
arr[j][k] = c[k];
if (arr[j][k] == 'S') {
start1 = j;
start2 = k;
}
}
}
System.out.println(result(arr, start1 ,start2));
}
}
private static int result(char[][] arr, int start1, int start2) {
int i = start1;
int j = start2;
int ans = 0;
if (i == 0) {
road = Road.DOWN ;
} else if (j == 0) {
road = Road.RIGHT ;
} else if (i == arr.length - 1) {
road = Road.UP ;
} else {
road = Road.LEFT ;
}
int[] ij;
while (arr[i][j] != 'E') {
ans++;
if (!wallLeft(i, j, arr)) {
ij = trunLeft(i, j);
} else if (!wallFront (i, j, arr)) {
ij = forward(i, j);
} else if (!wallRight (i, j, arr)) {
ij = trunRight(i, j);
} else {
ij = back(i, j);
}
i = ij[0];
j = ij[1];
}
return ++ans;
}
private static int[] back(int i, int j) {
int[] ij = new int[2];
if (road == Road.UP) {
road = Road.DOWN ;
i++;
} else if (road == Road. DOWN) {
road = Road.UP ;
i--;
} else if (road == Road. LEFT) {
road = Road.RIGHT ;
j++;
} else {
road = Road.LEFT ;
j--;
}
ij[0] = i;
ij[1] = j;
return ij;
}
private static int[] trunRight(int i, int j) {
int[] ij = new int[2];
if (road == Road.UP) {
road = Road.RIGHT ;
j++;
} else if (road == Road. DOWN) {
road = Road.LEFT ;
j--;
} else if (road == Road. LEFT) {
road = Road.UP ;
i--;
} else {
road = Road.DOWN ;
i++;
}
ij[0] = i;
ij[1] = j;
return ij;
}
private static int[] forward(int i, int j) {
int[] ij = new int[2];
if (road == Road.UP) {
i--;
} else if (road == Road. DOWN) {
i++;
} else if (road == Road. LEFT) {
j--;
} else {
j++;
}
ij[0] = i;
ij[1] = j;
return ij;
}
private static int[] trunLeft(int i, int j) {
int[] ij = new int[2];
if (road == Road.UP) {
road = Road.LEFT ;
j--;
} else if (road == Road. DOWN) {
road = Road.RIGHT ;
j++;
} else if (road == Road. LEFT) {
road = Road.DOWN ;
i++;
} else {
road = Road.UP ;
i--;
}
ij[0] = i;
ij[1] = j;
return ij;
}
private static boolean wallRight(int i, int j, char[][] arr) {
if (road == Road.UP) {
return arr[i][j + 1] == '#';
} else if (road == Road. DOWN) {
return arr[i][j - 1] == '#';
} else if (road == Road. RIGHT) {
return arr[i + 1][j] == '#';
} else
return arr[i - 1][j] == '#';
}
private static boolean wallFront(int i, int j, char[][] arr) {
if (road == Road.UP) {
return arr[i - 1][j] == '#';
} else if (road == Road. DOWN) {
return arr[i + 1][j] == '#';
} else if (road == Road. RIGHT) {
return arr[i][j + 1] == '#';
} else
return arr[i][j - 1] == '#';
}
private static boolean wallLeft(int i, int j, char[][] arr) {
if (road == Road.UP) {
return arr[i][j - 1] == '#';
} else if (road == Road. DOWN) {
return arr[i][j + 1] == '#';
} else if (road == Road. RIGHT) {
return arr[i - 1][j] == '#';
} else
return arr[i + 1][j] == '#';
}
}
enum Road {
UP, DOWN ,LEFT, RIGHT;
}