网易09-16菜鸡做法!求指点!
public class Laji {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int w = scanner.nextInt();
int h = scanner.nextInt();
scanner.nextLine();
String[][] map = new String[h][];
for (int j = 0; j < h; j++) {
map[j] = scanner.nextLine().split("");
}
getStep(map, w , h);
}
}
private static void getStep(String[][] map, int w ,int h) {
int ei = 0, ej = 0, si = 0, sj = 0;
for (int j = 0; j < w; j++) {
if (map[0][j].equals("E")){
ei = 0;
ej = j;
}
if (map[0][j].equals("S")){
si = 0;
sj = j;
}
}
for (int j = 0; j < w; j++) {
if (map[h-1][j].equals("E")){
ei = h-1;
ej = j;
}
if (map[h-1][j].equals("S")){
si = h-1;
sj = j;
}
}
for (int i = 0; i < h; i++) {
if (map[i][0].equals("E")){
ei = i;
ej = 0;
}
if (map[i][0].equals("S")){
si = i;
sj = 0;
}
}
for (int i = 0; i < h; i++) {
if (map[i][w-1].equals("E")){
ei = i;
ej = w-1;
}
if (map[i][w-1].equals("S")){
si = i;
sj = w-1;
}
}
for (int i = 0; i < map.length; i+=(map.length - 1)) {
for (int j = 0; j < map[0].length; j++) {
}
}
int count = 2;
int fangXiangI = 0;
int fangXiangJ = 0;
if (si == 0){
fangXiangI = 1;
}
if (si == h-1){
fangXiangI = -1;
}
if (sj == 0){
fangXiangJ = 1;
}
if (sj == w-1){
fangXiangJ = -1;
}
while (si != ei || sj != ej) {
if (fangXiangI != 0) {
switch (map[si][sj + fangXiangI]) {
case "E" : sj += fangXiangI;break;
case ".":
sj += fangXiangI;
count++;
fangXiangJ = fangXiangI;
fangXiangI = 0;
break;
case "#":
switch (map[si + fangXiangI][sj]) {
case "E" : si+=fangXiangI; break;
case ".":
si += fangXiangI;
count++;
break;
case "#":
switch (map[si][sj - fangXiangI]) {
case "E" : sj -= fangXiangI;break;
case ".":
sj -= fangXiangI;
count++;
fangXiangJ = -fangXiangI;
fangXiangI = 0;
break;
case "#":
si -= fangXiangI;
count++;
fangXiangI = -fangXiangI;
break;
}
}
}
}
if (fangXiangJ != 0) {
switch (map[si-fangXiangJ][sj]) {
case "E" : si-= fangXiangJ;break;
case ".":
si -= fangXiangJ;
count++;
fangXiangI = -fangXiangJ;
fangXiangJ = 0;
break;
case "#":
switch (map[si][sj + fangXiangJ]) {
case "E" : sj += fangXiangJ;break;
case ".":
sj += fangXiangJ;
count++;
break;
case "#":
switch (map[si + fangXiangJ][sj]) {
case "E" : si += fangXiangJ;break;
case ".":
si += fangXiangJ;
count++;
fangXiangI = fangXiangJ;
fangXiangJ = 0;
break;
case "#":
sj -= fangXiangJ;
count++;
fangXiangJ = -fangXiangJ;
break;
}
}
}
}
}
System.out.println(count);
}
}
#网易#