mysql深度优先遍历储存过程_DFS-深度优先遍历

#include

/*

5 4

0 0 1 0

0 0 0 0

0 0 1 0

0 1 0 0

0 0 0 1

0 0 4 1

Total: 9 7 5

Min: 5

--------------------------------

Process exited with return value 0

Press any key to continue . . .

*/

using namespace std;

int n, m;

int minnum = 9999999;

int endy1 = 0, endy2 = 0;

int maze[20][20] = {0}, book[20][20] = {0};

int direction[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };

void DFS(int x, int y, int step)

{

if(x == endy1 && y == endy2)

{

if(step < minnum)

{

minnum = step;

cout << minnum << " ";

}

return;

}

for(int i = 0; i <= 3; i++)

{

int tx = x + direction[i][0];

int ty = y + direction[i][1];

if(tx < 0 || tx > n - 1 || ty < 0 || ty > m - 1)

{

continue;

}

if(maze[tx][ty] == 0 && book[tx][ty] == 0)

{

book[tx][ty] = 1;

DFS(tx, ty, step + 1);

book[tx][ty] = 0;//尝试结束,取消这个点的标记

}

}

return;

}

int main()

{

cin >> n >> m;

for(int i = 0; i < n; i++)

{

for(int j = 0; j < m; j++)

{

cin >> maze[i][j];

}

}

int beginx1, beginx2;

cin >> beginx1 >> beginx2 >> endy1 >> endy2;

cout << endl << "Total: ";

DFS(beginx1, beginx2, 0);

cout << endl << "Min: " << minnum;

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值