【思特奇杯·云上蓝桥-算法集训营】第2周

第一题

 

 

import java.util.Scanner;

public class mixed{
    public static int[] num={1,2,3,4,5,6,7,8,9};
    static int temp;
    static int t=0;
    static int x=1;//第一个数的位数
    static int input;//输入值

    public static void swap(int a,int b){//交换
        temp=num[a];
        num[a]=num[b];
        num[b]=temp;
    }

    public static void ergodic(int n){//遍历
        if(n>8){
            judge();
            return;
        }

        for(int i=n;i< num.length;i++){
            swap(i,n);
            ergodic(n+1);
            swap(i,n);
        }
    }

    public static void judge(){//判断
        int n1=0;
        int n2=0;
        int n3=0;
        for(int i=0;i<=x-1;i++){
            n1=sum(0,i);
            if(n1>=input)
                continue;
            for(int j=i+(9-i-1)/2;j<= num.length-2;j++){
                n2=sum(i+1,j);
                n3=sum(j+1,num.length-1);
                if(n2>n3 && n2%n3==0 && input==n1+n2/n3) {
                    t++;
                }
            }
        }
    }

    static int sum(int start,int end){
        int tsum=0;
        for(int i=start;i<=end;i++)
            tsum=tsum*10+num[i];
        return tsum;
    }

    public static void main(String[] args){
        //TODO Auto-generated method stub
        Scanner scanner=new Scanner(System.in);
        input=scanner.nextInt();
        temp=input;
        while((temp=temp/10)!=0) x++;
        ergodic(0);
        System.out.println(t);
    }
}

第二题

 

public class LiBai{
    public static void main(String[] args){
        System.out.println(t(2,5,9,15));
    }

    public static int t(int a,int b,int c,int d){
        if(a==0 || d==0){
            return 0;
        }
        if(a==1 && b==0 && c==0 && d==1){
            return 1;
        }

        return t(a*2,b-1,c,d-1)+t(a-1,b,c-1,d-1);
    }
}

第三题

 

public class step{

    public static int c=0;

    public static void a(int n,int s){
        if(n<0) return;
        if(n==0){
            if(s%2==0){
                c++;
            }
        }
        a(n-1,s+1);
        a(n-2,s+1);
    }
    public static void main(String[] args){
        a(39,0);
        System.out.println(c);
    }
}

第四题

 

 

 

 

import java.util.Scanner;

public class minefield{
    static int min;
    static int tl;
    static boolean b[][] = new boolean[101][101];
    static int m[][]= new int[101][101];
    static int n,xB,yB;
    static int d[][]=new int[][]{{-1,0},{0,1},{1,0},{0,-1}};

    public static void main(String[] args) {
        Scanner scan =new Scanner(System.in);
        n=scan.nextInt();
        scan.nextLine();
        String str;
        int xA=0,yA=0;
        for(int i=0;i<n;i++) {
            str = scan.nextLine();
            String[] s= str.split(" ");
            for(int j=0;j<n;j++) {
                switch(s[j].charAt(0)) {
                    case '+' : m[i][j] = 0;break;
                    case '-' : m[i][j] = 1;break;
                    case 'A' : m[i][j] = 2;xA=i;yA=j;break;
                    case 'B' : m[i][j] = 3;xB=i;yB=j;break;
                    default :break;
                }
            }
        }
        for(int i=0;i<n;i++) {for(int j=0;j<n;j++) {
            b[i][j] = false;
        }
        }
        min = 1<<30;
        tl= 0;
        a(xA,yA);
        if(min< 1<<30) {
            System.out.println(min);
        }
        else {
            System.out.println("-1");
        }
    }

    private static void a(int x,int y) {
        int tx,ty;
        if(x==xB&&y==yB) {
            if(tl<min) {
                min=tl;
            }
            return;
        }
        if(x<0 || y<0 || x>n-1 || y>n-1) {
            return;
        }
        if(tl == min) {return;}
        for(int i=0;i<4;i++) {
            tx=x+d[i][0];
            ty=y+d[i][1];
            if(tx<0||tx>n-1||ty<0||ty>n-1){
                continue;
            }
            if(m[tx][ty]!=m[x][y] && !b[tx][ty]) {
                b[tx][ty]=true;
                tl++;
                a(tx,ty);
                tl--;
                b[tx][ty]=false;}
        }
    }
}

 第五题

 

 

 

 答案:

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class maze{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            String s = "01010101001011001001010110010110100100001000101010"
                    + "00001000100000101010010000100000001001100110100101"
                    + "01111011010010001000001101001011100011000000010000"
                    + "01000000001010100011010000101000001010101011001011"
                    + "00011111000000101000010010100010100000101100000000"
                    + "11001000110101000010101100011010011010101011110111"
                    + "00011011010101001001001010000001000101001110000000"
                    + "10100000101000100110101010111110011000010000111010"
                    + "00111000001010100001100010000001000101001100001001"
                    + "11000110100001110010001001010101010101010001101000"
                    + "00010000100100000101001010101110100010101010000101"
                    + "11100100101001001000010000010101010100100100010100"
                    + "00000010000000101011001111010001100000101010100011"
                    + "10101010011100001000011000010110011110110100001000"
                    + "10101010100001101010100101000010100000111011101001"
                    + "10000000101100010000101100101101001011100000000100"
                    + "10101001000000010100100001000100000100011110101001"
                    + "00101001010101101001010100011010101101110000110101"
                    + "11001010000100001100000010100101000001000111000010"
                    + "00001000110000110101101000000100101001001000011101"
                    + "10100101000101000000001110110010110101101010100001"
                    + "00101000010000110101010000100010001001000100010101"
                    + "10100001000110010001000010101001010101011111010010"
                    + "00000100101000000110010100101001000001000000000010"
                    + "11010000001001110111001001000011101001011011101000"
                    + "00000110100010001000100000001000011101000000110011"
                    + "10101000101000100010001111100010101001010000001000"
                    + "10000010100101001010110000000100101010001011101000"
                    + "00111100001000010000000110111000000001000000001011"
                    + "10000001100111010111010001000110111010101101111000";
            int[][] labyrinth = new int[30][50];
            for (int i = 0; i < 30; i++) {
                for (int j = 0; j < 50; j++) {
                    labyrinth[i][j] = s.charAt(50 * i + j) - '0';
                }
            }
            System.out.println(BFS(labyrinth, 30, 50));
        } catch (Exception e) {
            input.close();
        }
    }

    public static String BFS(int[][] labyrinth, int row, int column) {
        int[][] stepArr = { { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 0 } };
        String[] direction = { "U", "R", "L","D"};
        int[][] visit = new int[row][column];// 标记是否已经访问过
        StringBuilder sb = new StringBuilder();
        Node node = new Node(0, 0, -1, -1, 0, null);
        Queue<Node> queue = new LinkedList<Node>();
        Stack<Node> stack = new Stack<Node>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            Node head = queue.poll();
            stack.push(head); // 用于回溯路径
            visit[head.x][head.y] = 1;
            for (int i = 0; i < 4; i++) {
                int x = head.x + stepArr[i][0];
                int y = head.y + stepArr[i][1];
                String d = direction[i];
                // exit
                if (x == row - 1 && y == column - 1 && labyrinth[x][y] == 0 && visit[x][y] == 0) {
                    // 打印路径
                    Node top = stack.pop();
                    sb.append(d);
                    sb.append(top.direction);
                    int preX = top.preX;
                    int preY = top.preY;
                    while (!stack.isEmpty()) {
                        top = stack.pop();
                        if (preX == top.x && preY == top.y) {
                            if (top.direction != null)
                                sb.append(top.direction);
                            preX = top.preX;
                            preY = top.preY;
                        }

                    }
                    return sb.reverse().toString();
                }
                // bfs
                if (x >= 0 && x < row && y >= 0 && y < column && labyrinth[x][y] == 0 && visit[x][y] == 0) {
                    Node newNode = new Node(x, y, head.x, head.y, head.step + 1, d);
                    queue.offer(newNode);
                }
            }
        }
        return null;
    }
}

class Node {
    int x, y;
    int step;
    int preX, preY;
    String direction;

    Node(int x, int y, int preX, int preY, int step, String direction) {
        this.x = x;
        this.y = y;
        this.preX = preX;
        this.preY = preY;
        this.step = step;
        this.direction = direction;
    }
}

第六题


 

 

 

import java.util.Scanner;

public class horse{
    static int n,m,a;
    static int[] dx=new int[]{1,2,2,1};
    static int[] dy = new int[]{2,1,-1,-2};

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        f(0,0);
        System.out.println(a);
    }

    public static void f(int x,int y){
        if(x==m && y==n){
            a++ ;
        }
        int i ;
        int tx=0 ;
        int ty=0 ;
        for(i=0;i<4;i++){
            tx=x+dx[i] ;
            ty=y+dy[i] ;
            if(tx>m || tx<0 || ty>n || ty<0)
                continue;
            else {
                f(tx,ty);
            }
        }
    }
}

第七题

 

 

 

import java.util.Scanner;

public class road{
    static int n;
    static int[] row,col;
    static int rowSum;
    static int colSum;
    static int[][] print;
    static int[] map;
    static int len=0;
    static int[][] vis;
    static int[][] dir={{0, 1},{0, -1},{-1, 0},{1, 0}};

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        n=in.nextInt();
        row=new int[n+1];
        col=new int[n+1];
        vis=new int[n+1][n+1];
        print=new int[n+1][n+1];
        map=new int[n*n+1];
        int index=0;
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                print[i][j]=index++;
            }
        }
        for (int i=0;i<n;i++) {
            row[i]=in.nextInt();
            rowSum+=row[i];
        }
        for (int i=0;i<n;i++) {
            col[i]=in.nextInt();
            colSum+=col[i];
        }
        len=1;
        vis[0][0]=1;
        row[0]--;
        rowSum--;
        col[0]--;
        colSum--;
        map[0]=0;
        f(0, 0);
    }

    private static void f(int x, int y){
        // TODO Auto-generated method stub
        if(x==n-1 && y==n-1){
            if(colSum==0 && rowSum==0){
                for(int i=0;i<len;i++){
                    System.out.print(map[i]+"");
                }
            }
        }
        for (int i=0;i<4;i++){
            int dx=x+dir[i][0];
            int dy=y+dir[i][1];
            if (dx>=0 && dx<n && dy>=0 && dy<n && vis[dx][dy]==0 && row[dy]>0 && col[dx]>0){
                vis[dx][dy]=1;
                row[dy]--;
                rowSum--;
                col[dx]--;
                colSum--;
                map[len++]=print[dx][dy];
                f(dx,dy);
                len--;
                vis[dx][dy]=0;
                row[dy]++;
                rowSum++;
                col[dx]++;
                colSum++;
            }
        }
    }
}

第八题

 

import java.util.Scanner;

public class trouble{
    public static int getArrange(int m,int n) {
        if(m<n)
            return 0;
        if(n==0)
            return 1;
        return getArrange(m-1,n)+getArrange(m,n-1);
    }

    public static int getArrange1(int m,int n){
        int[][] result=new int[m+1][n+1];
        for(int i=1;i<=m;i++){
            result[i][0]=1;
            for(int j=1;j<=n;j++){
                if(i>=j)
                    result[i][j]=result[i-1][j]+result[i][j-1];
            }
        }
        return result[m][n];
    }

    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int m=in.nextInt();
        int n=in.nextInt();
        System.out.println(getArrange(m,n));
    }
}

第九题

 

 

import java.util.Scanner;

public class travel{
    static int[][] map;
    static int[] isVisited;
    static int km=0;
    static int km_final=0;
    
    public static void main(String[] args){
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        map=new int[n][n];
        isVisited = new int[n];
        for(int i=0;i<n;i++) {
            isVisited[i]=0;
            for(int j=0;j<n;j++)
                map[i][j]=0;
        }
        for(int i=0;i<n-1;i++) {
            int c1=in.nextInt();
            int c2=in.nextInt();
            int dis=in.nextInt();
            map[c1-1][c2-1]=dis;
            map[c2-1][c1-1]=dis;
        }
        for(int i=0;i<n;i++) {
            isVisited[i]=1;
            d(i,n,0);
            km_final=km_final>km?km_final:km;
            km=0;
            isVisited[i]=0;
        }
        System.out.print((int)(0.5*km_final*km_final+10.5*km_final));
    }

    private static void d(int a,int n,int dis){
        if(isEnd(a,n)){
            km=km>dis?km:dis;
            return;
        }
        for(int i=0;i<n;i++) {
            if(map[a][i]!=0&&isVisited[i]==0) {
                isVisited[i]=1;
                d(i,n,dis+map[a][i]);
                isVisited[i]=0;
            }
        }
    }

    private static boolean isEnd(int a,int n) {
        for(int i=0;i<n;i++) {
            if(map[a][i]!=0&&isVisited[i]==0)
                return false;
        }
        return true;
    }
}

第十题

 

 

 

import java.util.ArrayList;
import java.util.Scanner;

public class queens{
    static int n;
    static int[] col1;
    static int[] col2;
    static ArrayList<Integer> list;
    static int count=0;
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        n=input.nextInt();
        col1=new int[n+1];
        col2=new int[n+1];
        list=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(input.nextInt()==0){
                    list.add(i+1);
                    list.add(j+1);
                }
            }
        }

        d(1);

        System.out.println(count);
    }

    public static void d(int row){
        if(row>2*n){
            count++;
            return;
        }
        if(row<=n){
            for(int i=1;i<=n;i++){
                col1[row]=i;
                if(judge1(row,i)){
                    d(row+1);
                }
            }
        }
        else{
            for(int i=1;i<=n;i++){
                int temp=row%n;
                if(row%n==0)temp=n;
                col2[temp]=i;
                if(judge2(temp,i)){
                    d(row+1);
                }
            }
        }
    }

    public static boolean judge1(int r,int c){
        for(int i=0;i<list.size();i+=2){
            if(list.get(i)==r && list.get(i+1)==c){
                return false;
            }
        }
        for(int i=1;i<r;i++){
            if(col1[i]==c || (Math.abs(i-r)==Math.abs(col1[i]-c))) return false;
        }
        return true;
    }

    public static boolean judge2(int r,int c){
        for(int i=0;i<list.size();i+=2){
            if(list.get(i)==r && list.get(i+1)==c){
                return false;
            }
        }
        for(int i=1;i<col1.length;i++){
            if(i==r && col1[i]==c)return false;
        }
        for(int i=1;i<r;i++){
            if(col2[i]==c || (Math.abs(i-r)==Math.abs(col2[i]-c))) return false;
        }
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值