UVa 四叉树

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=233

参考了刘汝佳的算法,写得太妙了。

因为最多是1024块,所以每行每列最多是32,利用先序遍历,一旦是'p'时,就访问第1块,如果第一块内还有细分,则继续递归下去。然后继续依次访问第2,3,4块空间。

 #include<iostream>
#include<cstring>
using namespace std; const int len = ;
const int maxn = + ;
char s[maxn];
char buf[maxn][maxn];
int number; void solve(char *s, int &p,int r,int c,int w)
{
char q = s[p++];
if (q == 'p')
{
solve(s, p, r, c + w / , w / );
solve(s, p, r, c, w / );
solve(s, p, r + w / , c, w / );
solve(s, p, r + w / , c + w / , w / );
}
else if (q=='f')
for (int i = r; i < r + w;i++)
for (int j = c; j < c + w;j++)
if (buf[i][j] == )
{
buf[i][j] = ;
number++;
}
} int main()
{
int t;
cin >> t;
while (t--)
{
memset(buf, , sizeof(buf));
number = ;
cin >> s;
int p = ;
solve(s,p,,,len);
cin >> s;
p = ;
solve(s,p,,,len);
cout << "There are " << number << " black pixels." << endl;
}
return ;
}
上一篇:Schlumberger Petrel 2016.3 地震解释 油藏模拟


下一篇:【The Most Important】浅谈JSP表单Post方式中文乱码问题