0920度小满笔试,记录自己的脑抽

两道编程,一道ac,一道0,
第一道
import java.util.Scanner;

public class D1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();    //需要的颜料
        StringBuffer str = new StringBuffer(str1);  //有的颜料
        int count = 0;
        for (int i = 0;i<str2.length();i++){
             int tag = str.indexOf(Character.toString(str2.charAt(i)));
             if(tag>=0){
                 count++;
                 str.delete(tag,tag+1);
            }
        }
        System.out.println(count);
    }
}
第二道,脑抽了,居然把条件判断里的一个或写成了与,笔试时找了10多分钟没找到
出来在代码里插了很多sout才发现,希望自己以后不会犯这种低级错误了
import java.util.PriorityQueue;
import java.util.Scanner;

public class D2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String st = sc.nextLine();
       int t = Integer.parseInt(st);
        for (int i = 0; i < t; i++) {
            String str = sc.nextLine();
            String[] strs = str.split(" ");
            int n =Integer.parseInt(strs[0]);    //行数
            int m =Integer.parseInt(strs[1]);    //列数

            char[][] board = new char[n][m];
            int x = -1;
            int y = -1;
            for (int j = 0; j < n; j++) {
                String s = sc.nextLine();
                for (int k = 0; k < m; k++) {
                    board[j][k]=s.charAt(k);
                    if (board[j][k] == '@') {
                        x = k;    //列号
                        y = j;    //行号
                    }
                }
            }
            boolean[][] used = new boolean[n][m];
            int[][] move = new int[4][2];
            move[0][0]=0;
            move[0][1]=1;
            move[1][0]=0;
            move[1][1]=-1;
            move[2][0]=1;
            move[2][1]=0;
            move[3][0]=-1;
            move[3][1]=0;

            PriorityQueue<Integer> list = new PriorityQueue<>();
            //System.out.println(t +" "+n+ " "+m+" "+x+" "+y );
            dis(x,y,board,used,list,0,move);
            if(list.size()>0){
                System.out.println(list.poll());
            }else{
                System.out.println("-1");
            }


        }
    }

    public static void dis(int x,int y,char[][] board,boolean[][] used, PriorityQueue<Integer> list,int count,int[][] move){
        if(x<=0||x>=board[0].length-1||y<=0||y>=board.length-1){     //第3个或写成了与
            //System.out.println(count);
            list.add(count);
            return;
        }
        for (int i = 0; i < 4; i++) {
            x=x+move[i][0];
            y=y+move[i][1];
            if(x>=0&&x<board[0].length&&y>=0&&y<board.length&&!used[y][x]){
                used[y][x] = true;
                if (board[y][x]=='*'){
                    dis(x,y,board,used,list,count+1,move);
                    // System.out.println(count);
                }else if (board[y][x]=='.'){
                    dis(x,y,board,used,list,count,move);
                }

                used[y][x] = false;
            }
            x-=move[i][0];
            y-=move[i][1];
        }


    }
}



#笔试题目##度小满#
全部评论
dfs找所有出路 然后选择花费能量最少的输出?
点赞 回复
分享
发布于 2020-09-21 08:53

相关推荐

点赞 6 评论
分享
牛客网
牛客企业服务