四叉树 bnuoj
建树+广搜一棵树;最下面有更短代码(很巧妙)。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm> using namespace std; int maze[][];
struct node
{
int num;
node *next[];
};
void init_tree(node *root)
{
int i;
for(i=; i<; i++)
root->next[i] = NULL;
}
void built(int istart,int iend,int jstart,int jend,node *root)
{
int dive,i,j;
int istart1=istart,iend1=iend,jstart1=jstart,jend1=jend;
for(dive=; dive<; dive++)
{
if(dive==) istart=istart1,iend=istart1+(iend1-istart1+)/-,jstart=jstart1,jend=jstart1+(jend1-jstart1+)/-;
else if(dive==) istart=istart1,iend=istart1+(iend1-istart1+)/-,jstart=jstart1+(jend1-jstart1+)/,jend=jend1;
else if(dive==) istart=istart1+(iend1-istart1+)/,iend=iend1,jstart=jstart1,jend=jstart1+(jend1-jstart1+)/-;
else istart=istart1+(iend1-istart1+)/,iend=iend1,jstart=jstart1+(jend1-jstart1+)/,jend=jend1;
int flag0=,flag1=,tree_point;
for(i=istart; i<=iend; i++)
{
for(j=jstart; j<=jend; j++)
{
if(maze[i][j]==) flag0=;
if(maze[i][j]==) flag1=;
}
}
if(flag0&&flag1) tree_point=;
else if(flag0&&!flag1) tree_point=;
else if(!flag0&&flag1) tree_point=;
node *p;
p=new node;
init_tree(p);
root->next[dive]=p;
p->num=tree_point;
if(tree_point==||tree_point==) continue ;
built(istart,iend,jstart,jend,p);
}
}
void print(node *root)
{
node *ss,*tt;
queue<node*>q;
q.push(root);
while(!q.empty())
{
ss=q.front();
int su=ss->num;
if(su==) printf("");
else if(su==) printf("");
else if(su==) printf("");
q.pop();
for(int i=; i<; i++)
{
if(ss->next[i] !=NULL)
{
tt=ss->next[i];
q.push(tt);
}
}
}
printf("\n");
}
int check_map(int n)
{
int i,j,sum=;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
sum+=maze[i][j];
if(sum==n*n) return ;
else if(sum==) return ;
else return -;
}
void delete_tree(node *root)
{
int i;
for(i=; i<; i++)
{
if(root->next[i]!=NULL)
{
delete_tree(root->next[i]);
}
}
delete root;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
memset(maze,,sizeof(maze));
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
scanf("%d",&maze[i][j]);
}
int ans=check_map(n);
if(ans==) printf("00\n");
else if(ans==) printf("01\n");
else
{
node *tree;
tree=new node;
init_tree(tree);
tree->num=;
built(,n,,n,tree);
print(tree);
delete_tree(tree);
}
}
return ;
}
短代码:
#include <cstring>
#include <vector>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
//#define N 10005
using namespace std;
int zu[][],n;
struct sb
{
int x,y,x1,y1;
bool Get()
{
int ans=;
for(int i=x;i<=x1;i++)
for(int j=y;j<=y1;j++)
ans+=zu[i][j];
if(!ans)
return true;
if(ans==(x1-x+)*(y1-y+))
return true;
return false;
}
};
int main()
{
while(scanf("%d",&n)==)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&zu[i][j]);
//
sb s1,s2;
s1.x=;
s1.y=;
s1.x1=n;
s1.y1=n;
queue<sb>Q;
Q.push(s1);
while(!Q.empty())
{
s1=Q.front();
Q.pop();
if(s1.Get())
{
printf("0%d",zu[s1.x1][s1.y1]);
continue;
}
printf("");
int xx=(s1.x+s1.x1)/;
int yy=(s1.y+s1.y1)/;
s2=s1;
s2.y1=yy;
s2.x1=xx;
Q.push(s2);
//
s2=s1;
s2.x1=xx;
s2.y=yy+;
Q.push(s2);
//
s2=s1;
s2.x=xx+;
s2.y1=yy;
Q.push(s2);
//
s2=s1;
s2.x=xx+;
s2.y=yy+;
Q.push(s2);
}
printf("\n");
}
}
四叉树 bnuoj的更多相关文章
- BNUOJ 4049 四叉树
四叉树 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Ma ...
- 地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了
地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了 四叉树对于区域查询,效率比较高. 原理图
- HTML5实现3D和2D可视化QuadTree四叉树碰撞检测
QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是 ...
- HT for Web可视化QuadTree四叉树碰撞检测
QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是 ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- [转]一个四叉树Demo学习
程序代码: http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 四叉树: using Sys ...
- [转]基于四叉树(QuadTree)的LOD地形实现
实现基于四叉树的LOD地形时,我遇到的主要问题是如何修补地形裂缝. 本段我将描述使用LOD地形的优势,我实现LOD地形的思路,实现LOD地形核心模块的详细过程,以及修补地形裂缝的思路. 首先,LOD地 ...
- 线性四叉树十进制Morton码计算示例
线性四叉树十进制Morton码计算,具体算法描述龚健雅<地理信息系统基础>P108
随机推荐
- Spring 3.0 注解
原文 :http://www.blogjava.net/ashutc/archive/2011/04/14/348270.html 另两 参考博客 : http://kingtai168.iteye. ...
- 【SQL Server 学习系列】-- 获取字符串中出现某字符的次数及字符某次出现的下标
) = '1_BB_CC_DD_AA_EE_YY_WW_HH_GG' --// 1. 获取下划线在字符串中出现的次数 SELECT LEN(@Str) - LEN(REPLACE(@Str, '_', ...
- Camtasia Studio录制屏幕字迹不清晰的原因
Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...
- ubuntu harddisk uuid already exists
就是virtualbox下先用u盘启动的虚拟机,把U盘的vhdk文件拷贝到本机,然后再启动,就有问题,提示什么uuid already exist 找了半天,网上基本都是说windows下如何用的.. ...
- BUPT复试专题—排序(2009)
题目描述 查找序列a 中小于 b 的第 i 个数的数的个数 输入 输入有多组,每组四行第一行:序列a个数N第二行:(序列a的)N个数,升序排列第三行:序列b个数M 第四行:(序列b的)M个数,升序排列 ...
- 天下文章一大抄 mysql远程连接
使用GRANT命令创建远程连接mysql授权用户特定用户mysql -u root -ppassword 注意:p后面没有空格直接密码.mysql>grant all privileges ...
- [WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment
js-sys offers bindings to all the global APIs available in every JavaScript environment as defined b ...
- Autolayout和VFL
Autolayout,開始于iOS6.0 一.什么时候用autolayout比較适合 1.不负责任的骑墙派说法:apple的设备越来越多了,你的应用应该都使用al. (而且用sb) 2.要 ...
- winform中使用ReportViewer的时候,找不到报表数据面板.
最近在做winform的报表项目,看人家教程里都有报表数据面板,可我的怎么都找不到,我的是VS2012,最后终于找到,首先必须在报表设计TAB上(选中它),然后选择视图->报表数据
- cin,和几个get函数的用法
1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...