UVA 297 - Quadtrees

我自己的神码,祸害了我两天的时间:尴尬

各种逻辑错误,小型代码错误:

思路倒不难,只是这段时间代码敲的少,能力严重下降了!!!

主要是用递归函数来模拟人的比较方法。和二叉树的逻辑差不多的。

代码如下:

#include <iostream>
#include <string>
using namespace std;
string s1, s2;
int black;
int s1n, s2n;
int mypow(int e, int n)//我自己写的pow()函数;
{
    int sum= 1;
    for(int i = 0; i < n; i++)
        sum*=e;
    return sum;
}
int returnblack(int presionnum, int x)//returnblack()函数用来对付【e】【peeepeeef】的情况
{
    for(int i = 1; i <= 4; i++)
    {
        if(x == 1)
        {
            if(s1[s1n] == 'p'){++s1n;returnblack(presionnum+1, 1);}
            else {
                    if(s1[s1n] == 'f')black += (1024/(mypow(4,presionnum)));
                    ++s1n;//cout<<s1n<<endl;
                 }
        }
        else
        {
            if(s2[s2n] == 'p'){++s2n;returnblack(presionnum+1, 2);}
            else {
                    if(s2[s2n] == 'f')black += (1024/(mypow(4,presionnum)));
                    ++s2n;
                }
        }
    }
    return 0;
}
int nextnode(int x)//用来对付【f】【peeepeeef】的情况。
{
    for(int i = 1; i <= 4; i++)
    {
        if(x == 1)
        {
            if(s1[s1n] == 'p'){++s1n;nextnode(1);}
            else ++s1n;
        }
        else
        {
            if(s2[s2n] == 'p'){++s2n;nextnode(2);}
            else ++s2n;
        }
    }
    return 0;
}
int Cacultor(int presionnum)//当p与p相遇时就进行子代的比较。如:【peeef】【pffff】
{

    for(int temp = 1; temp <= 4;temp++)
    {//cout<<"****"<<s1[s1n]<<":"<<s1n<<"  "<<"****"<<s2[s2n]<<":"<<s2n<<endl;
        if(s1[s1n] == 'p' && s2[s2n] == 'p')
            {++s1n; ++s2n;Cacultor(presionnum+1);}
        else if(s1[s1n] == 'f' || s2[s2n] == 'f')
        {
            black += (1024/mypow(4, presionnum));//cout<<"***"<<black<<endl;
            if(s1[s1n] == 'p') {++s2n;++s1n;nextnode(1);}
            else if(s2[s2n] == 'p') {++s1n;++s2n;nextnode(2);}
            else { ++s1n; ++s2n;}
        }
        else if(s1[s1n] == 'e' || s2[s2n] == 'e' )
        {
            if(s1[s1n] == 'p')
                {
                    ++s2n;++s1n;returnblack(presionnum+1,1);
                }
            else if(s2[s2n] == 'p')
                {
                    ++s1n;++s2n;returnblack(presionnum+1,2);
                }
            else {++s1n; ++s2n;}
        }
    }
    return 0;
}

int main ()
{
    int num;
    cin>>num;
    while(num--)
    {
        black = s1n = s2n = 0;//black用来记录黑色像素的个数,s1n和s2n分别用来记录s1和s2的比较节点处的当前位置。
        cin>>s1>>s2;
        if(s1[0] == 'p'&& s2[0] == 'p')
           {++s1n;++s2n; Cacultor(1);}
        else if(s1[0] == 'f' || s2[0] == 'f')
        {
            black += (1024/mypow(4, 0));
            if(s1[0] == 'p') nextnode(1);
            else if(s2[0] == 'p') nextnode(2);
        }
        else if(s1[0] == 'e' || s2[0] == 'e' )
        {
            if(s1[0] == 'p')
                returnblack(0,1);
            else if(s2[0] == 'p')
                returnblack(0,2);
        }
        cout<<"There are "<<black<<" black pixels."<<endl;
    }
    return 0;
}


posted on 2012-11-02 22:18  Primo...  阅读(156)  评论(0编辑  收藏  举报