蓝桥杯31天冲刺打卡题解(Day20)

Day20

第一题

第七届2016年蓝桥杯国赛

七星填数字

JavaC组第2题

填空题

dfs+模拟。

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13};
        dfs(arr,0);
    }

    private static void dfs(int[] arr, int u) {
        if (u == arr.length) {
            int sum1  =  arr[0]  +  arr[1]  +  arr[2] + arr[3];
            int sum2 = arr[2] + arr[4] + 6 + 11;
            int sum3 = arr[3] + arr[4] + arr[5] + arr[6];
            int sum4 = 6 + arr[1] + arr[9] + 14;
            int sum5 = arr[0] + arr[9] + arr[8] + arr[10];
            int sum6 = arr[10] + arr[7] + arr[5] + 11;
            int sum7 = arr[6] + arr[7] + arr[8] + 14;
            if (sum1 == sum2 && sum1 == sum3 && sum1 == sum4 
             && sum1 == sum5 && sum1 == sum6 && sum1 == sum7) {
                System.out.println(arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3]);
            }
        }
        for (int i = u; i < arr.length; i++) {
            int temp = arr[i];
            arr[i] = arr[u];
            arr[u] = temp;
            dfs(arr, u + 1);
            temp = arr[i];
            arr[i] = arr[u];
            arr[u] = temp; // 回溯
        }
    }
}

超时了,我们直接输出结果。

public class Main {
    public static void main(String[] args) {
        System.out.println("10 3 9 8");
    }
}

第二题

第十届2019年蓝桥杯省赛

旋转

JavaC组第6题

矩阵的旋转。

image-20220327213725233

import java.util.Scanner;

public class Main {
	
	static final int N = 110, M = 110;
	static int[][] a = new int[N][M];
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        for (int i = 0; i < n; i++) {
        	for (int j = 0; j < m; j++) {
        		a[i][j] = sc.nextInt();
        	}
        }
        
        for (int i = 0; i < m; i++) {
        	for (int j = n - 1; j >= 0; j--) {
        		System.out.print(a[j][i] + " ");
        	}
        	System.out.println();
        }
    }
}

第三题

第九届2018年蓝桥杯国赛

迷宫与陷阱

C++C组第6题

一道很复杂的bfs。

不过这个bfs和一般的bfs就差在了它无敌的时候,有时候重复走某些格子可能会形成更短的路径,因此这道题只需要把原本二维标记数组st[N][N]变成三维的st[N][N][11]就可以了,st[x][y][z]用以记录在坐标(x, y)时,无敌步数还剩z步的情况。

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

public class Main {

    static final int N = 1010;
    static int[][][] st = new int[N][N][11];
    static char[][] g = new char[N][N];
    static int n, k, ans;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            n = sc.nextInt();
            k = sc.nextInt();

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    Arrays.fill(st[i][j], 0);
                }
            }

            for (int i = 0; i < n; i++) g[i] = sc.next().toCharArray();

            bfs();
        }
    }

    private static void bfs() {
        Queue<PII> q = new LinkedList<>();
        q.offer(new PII(0, 0, 0));
        st[0][0][0] = 1;

        int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
        while (!q.isEmpty()) {
            int m = q.size();
            int flag = 0;

            while (m-- > 0) {
                PII t = q.poll();

                int x = t.x, y = t.y, p = t.p;
                if ((x == n - 1) && (y == n - 1)) {
                    flag = 1;
                    break;
                }

                for (int i = 0; i < 4; i++) {
                    int nx = x + dx[i], ny = y + dy[i];
                    if (nx >= 0 && nx < n && ny >= 0 && ny < n && st[nx][ny][p] == 0) {
                        if (g[nx][ny] == '#') continue;
                        else if (g[nx][ny] == 'X') {
                            if (p == 0) continue;
                            else {
                                st[nx][ny][p] = 1;
                                q.offer(new PII(nx, ny, p - 1));
                            }
                        }
                        else if (g[nx][ny] == '.') {
                            st[nx][ny][p] = 1;
                            q.offer(new PII(nx, ny, p > 0 ? p - 1 : 0));
                        }
                        else if (g[nx][ny] == '%') {
                            g[nx][ny] = '.';
                            st[nx][ny][p] = 1;
                            q.offer(new PII(nx, ny, k));
                        }
                    }
                }
            }
            if (flag == 1) break;

            ans++;
        }

        System.out.println(ans);
    }

    static class PII {
        int x;
        int y;
        int p; // p用来记录剩下的无敌步数

        public PII(int x, int y, int p) {
            this.x = x;
            this.y = y;
            this.p = p;
        }
    }
}

第四题

第八届2017年蓝桥杯省赛

九宫幻方

C++C组第8题

dfs。

import java.util.Scanner;

public class Main {
	
	static final int N = 15;
	static int[] g = new int[N];
	static int[] ans = new int[N];
	static boolean[] st = new boolean[N]; // 位置
	static boolean[] used = new boolean[N]; // 数字
	static int[] t = new int[8]; // 求和
	static int cnt;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 9; i++) {
        	g[i] = sc.nextInt();
        	if (g[i] != 0) {
        		st[i] = true;
        		used[g[i]] = true;
        	}
        }
        
        dfs(1);
        
        if (cnt == 1) {
        	for (int i = 1; i <= 9; i++) {
        		System.out.print(ans[i] + " ");
        		if (i % 3 == 0) System.out.println();			
        	}
        } else System.out.println("Too Many");
    }


	private static void dfs(int u) {
		if (cnt > 1) return; // 剪枝
        
		if (u > 9) {
			check();
			return;
		}
		
		if (st[u]) {
			dfs(u + 1);
			return;
		}
		
		for (int i = 1; i <= 9; i++) {
			if (!used[i]) {
				used[i] = true;
				g[u] = i;
				dfs(u + 1);
				used[i] = false; // 回溯
			}
		}
	}

	private static void check() {
		t[0] = g[1] + g[2] + g[3]; // 行
	    t[1] = g[4] + g[5] + g[6];
	    t[2] = g[7] + g[8] + g[9];
	    t[3] = g[1] + g[4] + g[7]; // 列
	    t[4] = g[2] + g[5] + g[8];
	    t[5] = g[3] + g[6] + g[9];
	    t[6] = g[1] + g[5] + g[9]; // 对角线
	    t[7] = g[3] + g[5] + g[7];

	    for (int i = 0; i <= 7; i++) if (t[i] != 15) return;
	    cnt++;
	    for (int i = 1; i <= 9; i++) ans[i] = g[i];
	}
}

第五题

第四届2013年蓝桥杯真题

大臣的旅费

这道题我写了图论解法,题解在这篇文章的最后一题:蓝桥杯AcWing学习笔记 6-3图论的学习

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小成同学_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值