您的位置:首页 > 其它

POJ 2816 红与黑 解题报告

2009-02-05 23:24 344 查看
POJ 2816 红与黑 解题报告

编号:2816

 

考查点:递归

 

思路:主要的问题在于如何让其向不同方向进行递归,我的思路是另外开个变量,然后switch判断方向,结果自然而然的栈溢出了.。书上的方法确实经典,每次先加了,就不用考虑很多异常情况了,我的数组从1开始,事先全初始化为#,比书上代码又简洁了些.。

 

提交情况:自己的方法提交出现了多次RunTime Error.后来重写的代码还不错,轻松的AC了.。

 

Source Code:

 

//POJ Grids 2816

#include <iostream>

using namespace std;

int x,y;

char ch[25][25];

int Search(int m,int n)

{

    if (ch[m]
=='#')

        return 0;

    else

        ch[m]
 ='#';

    return 1 + Search(m-1,n) + Search(m+1,n) + Search(m,n-1) + Search(m,n+1);

}

int main()

{

    while (true)

    {

        cin>>x>>y;

        if (x==0&&y==0)

            break;

        int m,n;

        memset(ch,'#',sizeof ch);

        for (int i=1;i<=y;i++)

        {

            for (int j=1;j<=x;j++)

            {

                cin>>ch[i][j];

                if (ch[i][j]=='@')

                {

                    m = i;

                    n = j;

                }

            }

        }

        int count = Search(m,n);

        cout<<count<<endl;

    }

    return 0;

}

总结:表把问题想复杂了.。

 

 

                                                       By   Ns517

                                                     
Time 09.02.05
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: