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 < ...
随机推荐
- Docker安装(Debian8)-构建简单的SpringBoot应用
安装docker 1. 建立仓库 移除已安装的docker(docker以前被称为docker或者docker-enginer现在称为docker-ce) apt-get remove docker ...
- 【NET CORE微服务一条龙应用】第二章 配置中心使用
背景 系列目录:[NET CORE微服务一条龙应用]开始篇与目录 在分布式或者微服务系统里,通过配置文件来管理配置内容,是一件比较令人痛苦的事情,再谨慎也有湿鞋的时候,这就是在项目架构发展的过程中,配 ...
- [TJOI 2018]智力竞赛
Description 题库链接 给出一张 \(m\) 个点的有向图.问可重最小路径覆盖是否 \(\leq n+1\) .若不,求最多用 \(n+1\) 条路径去覆盖,最大化未覆盖点点权最小值. \( ...
- Object与Class的区别
1.在Scala中声明private变量,Scala编译器会自动生成get,set方法 2.在Scala中变量需要初始化 3.在Scala中没有静态修饰符,在object下的成员全部都是静态的,如果在 ...
- laravel使用JSON 类型方式进行存储
Laravel 从 5.0 版本开始就已支持 JSON 数据的转换,但这样做的目的只是为了方便数据处理.你的数据依然以 TEXT 类型存放在你的数据库里.不过 MySQL 从 5.7 版本起开始支持原 ...
- canvas学习和滤镜实现
最近学习了 HTML5 中的重头戏--canvas.利用 canvas,前端人员可以很轻松地.进行图像处理.其 API 繁多,这次主要学习常用的 API,并且完成以下两个代码: 实现去色滤镜 实现负色 ...
- vue规格新增一对多的例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 用animate改变了某个样式之后再用css控制会无效
我前几天写了个移动端运动,(这个运动的代码我贴在这了:http://www.cnblogs.com/weiman/p/6600380.html)然后发现了这个问题,具体如下: 未运动前是这样的,菜单栏 ...
- chrome 远程调试相关问题
1.使用chrome remote debug时打开inspect时出现一片空白 2.如何不用FQ可以享受Chrome for android的远程调试功能 3.chrome://appcache-i ...
- IDEA项目搭建十二——站点用户登录会话实现
一.简介 前两天写了一篇用户登录会话设计的脑图,这次就把这个引入到项目中实现,总体来说需要几步先罗列一下: 1.需要一个Cookie工具类用于读写cookie 2.需要一个Cache工具类用于在服务端 ...