zoukankan      html  css  js  c++  java
  • 【紫书】Quadtrees UVA

    题意:前序遍历给出两个像素方块。求两个方块叠加后有几个黑色格子。

    题解:每次读进来一个方块,就在二维数组上涂色。每次把白色涂黑就cnt++;

       具体递归方法是以右上角坐标与边长为参数,每次通过几何规律往下递归一层。

       如果当前节点是'p'就继续递归,如果是f,e就说明是叶子结点,e直接返回,f对整个区域涂色。

    #define _CRT_SECURE_NO_WARNINGS
    #include "stdio.h"
    #include<stdio.h>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<list>
    #include<set>
    #include<iostream>
    #include<string.h>
    #include<queue>
    #include<string>
    #include<sstream>
    using namespace std;
    const int maxn = 1024+5;
    const int len = 32;
    char s[maxn];
    int buf[len][len], cnt;
    void draw(const char *s, int &p, int r, int c, int w) {
        char ch = s[p++];
        if (ch == 'p') {
            draw(s, p, r, c + w / 2, w / 2);
            draw(s, p, r, c, w / 2);
            draw(s, p, r + w / 2, c, w / 2);
            draw(s, p, r + w / 2, c + w / 2, w / 2);
    
        }else if(ch=='f')
            for(int i=r;i<r+w;i++)
                for(int j=c;j<c+w;j++)
                    if (buf[i][j] == 0) { buf[i][j] = 1; cnt++; }
    }
    int main(){
        int t; cin >> t; while (t--) {
            memset(buf, 0, sizeof(buf));
            cnt = 0;
            for (int i = 0; i < 2; i++) {
                scanf("%s", s);
                int p = 0;
                draw(s, p, 0, 0, len);
            }
            printf("There are %d black pixels.
    ", cnt);
        }
        return 0;
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    Spring Boot 缓存技术:Spring Boot
    Java基础之Iterable接口
    使用sqlyog连接 Mysql 出现1251错误
    IDEA更改主题插件——Material Theme UI详解
    免安装版的Mysql
    使用Nexus搭建Maven私服
    Spring之注解注入bean
    Idea springboot 配置热部署
    Spring Boot 异常处理与单元测试
    Ubuntu20.04在线安装VMware-Tools
  • 原文地址:https://www.cnblogs.com/SuuT/p/8810319.html
Copyright © 2011-2022 走看看