问一个关于四叉树的问题.

j8daxue 2012-07-07 03:00:06
最近发现工作中经常会有按格子划分地图的思想,名曰四叉树,到网上找找资料。
看到一题试试自己的最初的理解。不料WA,查看代码半天感觉没问题.
http://acm.cugb.edu.cn/showproblem.php?problemid=1155


#include <iostream>
#include <deque>
#include <string.h>
using namespace std;
struct node
{
node* child[4];
bool zero;
node()
:zero(true)
{
memset(child,0,sizeof(child));
}
void print()
{
if(child[0])
{
cout<<1;
return;
}
cout<<0<<(zero ? 0 : 1);
}
};
node gnode[257];
char gmap[16][16];
node* alloc()
{
static int i = 0;
return &gnode[i++];
}
void print(node* root)
{
deque<node*> dq;
dq.push_back(root);
cout<<1;
while(!dq.empty())
{
node* front = dq.front();
dq.pop_front();
for(int i = 0 ; i < 4 ; ++ i)
{
node* child = front->child[i];
child->print();
if(child->child[0])
dq.push_back(child);
}
}
cout<<endl;
}
bool check_blocks(int x,int y,int n)
{
char c = gmap[y][x];
if(n == 1) return true;
for(int i = y ; i < y + n; ++ i)
{
for(int j = x ; j < x + n ; ++ j)
{
if(gmap[i][j] != c) return false;
}
}
return true;
}
void get_start(int i,int n,int sx,int sy,int& x,int& y)
{
switch(i)
{
case 0: x = sx; y = sy; break;
case 1: x = sx + n / 2; y = sy; break;
case 2: x = sx; y = sy + n / 2; break;
case 3: x = sx + n / 2; y = sy + n / 2; break;
}
}
void build(node* root,int n,int sx,int sy)
{
int x,y;
for(int i = 0 ; i < 4 ; ++ i)
{
get_start(i,n,sx,sy,x,y);
node* child = alloc();
root->child[i] = child;
if(check_blocks(x,y,n / 2))
{
child->zero = gmap[y][x] == '0';
}
else
{
build(child,n / 2,x,y);
}
}
}
void solve(int n)
{
if(n == 1)
{
cout<<1<<gmap[0][0]<<endl;
}
else
{
node root;
root.zero = true;
build(&root,n,0,0);
print(&root);
}
}
int main()
{
int n;
cin>>n;
for (int i = 0 ; i < n ; ++ i)
for (int j = 0 ; j < n ; ++ j)
cin>>gmap[i][j];
solve(n);
return 0;
}

...全文
247 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
j8daxue 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
话说,楼主这道题好像都没有人AC过啊。
[/Quote]
首先不是每个oj都比较多人去上,我也是偶尔搜索XX树看到这个题。

这题比较简单,可能不是每个人都比较有空帮看吧。
代码能通过sample。而且感觉逻辑清晰,没差错。
W170532934 2012-07-12
  • 打赏
  • 举报
回复
话说,楼主这道题好像都没有人AC过啊。
j8daxue 2012-07-09
  • 打赏
  • 举报
回复

回复内容太短了!
j8daxue 2012-07-09
  • 打赏
  • 举报
回复
上班中无法上网,赶紧上班前顶下,下班后看帖。
W170532934 2012-07-07
  • 打赏
  • 举报
回复
哎,怎么没有人?
j8daxue 2012-07-07
  • 打赏
  • 举报
回复
没人回...

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧