UVA806-Spatial Structures(四分树)
Problem UVA806-Spatial Structures
Accept:329 Submit:2778
Time Limit: 3000 mSec
Problem Description
Input
The input contains one or more images. Each image is square, and the data for an image starts with an integer n, where |n| is the length of a side of the square (always a power of two, with |n| < 64) followed by a representation of the image. A representation is either a sequence of n2 zeros and ones comprised of |n| lines of |n| digits per line, or the sequence of numbers that represent the root-to-leaf paths of each black node in the quadtree that represents the image. If n is positive, the zero/one representation follows; if n is negative, the sequence of black node path numbers (in base 10) follows. The sequence is terminated by the number -1. A one-node tree that represents an all-black image is represented by the number 0. A one-node tree that represents an all-white image is represented by an empty sequence (no numbers). The end of data is signaled by a value of 0 for n.
Output
For each image in the input, first output the number of the image, as shown in the sample output. Then output the alternate form of the image. If the image is represented by zeros and ones, the output consists of root-to-leaf paths of all black nodes in the quadtree that represents the image. The values should be base 10 representations of the base 5 path numbers, and the values should be printed in sorted order. If there are more than 12 black nodes, print a newline after every 12 nodes. The total number of black nodes should be printed after the path numbers. If the image is represented by the root-to-leaf paths of black nodes, the output consists of an ASCII representation of the image with the character ‘.’ used for white/zero and the character ‘*’ used for black/one. There should be n characters per line for an n×n image.
Sample Input
Sample Ouput
Image 1
9 14 17 22 23 44 63 69 88 94 113
Total number of black nodes = 11
Image 2
........
........
....****
....****
...*****
..******
..****..
..***...
Image 3
Total number of black nodes = 0
Image 4
****
****
****
****
题解:这个题如果没有lrj前面例题的铺垫,我自己估计是搞不定,不过做了那个例题之后,这个题就是稍微麻烦一点,没什么特殊的地方,重在代码基本功。
这个题的输出格式有点坑,总的来说就是题中没说的换行不要有,尤其是最后一个Case。
四分树例题:UVA297:Quadtrees
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = ;
int n,cnt;
char gra[maxn][maxn];
vector<int> ans; bool is_black(int r,int c,int wide){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
if(gra[i][j] == '') return false;
}
}
return true;
} bool is_white(int r,int c,int wide){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
if(gra[i][j] == '') return false;
}
}
return true;
} int consumption(string &ss){
int res = ;
for(int i = ss.size()-;i >= ;i--){
res *= ;
res += ss[i]-'';
}
return res;
} void cal(string str,int r,int c,int wide){
if(is_black(r,c,wide)){
ans.push_back(consumption(str));
return;
}
else if(is_white(r,c,wide)) return;
else{
cal(str+"",r,c,wide/);
cal(str+"",r,c+wide/,wide/);
cal(str+"",r+wide/,c,wide/);
cal(str+"",r+wide/,c+wide/,wide/);
}
} int solve(int n){
cnt = ;
ans.clear();
for(int i = ;i < n;i++){
scanf("%s",gra[i]);
}
string str = "";
if(is_black(,,n)) return ;
if(is_white(,,n)) return -;
cal(str+"",,,n/);
cal(str+"",,n/,n/);
cal(str+"",n/,,n/);
cal(str+"",n/,n/,n/);
return ;
} vector<int> num; void converse(int val,string &ss){
while(val){
ss += (val%+'');
val /= ;
}
} void draw(string &ss,int pos,int r,int c,int wide){
if(pos == ss.size()){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
gra[i][j] = '*';
}
}
return;
}
if(ss[pos] == ''){
draw(ss,pos+,r,c,wide/);
}
else if(ss[pos] == ''){
draw(ss,pos+,r,c+wide/,wide/);
}
else if(ss[pos] == ''){
draw(ss,pos+,r+wide/,c,wide/);
}
else draw(ss,pos+,r+wide/,c+wide/,wide/);
} void solve2(int n){
int x;
num.clear();
memset(gra,,sizeof(gra));
while(scanf("%d",&x) && x!=-) num.push_back(x);
if(num.size()== && num[]==){
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
printf("*");
}
printf("\n");
}
return;
}
for(int i = ;i < num.size();i++){
string ss = "";
converse(num[i],ss);
draw(ss,,,,n);
}
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
if(gra[i][j] == '*') printf("%c",gra[i][j]);
else printf(".");
}
printf("\n");
}
} int iCase = ; int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
bool flag = false;
while(~scanf("%d",&n) && n){
if(flag) printf("\n");
flag = true;
if(n > ){
int flag = solve(n);
printf("Image %d\n",iCase++);
if(flag == ){
printf("%d\n",);
printf("Total number of black nodes = %d\n",);
}
else if(flag == -){
printf("Total number of black nodes = %d\n",);
}
else{
sort(ans.begin(),ans.end());
int cnt = ;
for(int i = ;i < ans.size();i++){
if(cnt == ) printf("%d",ans[i]);
else printf(" %d",ans[i]);
cnt++;
if(cnt == ) cnt = ,printf("\n");
}
if(ans.size()%)printf("\n");
printf("Total number of black nodes = %d\n",ans.size());
}
}
else{
printf("Image %d\n",iCase++);
solve2(-n);
}
}
return ;
}
UVA806-Spatial Structures(四分树)的更多相关文章
- UVA-806 Spatial Structures (四分树)
题目大意:将一块图像上的黑点在两种表示法之间转换. 题目分析:递归下去... 注意:输出时要注意细节!!! 代码如下: # include<iostream> # include<c ...
- [刷题]算法竞赛入门经典(第2版) 6-8/UVa806 - Spatial Structures
题意:黑白图像的路径表示法 代码:(Accepted,0.120s) //UVa806 - Spatial Structures //Accepted 0.120s //#define _XIENAO ...
- uva806 Spatial Structures 空间结构 (黑白图像的四分树表示)
input 8 00000000 00000000 00001111 00001111 00011111 00111111 00111100 00111000 -8 9 14 17 22 23 44 ...
- UVa 297 (四分树 递归) Quadtrees
题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表
4513: [Sdoi2016]储能表 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 395 Solved: 213[Submit][Status] ...
- [C++]四分树(Quadtrees)
[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] 四分树Quadtrees 一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个 ...
- Uva297 Quadtrees【递归建四分树】【例题6-11】
白书 例题6-11 用四分树来表示一个黑白图像:最大的图为根,然后按照图中的方式编号,从左到右对应4个子结点.如果某子结点对应的区域全黑或者全白,则直接用一个黑结点或者白结点表示:如果既有黑又有白,则 ...
- 四分树 (Quadtrees UVA - 297)
题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...
随机推荐
- C++ 重载运算符简单举例
我们可以重定义或重载大部分 C++ 内置的运算符.这样,就能使用自定义类型的运算符. 重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的.与其他函数一 ...
- [PHP] 算法-找出两个链表的第一个公共结点的PHP实现
输入两个链表,找出它们的第一个公共结点 1.两个单链表,有公共结点,那么必然,尾部公用 2.找出链表1的长度,找出链表2的长度,长的链表减去短的链表得出一个n值 3.长的链表先走n步,两个链表再同时移 ...
- webpack4 系列教程(四): 单页面解决方案--代码分割和懒加载
本节课讲解webpack4打包单页应用过程中的代码分割和代码懒加载.不同于多页面应用的提取公共代码,单页面的代码分割和懒加载不是通过webpack配置来实现的,而是通过webpack的写法和内置函数实 ...
- python之锁, 队列
进程的其他方法 进程id,进程名字,查看进程是否活着is_alive() terminate()发送结束进程的信号 import time import os from multiprocessin ...
- 【机器学习】激活函数(Activation Function)
https://blog.csdn.net/ChenVast/article/details/81382795 激活函数是模型整个结构中的非线性扭曲力 神经网络的每层都会有一个激活函数 1.逻辑函数( ...
- TUM数据集rgbd_benchmark工具的使用方法
# 在学习视觉slam过程中,需要对数据集合进行预处理和对slam或者跟踪结果进行评价,TUM提供一组这样的工具,为了自己以后方便查找,于是把它记录下来 一.RGBD_Benchmark工具下载链接: ...
- Android解析XML文件
XML文件和获取XML值 XML文件样例 <?xml version="1.0" encoding="utf-8"?> <citys> ...
- vue 环境的搭建及初始化项目
其实超级简单,虽然网上很多,但是我顺便记录下相当于做笔记吧 1nodejs 的安装, 在node官网下载,点击安装,安装的时候最好选择路径在d盘 2设置环境变量 我的电脑-->属性-->系 ...
- Android View体系(三)属性动画
上一篇文章讲了View滑动的六种方法,其中一种是使用动画,这篇文章我们来讲一讲动画的其中一种:属性动画. 1.android视图动画和属性动画 视图动画我们都了解,它提供了AlphaAnimation ...
- Fiddler抓包使用教程-Https
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72956016 本文出自[赵彦军的博客] 开启 Https 抓包 Fiddler 默 ...