骑士游历

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 5
#define M 9

void dfs(int x, int y);
void pop(void);
void print(void);
void push(int x, int y);

int maze[N][M];

struct node {
	int x;
	int y;
};

int top = 0;

struct node path[1000];

int count;
void dfs(int x, int y)
{
	push(x, y);
	if (x >= N || x < 0 || y >= M || y < 0) {
		pop();
		return ;
	}
	if (x == N-1 && y == M-1) {
		print();
		printf("%d\n\n\n",count++);
		pop();
		return;
	}
	dfs(x-2, y+1);
	dfs(x-1, y+2);
	dfs(x+1, y+2);
	dfs(x+2, y+1);
	pop();
}

void push(int x, int y)
{
	path[top].x = x;
	path[top].y = y;
	top++;
}

void pop(void)
{
	top--;
}

void print(void)
{
	int i;

	for (i = 0; i < top; i++)
		printf("( %d, %d )\n", path[i].x, path[i].y);
}

int main(void)
{
	count = 0;
	dfs(0, 0);

	return 0;
}
原文地址:https://www.cnblogs.com/steady/p/1859938.html