递归~红与黑


输出个数:

#include<iostream>
#include<string>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
int w, h;


int f(int x, int y)
{
    if(x < 0 || y < 0 || x >= h || y >= w)        //超区矩阵范围
        return 0;
    if(str[x][y] == '#')
        return 0;
    else
    {
        str[x][y] = '#';        //标记走过的砖
        return f(x, y-1) + f(x, y+1) + f(x-1,y) + f(x+1,y) + 1;
    }
}
int main()
{
    cout << "Enter the row and the column: " << endl;
    cin >> w >> h;
    cout << "Enter the stings: " << endl;
    for(size_t ix = 0; ix != h; ++ix)
    for(size_t index = 0; index != w; ++index)
        cin >> str[ix][index];
    for(size_t ix = 0; ix != h; ++ix)
        for(size_t index = 0; index != w; ++index)
            if(str[ix][index] == '@')
                cout << f(ix,index) << endl;
    return 0;
}



结果:45



输出走过的路径(不重复):

#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
int a[row][column];
int w, h;
int f(int x, int y)
{
    int m, n, flag = 1;
    char str1[200] = {}, str2[200] = {}, str3[500] = {};
    if(x < 0 || y < 0 || x >= h || y >= w)
        return 0;
    if(str[x][y] == '#')
        return 0;
    else
    {
        itoa(x, str1, 10);
        itoa(y, str2, 10);
        strcat(str3, "(");
        strcat(str3, str1);
        strcat(str3, "!");
        strcat(str3, str2);
        strcat(str3, ")\t");
        if(a[x][y] == 0)
            cout << str3;
        a[x][y] =1;
        if(f(x-1,y) != 0 && f(x, y+1) != 0 && flag)
        {
            m = x;
            n = y;
            flag = 0;
        }
        str[x][y] = '#';
        if(f(x-1, y) != 0)
            f(x-1,y);
        else if(f(x, y+1) != 0)
            f(x, y+1);
        else if(f(x+1, y) != 0)
            f(x+1, y);
        else if(f(x, y-1) != 0)
            f(x, y-1);
        else
            f(m, n-1);
    }
}
int main()
{
    cout << "Enter the wideth and the hight: " << endl;
    cin >> w >> h;
    for(size_t ix = 0; ix != h; ++ix)
        for(size_t index = 0; index != w; ++index)
        {
            cin >> str[ix][index];
            if(str[ix][index] == '#')
                a[ix][index] = 1;
        }
    for(size_t ix = 0; ix != h; ++ix)
        for(size_t index = 0; index != w; ++index)
            if(str[ix][index] == '@')
            {
                f(ix, index);
                break;
            }
    cout << endl;
    return 0;
}



//输出走过的路径(重复):

#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
//int a[row][column];
int w, h;
int f(int x, int y)
{
    int m, n, flag = 1;
    char str1[200] = {}, str2[200] = {}, str3[500] = {};
    if(x < 0 || y < 0 || x >= h || y >= w)
        return 0;
    if(str[x][y] == '#')
        return 0;
    else
    {
        itoa(x, str1, 10);
        itoa(y, str2, 10);
        strcat(str3, "(");
        strcat(str3, str1);
        strcat(str3, "!");
        strcat(str3, str2);
        strcat(str3, ")\t");
        //if(a[x][y] == 0)
            cout << str3;
        //a[x][y] =1;
        if(f(x-1,y) != 0 && f(x, y+1) != 0 && flag)
        {
            m = x;
            n = y;
            flag = 0;
        }
        str[x][y] = '#';
        if(f(x-1, y) != 0)
            f(x-1,y);
        else if(f(x, y+1) != 0)
            f(x, y+1);
        else if(f(x+1, y) != 0)
            f(x+1, y);
        else if(f(x, y-1) != 0)
            f(x, y-1);
        else
            f(m, n-1);
    }
}
int main()
{
    cout << "Enter the wideth and the hight: " << endl;
    cin >> w >> h;
    for(size_t ix = 0; ix != h; ++ix)
        for(size_t index = 0; index != w; ++index)
        {
            cin >> str[ix][index];
            //if(str[ix][index] == '#')
               // a[ix][index] = 1;
        }
    for(size_t ix = 0; ix != h; ++ix)
        for(size_t index = 0; index != w; ++index)
            if(str[ix][index] == '@')
            {
                f(ix, index);
                break;
            }
    cout << endl;
    return 0;
}


同时欢迎提出宝贵意见,以帮助我改进,不胜感激!!!

——桑海整理

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值