poj2816-红与黑-C语言-递归算法入门

递归算法入门

#include <stdio.h>
char places[22][22];
int steps;
void slu(int x,int y)
{
    if(places[y][x]=='.'||places[y][x]=='@')
    {
        steps++;
        places[y][x]='#';
        slu(x+1,y);
        slu(x-1,y);
        slu(x,y+1);
        slu(x,y-1);
    }
}
int main()
{
    int x,y,i,j;
    scanf("%d %d",&x,&y);
    while(x)
    {
        steps=0;
        for(i=1;i<=y;i++)
            scanf("%s",&places[i][1]);
        for(i=0;i<x+2;i++)
            places[0][i]=places[y+1][i]='#';
        for(i=0;i<y+2;i++)
            places[i][0]=places[i][x+1]='#';
        for(i=1;i<=y;i++)
            for(j=1;j<=x;j++)
                if(places[i][j]=='@')
                    slu(j,i);
        printf("%d\n",steps);
        scanf("%d %d",&x,&y);
    }
    return 0;
}

    原文作者:递归算法
    原文地址: https://blog.csdn.net/OrdinaryCrazy/article/details/73061065
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞