算法进阶指南-基础算法-总结与练习(1~4)

1.The Pilots Brothers’ refrigerator

题目链接
在这里插入图片描述

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

public  class Main
{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         int state=0;
         String yjl;
         int [][]change=new int[4][4];
         for(int i=0;i<4;i++){
             yjl=sc.nextLine();

             for(int j=0;j<4;j++)
             {
                 if(yjl.charAt(j)=='+')
                 {
                     state+=1<<(i*4+j);
                 }
             }

         }


         for(int i=0;i<4;i++)
             for(int j=0;j<4;j++)
             {
                 for(int k=0;k<4;k++)
                 {
                     change[i][j]+=1<<(i*4+k);
                     change[i][j]+=1<<(k*4+j);
                 }
                change[i][j]-=1<<(i*4+j);
        
            }
        ArrayList<path> res=new ArrayList<path>();
        for(int i=0;i<1<<16;i++)
        {
            ArrayList<path> v=new ArrayList<path>();
            int now=state;
            for(int j=0;j<16;j++)
            {
                if((i&(1<<j))>0)
                {
                    int x=j/4,y=j%4;
                    now^=change[x][y];
                    path ww=new path();
                    ww.x=x;
                    ww.y=y;
                    v.add(ww);
                }
            }
            if(now==0&&(res.size()==0||res.size()<v.size()))
                res=v;
        }
        System.out.println(res.size());
        for(int i=0;i<res.size();i++)
            System.out.println(res.get(i).x+1+" "+(res.get(i).y+1));

    }
}

class path{
    int x,y;
}

2.占卜DIY

题目链接
在这里插入图片描述

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

public  class Main
{
    static char maps[][]=new char[1050][1050];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer>x[]=new ArrayList[14];
        String k;
        for(int i=1;i<=13;i++){
            x[i]=new ArrayList<>();
            k=sc.nextLine();
            String p[]=k.trim().split(" ");
            for(int j=0;j<4;j++)
            {

                x[i].add(get(p[j].charAt(0)));
            }
        }
        int open[]=new int[14];
        Arrays.fill(open,0);
        for(int i=0;i<4;i++)
        {
            int t=x[13].get(i);
            while(t!=13)
            {
                open[t]++;
                int r=x[t].get(x[t].size()-1);
                x[t].remove(x[t].size()-1);
                t=r;
            }

        }
        int res=0;
        for(int i=1;i<=12;i++)
            if(open[i]==4) res++;


        System.out.println(res);
    }
    public static int get(char v){
        if(v=='0') return 10;
        if(v>'1'&&v<='9') return v-'0';
        if(v=='J') return 11;
        if(v=='A' ) return 1;
        if(v=='Q') return 12;
        return 13;

    }



}


3.Fractal

题目链接
在这里插入图片描述

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

public  class Main
{
    static char maps[][]=new char[1050][1050];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


         dfs(7);
        int n;
        while(true){
            n=sc.nextInt();
            if(n==-1)
                break;
            int len=1;
            for(int i=1;i<=n-1;i++) len*=3;

            for(int i=0;i<len;i++) {


                for (int j = 0; j <= len; j++) {
                    if (maps[i][j] == 'X') System.out.printf("X");
                    else System.out.printf(" ");
                }
                System.out.println();
            }

            System.out.println('-');
        }

    }

    public static void dfs(int x)
    {
        if(x==1)
        {
            maps[0][0]='X';
            return ;
        }
        dfs(x-1);

        int len=1;
        for(int i=1;i<x-1;i++) len*=3;

      int way[][]= new int[][]{{2, 0}, {1, 1}, {0, 2}, {2, 2}};
      for(int k=0;k<4;k++)
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++) {
                maps[way[k][0]*len+i][way[k][1]*len+j]=maps[i][j];
            }
    }


}


4.Raid

题目链接
在这里插入图片描述

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i=0;i<t;i++) {
            int n = sc.nextInt();
            Point[] points = new Point[n*2];
            Point[] temp = new Point[n*2];
            for (int j=0;j<n;j++){
                points[j] = new Point(sc.nextInt(), sc.nextInt());
                points[j].type=false;
            }
            for (int j=n;j<n<<1;j++){
                points[j]=new Point(sc.nextInt(), sc.nextInt());
                points[j].type=true;
            }
            Arrays.sort(points, Comparator.comparingInt(a -> a.x));
            System.out.printf("%.3f\n", dfs(points, 0, (n<<1)-1, temp));
        }
    }
    private static double dfs(Point[] points, int l, int r, Point[] temp) {
        if (l==r) {
            return Integer.MAX_VALUE;
        }
        int mid = l + r >> 1;
        int midX = points[mid].x;
        double ans = Math.min(dfs(points, l, mid, temp), dfs(points, mid+1, r, temp));
        int i=l,j=mid+1,cnt=0;
        while (i<=mid && j<=r) {
            if (points[i].y<points[j].y) {
                temp[cnt++]=points[i++];
            } else {
                temp[cnt++]=points[j++];
            }
        }
        while (i<=mid) {
            temp[cnt++]=points[i++];
        }
        while (j<=r) {
            temp[cnt++]=points[j++];
        }
        for (i=l;i<=r;i++) {
            points[i]=temp[i-l];
        }
        cnt=0;
        for (i=l;i<=r;i++) {
            if (points[i].x>=midX-ans && points[i].x<=midX+ans) {
                temp[cnt++]=points[i];
            }
        }
        for (i=0;i<cnt;i++) {
            for (j=i-1;j>=0&&temp[i].y-temp[j].y<=ans;j--) {
                ans = Math.min(ans, getDist(temp[i], temp[j]));
            }
        }
        return ans;
    }
    private static double getDist(Point i, Point j) {
        if (i.type == j.type) {
            return Integer.MAX_VALUE;
        }
        double dx=i.x-j.x, dy=i.y-j.y;
        return Math.sqrt(dx*dx+dy*dy);
    }
}
class Point {
    int x;
    int y;
    boolean type;
    Point(int x, int y){
        this.x=x;
        this.y=y;
    }
}



  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值