想知道为什么我只搜索完成一次就结束了??
查看原帖
想知道为什么我只搜索完成一次就结束了??
318876
金城武楼主2020/5/16 20:54
#include <iostream>
using namespace std;
int a[21][21] = { 0 },s=0, ne[8][2] , step = 0, book[21][21] = { 0 }, i, j, k, sum;
int n, m, c, d;
void dfs(int x, int y)
{
	
	int tx, ty;
	int next[2][2] = {
		{0,1},{1,0}};
	if (x == n && y == m)
	{
		sum++;
		return;
	}
	for (k = 0; k <2; k++)
	{
		tx = x + next[k][0];
		ty = y + next[k][1];
		if (tx<0 || tx>n || ty<0 || ty>m )
			continue;
		for (i = 0; i < 8; i++)
		{
			if ((tx == ne[i][0] &&ty == ne[i][1]))
			{
				 s++;
			}
		}
		if (s == 1)
			continue;
		
		if (book[tx][ty] == 0)
		{
			
			book[tx][ty] = 1;
			dfs(tx, ty);
			book[tx][ty] = 0;
		}
	}
	
}
int main()
{
	
	cin >> n >> m;
	cin >> c >> d;
	int ne[8][2] = { {c + 2,d + 1},{c + 1,d + 2},{c - 1,d + 2},{c - 2,d + 1},{c - 2,d - 1},{c - 1,d - 2},{c + 1,d - 2},{c + 2,d - 1} };
	book[0][0] = 1;
	dfs(0,0);
	cout << sum;
}
2020/5/16 20:54
加载中...