二、递归(一)

一、基本思想

二、递归(一)_第1张图片
二、递归(一)_第2张图片
二、递归(一)_第3张图片
二、递归(一)_第4张图片
二、递归(一)_第5张图片
二、递归(一)_第6张图片
二、递归(一)_第7张图片

二、 实例-小游戏

二、递归(一)_第8张图片
二、递归(一)_第9张图片
二、递归(一)_第10张图片
二、递归(一)_第11张图片
二、递归(一)_第12张图片
二、递归(一)_第13张图片
二、递归(一)_第14张图片
二、递归(一)_第15张图片
二、递归(一)_第16张图片
二、递归(一)_第17张图片
二、递归(一)_第18张图片
二、递归(一)_第19张图片
二、递归(一)_第20张图片
二、递归(一)_第21张图片
二、递归(一)_第22张图片
二、递归(一)_第23张图片
二、递归(一)_第24张图片
二、递归(一)_第25张图片
二、递归(一)_第26张图片
二、递归(一)_第27张图片
#include "stdafx.h"
#include
#include
#define MAXIN 75  
using namespace std;


char board[MAXIN + 2][MAXIN + 2];  //定义矩阵板,外围为空
int minstep, w, h, to[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //定义方向
bool mark[MAXIN + 2][MAXIN + 2]; //定义标记数组

//搜索函数
void Search(int now_x, int now_y, int end_x, int end_y, int step, int f) {
    if (step > minstep) {
        return;    //当前路径数大于minstep, 返回->优化策略
    }
    if (now_x == end_x && now_y == end_y) {   //到达终点
        if (minstep > step)  {   //更新最小路径数 
            minstep = step;
        }
        return;
    }
    for (int i = 0; i<4; i++) {  //枚举下一步的方向
        int x = now_x + to[i][0];
        int y = now_y + to[i][1];
        if ((x>-1) && (x-1) && (y>begin_x>>begin_y>>end_x>>end_y ){
            //读入起始点和终点
            if (begin_x == 0)break;
            printf("%d %d %d %d \n", begin_x,begin_y, end_x, end_y);
            cout << "begin search...\n";
            count++;
            minstep = 1000;
            memset(mark, false, sizeof(mark));
            //递归搜索
            Search(begin_x, begin_y, end_x, end_y, 0, -1);
            if (minstep < 1000)printf("pair %d: %d segments.\n", count, minstep);
            else printf("pair: %d: impossible.\n", count);
        }
        printf("\n");
    }




    return 0;
}

【相关文档】
1.老生常谈,正确使用memset
2.递归-小游戏(算法基础 第3周)
3.Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
4 深入了解scanf()/getchar()和gets()等函数

你可能感兴趣的:(二、递归(一))