【剑指offer】回溯法

面试题12:矩阵中的路径

//上下左右递归,在看边界条件
#include<iostream>
#include<vector>
using namespace std;

//递归
bool haspathcore( char* m, int row, int col, int i, int j, const char* str, int& pathlen, bool* vis)
{
	if (str[pathlen] == '\0')
	{
		return true;
	}
	bool haspath = false;
	if (i >= 0 && i < row && j >= 0 && j < col && m[i*col + j] == str[pathlen] && !vis[i*col + col])
	{
		++pathlen;
		vis[i*col + j] = true;

		haspath = haspathcore(m, row, col, i + 1, j, str, pathlen, vis) ||
			haspathcore(m, row, col, i - 1, j, str, pathlen, vis) ||
			haspathcore(m, row, col, i, j + 1, str, pathlen, vis) ||
			haspathcore(m, row, col, i, j - 1, str, pathlen, vis);

		if (!haspath)
		{
			--pathlen;
			vis[i*col + j] = false;
		}
	}
	return haspath;

}
//主遍历
bool haspath( char* m, int row, int col, const char* str)
{
	if (str == NULL || m == NULL || row < 1 || col < 1)
	{
		return false;
	}

	bool *vis = new bool[row*col];
	memset(vis, 0, row*col);

	int pathlen = 0;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (haspathcore(m, row, col, i, j, str, pathlen, vis))
			{
				delete[] vis;
				return true;
			}
		}
	}
	
	return false;
}
 
int main()
{
	char test[] = "abtgcfcsjdeh";
	char str[] = "bt";
	bool resu = haspath(test, 3, 4, str);
	cout << resu << endl;
	getchar();
	return 0;
	 
	 
}

面试题13:机器人的运动范围

思路:可以向上下左右移动 则递归(i+1,j),(i-1,j),(i,j+1),(i,j-1)
不能进入行列的数位之和相加>18 判断条件 i+j<18   而且没有访问过 !vis
得到行列之和  35  3行5列 3+5

#include<iostream>
using namespace std;
//得到行列数位之和
int getDigitSum(int num)
{
	int sum = 0;
	while (num > 0)
	{
		sum += num % 10;
		num /= 10;
	}
	return sum;
}

//检查判断条件
bool check(int threshold, int row, int col, int i, int j, bool* vis)
{
	if (i >= 0 && i < row&&j >= 0 && j < col&&getDigitSum(i) + getDigitSum(j) <= threshold && !vis[i*col + j])
	{
		return true;
	}
	return false;
}

//递归
int movingCountCore(int threshold, int row, int col, int i, int j, bool* vis)
{
	int count = 0;
	if (check(threshold, row, col, i, j, vis))
	{
		vis[i*col + j] = true;
		count = 1 + movingCountCore(threshold, row, col, i + 1, j, vis)
			+ movingCountCore(threshold, row, col, i - 1, j, vis)
			+ movingCountCore(threshold, row, col, i, j + 1, vis)
			+ movingCountCore(threshold, row, col, i, j - 1, vis);
	}
	return count;
}

//总的遍历
int movingCount(int threshold, int row, int col)
{
	if (threshold < 0 || row <= 0 || col <= 0)
	{
		return 0;
	}
	bool* vis = new bool[row*col];
	for (int i = 0; i < row*col; i++)
	{
		vis[i] = false;
		
	}
	int count = movingCountCore(threshold, row, col, 0, 0, vis);
	delete[] vis;
	return count;
}

int main()
{
	 
	int count = movingCount(5, 4, 4);
	cout << "count= " << count << endl;
	getchar();
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值