网易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);
}

}

#网易#
全部评论
//首先接受。用char[][]数组。 //找到S的位置,找到他左手边的墙的位置,然后走下一步。 //当它的方向朝右边的时候,上面就是它左手边的墙。碰壁以后方向顺时针旋转90度。如果左手边没有墙,那么往左走。否则看前面有墙吗。 //没有的话,往前走,有的话往下走,下面如果有墙,就往左走。 //第一步:判断该点左手边是否有墙。如果没墙,往左走。如果有墙,调转方向90度,如果没墙,走,如果右墙,调转90度。 package src; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); int a =0; while (a<num) {//注意while处理多个case a++; int sum=0; int m = in.nextInt();//长 int n = in.nextInt();//高 char[][] chars = new char[n][m]; for(int i =0;i<n;i++){ String s= in.next(); for(int j=0;j<m;j++){ chars[i][j] = s.charAt(j); } } //开始执行循环。 //找到入口。 int entrenceX = 0; int entrenceY = 0; int exitX = 0; int exitY = 0; for(int i =0;i<n;i++){ for(int j=0;j<m;j++){ if(chars[i][j]=='S'){entrenceX = j;entrenceY=i;} if(chars[i][j]=='E'){exitX = j;exitY = i;} } } int curX = entrenceX; int curY = entrenceY; String direction = null; if(curX==0)direction="right"; if(curX==m-1)direction="left"; if(curY==0)direction="down"; if(curY==n-1)direction="up"; while(!(curX==exitX&&curY==exitY)){ if(direction.equals("right")){ if(chars[curY-1][curX]=='#'){//左手边有墙 direction="down"; if(chars[curY][curX+1]=='#'){//前面有墙 direction="left"; if(chars[curY+1][curX]=='#'){//下面有墙。 direction="up"; curX--; sum++; direction = "left"; }else{ curY++; sum++; direction = "down"; } }else{ curX++; sum++; direction = "right"; } }else{ curY--; sum++; direction = "up"; } }else if(direction.equals("down")){ if(chars[curY][curX+1]=='#'){//左手边有墙 direction="left"; if(chars[curY+1][curX]=='#'){//前面有墙 direction="up"; if(chars[curY][curX-1]=='#'){//下面有墙。 direction="right"; curY--; sum++; direction = "up"; }else{ curX--; sum++; direction = "left"; } }else{ curY++; sum++; direction = "down"; } }else{ curX++; sum++; direction = "right"; } }else if(direction.equals("left")){ if(chars[curY+1][curX]=='#'){//左手边有墙 direction="up"; if(chars[curY][curX-1]=='#'){//前面有墙 direction="right"; if(chars[curY-1][curX]=='#'){//下面有墙。 direction="down"; curX++; sum++; direction = "right"; }else{ curY--; sum++; direction = "up"; } }else{ curX--; sum++; direction = "left"; } }else{ curY++; sum++; direction = "down"; } }else if(direction.equals("up")){ if(chars[curY][curX-1]=='#'){//左手边有墙 direction="right"; if(chars[curY-1][curX]=='#'){//前面有墙 direction="down"; if(chars[curY][curX+1]=='#'){//下面有墙。 direction="left"; curY++; sum++; direction = "down"; }else{ curX++; sum++; direction = "right"; } }else{ curY--; sum++; direction = "up"; } }else{ curX--; direction = "left"; sum++; } } } System.out.println(sum+1); } } }
点赞 回复 分享
发布于 2018-09-16 22:09

相关推荐

10-27 17:26
东北大学 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务