牛客2018.6模拟考编程题

emmm,今天的题目不知道怎么评价,感觉不难但是可能是太菜了,感觉时间不够and测试数据有点?emm异常。。

  • 1.牛牛玩牌

  题目如上,比较前三张的大小,模拟前者大于后者的可能数。样例没看懂。。0.3905*45*46是808.3,我模拟出来的可能数是807。(等牛客把模拟编程题再补这题吧)。

  • 2.牛牛数星星

看到m,n<=10w就把撸到一半的暴力扔一边了,思考了一下可以用一个二维数组dp[i][j]存储从(1,1)到(i,j)的矩阵内有多少数据,预处理输入dp[i][j] = dp[i-1][j]+add(add是a[i][1]到a[i][j]有多少星星数,预处理复杂度最大o(10w))

然后在a1,b1;a2,b2中的星星数为dp[x2][y2] -dp[x2][y1-1] -dp[x1-1][y2] + dp[x1-1][y1-1];

大概是这么个意思,总觉得是测试数据有问题,比如a1<a2<=1000这个条件没满足之类的。。过了50%,同上,等题再放出来再补。

  • 3.牛牛走迷宫

没想到这是最简单的一题,裸的bfs求最短路径即可。也是今晚唯一ac的一题。代码如下:

 1     public static void main(String[] args) {
 2         Scanner in = new Scanner(System.in);
 3         while (in.hasNextInt()) {// 注意while处理多个case
 4             int n = in.nextInt();
 5             int res = 0;
 6             int size = 0;
 7             char[][] maze = new char[n][n];
 8             char[] copy;
 9             int dir[][] = {{0,1},{1,0},{-1,0},{0,-1}};
10             Queue<Point> queue = new LinkedList<>();
11             Point gate = null;
12             for(int i = 0;i<n;i++){
13                 copy = in.next().toCharArray();
14                 for(int j = 0;j<n;j++){
15                     maze[i][j] = copy[j];
16                     if(maze[i][j] == '*'){
17                         gate = new Point(i,j);
18                     }
19                 }
20             }
21             //bfs搜索搜到就退出
22             queue.offer(gate);
23             size = 1;
24             int nexti,nextj;
25             boolean goal = false;
26             while(!queue.isEmpty()){
27                 Point next = queue.poll();
28                 for(int i = 0;i<dir.length;i++){
29                     nexti = next.x + dir[i][0];
30                     nextj = next.y + dir[i][1];
31                     if(nexti >=0 && nexti<n && nextj >=0&&nextj<n){
32                         if(maze[nexti][nextj] == '.'){//
33                             queue.offer(new Point(nexti, nextj));
34                             maze[nexti][nextj] = '#';//走过的不能再走,bfs保证每次走到的位置都是最短的步数
35                         }else if(maze[nexti][nextj] == '@'){//搜到就退出并且提前把res+1
36                             res++;
37                             goal = true;
38                             break;
39                         }
40                     }
41                 }
42                 if(goal){
43                     break;
44                 }
45                 if(--size == 0){//如果第res步不能走到入口,记录下一次能走到的size
46                     res++;
47                     size = queue.size();
48                 }
49             }
50             System.out.println(res);
51         }
52     }
53     static class Point{//坐标
54         int x;
55         int y;
56         public Point(int x1,int y1) {
57             x = x1;
58             y = y1;
59         }
60     }

总结的话,还是太菜,这种有时限的题带来的压力还是能让我不断提高的,还是得继续刷题补题。

猜你喜欢

转载自www.cnblogs.com/zzzdp/p/9185331.html
今日推荐