一笔画

版权声明:晓程原创 https://blog.csdn.net/qq_43469554/article/details/88165920

如图,问有多少种方法一笔画走到终点?
在这里插入图片描述

#include <iostream>
using namespace std;
int vis[10][10];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int ans;
void dfs(int x, int y, int step)
{
	if (x > 2 ||  x < 0 || y > 4 || y < 0)
	{
		return;
	}
	if (x == 2 && y == 0 && step == 14)
	{
		ans++;
		return;
	}
	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if (!vis[tx][ty])
		{
			dfs(tx, ty, step + 1);
		}
	} 
	vis[x][y] = 0;
}

int main()
{
	dfs(0, 0, 0);
	cout << ans << endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/88165920