码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 正方形(Squares, ACM/ICPC World Finals 1990, UVa201)rust解法


    有n行n列(2≤n≤9)的小黑点,还有m条线段连接其中的一些黑点。统计这些线段连成了多少个正方形(每种边长分别统计)。
    行从上到下编号为1~n,列从左到右编号为1~n。边用H i j和V i j表示,分别代表边
    (i,j)-(i,j+1)和(i,j)-(i+1,j)。如图4-5所示最左边的线段用V 1 1表示。图中包含两个边长为1的正方形和一个边长为2的正方形。
    在这里插入图片描述

    样例

    4 
    16
    H 1 1
    H 1 3
    H 2 1
    H 2 2
    H 2 3
    H 3 2
    H 4 2
    H 4 3
    V 1 1
    V 1 2
    V 1 4
    V 2 2
    V 2 3
    V 2 4
    V 3 2
    V 3 4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    2 squre of len 1
    1 squre of len 2
    
    • 1
    • 2

    分析:
    把所有边存到集合里。
    对每一种正方形长度,遍历所有点,看集合里是否包含构成正方形的所有边。

    解法:

    use std::{io, collections::HashSet};
    
    fn main() {
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let n: u32 = buf.trim().parse().unwrap();
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let m: u32 = buf.trim().parse().unwrap();
    
        let mut bian= HashSet::new();
        for _i in 0..m {
            let mut buf = String::new();
            io::stdin().read_line(&mut buf).unwrap();
            let mut it = buf.split_whitespace();
            let t = it.next().unwrap().chars().nth(0).unwrap();
            let x: u32 = it.next().unwrap().parse().unwrap();
            let y: u32 = it.next().unwrap().parse().unwrap();
            bian.insert((t, x, y));
        }
        //println!("{:?}", bian);
        for len in 1..n {
            let mut cnt = 0;
            for i in 1..=n-len{
                'foo: for j in 1..=n-len{
                    //println!("len: {}, i,j: {},{}", len, i, j);
                    for step in 0..len {
                        let one = ('H', i, j + step);
                        if !bian.contains(&one) {
                            //println!("{:?}", one);
                            continue 'foo;
                        }
                        let one = ('H', i + len, j + step);
                        if !bian.contains(&one) {
                            //println!("{:?}", one);
                            continue 'foo;
                        }
                        let one = ('V', i + step, j);
                        if !bian.contains(&one) {
                            //println!("{:?}", one);
                            continue 'foo;
                        }
                        let one = ('V', i + step, j + len);
                        if !bian.contains(&one) {
                            //println!("{:?}", one);
                            continue 'foo;
                        }
                    }
                    cnt += 1;
                }
            }
            if cnt > 0 {
                println!("{} squre of len {}", cnt, len);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    HCIP-Datacom-ARST自选题库__BGP多选【22道题】
    在北京工作5年的程序员,通过人才引进入职县城事业编,月薪曝光
    接口测试总结
    docker构建镜像上传到DockerHub
    Linux零基础入门(二)Linux基础命令
    数据结构与算法之Python实现——栈
    2022年十月份钓鱼威胁情报
    Python下非阻塞式读取键盘输入
    socket can查看详细信息 命令 ip -details -statistics link show can0
    Spring Cloud Alibaba【授权规则、系统自适应限流、SentinelResource注解配置详解之只配 置fallback】(八)
  • 原文地址:https://blog.csdn.net/inxunxun/article/details/133909963
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号