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 < ...
随机推荐
- Angular2入门:TypeScript的函数 - 函数定义、可选参数、默认参数和函数重载
- ASP.NET MVC5 + EF6 + LayUI实战教程,通用后台管理系统框架(3)
前言 本节将我们自己的CSS样式替换系统自带的 开始搭建 将脚本文件夹删掉,将内容文件夹里的内容删掉,将我们自己的CSS样式文件,全部复制到内容里边 新建家庭控制器 给家庭控制器添加索引视图 指数代码 ...
- Java字符串和容器
String Java.lang.String是Java的字符串类. Srting是一个不可变对象,所有对String修改的操作都需要构造新的String实例. String可以由char数组或字符串 ...
- 今天通过npm 安装 install 的时候出现的问题
E:\Workspace_WebStorm\angular2>npm install -gC:\Users\lyx\AppData\Roaming\npm`-- angular2@0.0.0 ` ...
- Extjs 在项目中碰到问题
1.切换tabpanel,新建tab关闭后再新建报错,在火狐下报错 TypeError: el is null el.addCls.apply(el, arguments); 这个我在下一篇文章中 ...
- Java 面向对象编程小练习(曾经)
最近打算将之前学习过的小练习分享出来,算是巩固知识.虽然是小练习,但是越看越觉得有趣,温故而知新. 练习:功能跳水比赛,8个评委评分.运动员成绩去掉最高和最低之后的平均分 代码实例: 1.导包 imp ...
- lfs(systemd版本)学习笔记-第2页
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs(systemd)学习笔记-第1页 的地址:https://www.cnblogs.com/renren-study-no ...
- Python args kwargs 技巧
def f(*args): print(args) a=[1, 2, 3] f(a) f(*a) 运行结果: ([1, 2, 3],) (1, 2, 3) def f(**kwargs): print ...
- csharp: mappings using Dapper-Extensions+Dapper.net.
sql: CREATE TABLE [PotoUsers] ( [UserID] INT IDENTITY(1,1) PRIMARY KEY, [UserName] NVARCHAR(50), [Fi ...
- 【读书笔记】iOS-如何选择本地化应用
早在2007年发布iPhone的时候 ,苹果并没有一同发布本地化SDK,苹果公司声称不需要本地SDK,鼓励大家使用JavaScript,CSS和HTML开发Web应用.但接下来剧情并没有按照苹果设计的 ...