博客
关于我
蓝桥杯 2016年javaB组原题剪邮票 (bfs+暴力迭代)
阅读量:792 次
发布时间:2019-03-25

本文共 2685 字,大约阅读时间需要 8 分钟。

剪邮票问题可视化为图形中寻找五个互相连接的点的问题。该图形由12张邮票组成,呈现3行4列的结构。用户的目标是从中剪取五张连续连接的邮票,仅仅连接一个边不算连续。

问题分析

将邮票排列的二维坐标视为一个3行4列的网格点。每个点的坐标为(i, j),其中i范围[1,3],j范围[1,4]。需要从中选择五个点,使得这五个点彼此连贯(上下左右相邻),无孤立点。

方法思路

针对该问题,可以采用广度优先搜索(BFS)遍历图形。首先,确定起点和终点,然后使用BFS检查是否所有点都被访问到。

  • 图形初始化:创建一个3x4的网格,标记是否访问过。
  • 起点和终点:根据输入点位置,设定起点和终点。
  • BFS遍历:从起点出发,逐层扩展,判断终点是否被访问。
  • 解决代码

    import java.util.Arrays;import java.util.LinkedList;import java.util.Queue;public class L2016剪邮票 {    static boolean vis[][] = new boolean[3][4];    static int mp[][] = new int[3][4];    static int dx[] = { -1, 0, 1, 0 };    static int dy[] = { 0, -1, 0, 1 };    static class d {        int x;        int y;        public d(int x, int y) {            this.x = x;            this.y = y;        }    }    static Queue
    q = new LinkedList<>(); static d end; static void init() { for (int i = 0; i < 3; i++) { Arrays.fill(vis[i], false); Arrays.fill(mp[i], -1); // 障碍 } } static boolean Check(int x, int y) { if (x >= 0 && y >= 0 && x < 3 && y < 4 && mp[x][y] != -1) return true; return false; } static void bfs(int x, int y) { end = new d(x, y); q.add(end); while (!q.isEmpty()) { d now = q.poll(); for (int i = 0; i < 4; i++) { int xx = now.x + dx[i]; int yy = now.y + dy[i]; if (Check(xx, yy) && !vis[xx][yy]) { vis[xx][yy] = true; d next = new d(xx, yy); q.add(next); } } } } public static void main(String[] args) { int ans = 0; for (int i = 0; i < 12; i++) { for (int j = i + 1; j < 12; j++) { for (int k = j + 1; k < 12; k++) { for (int l = k + 1; l < 12; l++) { for (int m = l + 1; m < 12; m++) { init(); mp[i/4][i%4] = 1; mp[j/4][j%4] = 1; mp[k/4][k%4] = 1; mp[l/4][l%4] = 1; mp[m/4][m%4] = 1; vis[i/4][i%4] = true; bfs(i/4, i%4); if (vis[i/4][i%4] && vis[j/4][j%4] && vis[k/4][k%4] && vis[l/4][l%4] && vis[m/4][m%4]) { ans++; } } } } } } System.out.println(ans); }}

    答案

    经过计算,共有116种不同的剪取方法。

    感受

    这道题在于将实际问题转化为图的遍历问题,通过BFS验证连通性。暴力枚举虽然不够优化,但在小规模数据下仍然有效。

    转载地址:http://mjiuk.baihongyu.com/

    你可能感兴趣的文章
    MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
    查看>>
    MySQL - ERROR 1406
    查看>>
    mysql - 视图
    查看>>
    MySQL - 解读MySQL事务与锁机制
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>