网易笔试后端9.27

蛇形矩阵那题,本地测试能过,提交0.00%😭麻烦哪位大佬帮我看看代码错在哪了,谢谢!!!调试了半个小时不知道问题在哪,唉......
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author: zhangyc
 * @date: 2020/9/27
 */
public class Main_netease_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int C = sc.nextInt();
        for (int i = 0; i < C; i++) {
            List<Integer> ret = new ArrayList<>();
            int N = sc.nextInt();
            int[][] maze = buildMaze(N);
            int M = sc.nextInt();
            for (int j = 0; j < M; j++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                ret.add(maze[x][y]);
            }
            for (Integer res : ret) {
                System.out.println(res);
            }
        }

    }

    public static int[][] buildMaze(int N) {
        int[][] storage = new int[N][N];
        int direction = 0;
        int number = 1;
        int curX = 0, curY = -1;
        while (number <= N * N) {
            switch (direction) {
                case 0:
                    curY++;
                    while (curY < N && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curY++;
                        number++;
                    }
                    curY--;
                    direction = 1;
                    break;
                case 1:
                    curX++;
                    while (curX < N && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curX++;
                        number++;
                    }
                    curX--;
                    direction = 2;
                    break;
                case 2:
                    curY--;
                    while (curY >= 0 && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curY--;
                        number++;
                    }
                    curY++;
                    direction = 3;
                    break;
                case 3:
                    curX--;
                    while (curX >= 0 && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curX--;
                        number++;
                    }
                    curX++;
                    direction = 4;
                    break;
                case 4:
                    curX++;
                    while (curX < N && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curX++;
                        number++;
                    }
                    curX--;
                    direction = 5;
                    break;
                case 5:
                    curY++;
                    while (curY < N && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curY++;
                        number++;
                    }
                    curY--;
                    direction = 6;
                    break;
                case 6:
                    curX--;
                    while (curX >= 0 && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curX--;
                        number++;
                    }
                    curX++;
                    direction = 7;
                    break;
                case 7:
                    curY--;
                    while (curY >= 0 && storage[curX][curY] == 0) {
                        storage[curX][curY] = number;
                        curY--;
                        number++;
                    }
                    curY++;
                    direction = 0;
                    break;
                default:
                                        break;
            }    
        }
        return storage;
    }
}



#网易互娱##笔试题型#
全部评论
第一题Java的AC代码
点赞 回复 分享
发布于 2020-09-27 21:51

相关推荐

ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务