poj-2488

//376K	16MS	G++
#include <stdio.h>
#include <string.h>
#define MAX 28 

int board[MAX][MAX];
int p;
int q;
struct Point{
	int x;
	int y;
};

typedef struct Point Point;

Point visitOrder[MAX];


int visitedSquareNum;
int flag[MAX][MAX];
int squareNum = 0;

#define INF 999999;

int dfs(int curX, int curY, int visitedSquareNum);

int checkSquare(int curX, int curY, int visitedSquareNum) {
	if (!flag[curX][curY]) {
		int res = dfs(curX, curY, visitedSquareNum + 1); 
		return res;
	}
	return 0;
}

int dfs(int curX, int curY, int visitedSquareNum) {
	// printf("dfs %d %d\n", curX, curY);
	int maxVisted = 0;
	flag[curX][curY] = 1;
	visitOrder[visitedSquareNum].x = curX;
	visitOrder[visitedSquareNum].y = curY;

	if (visitedSquareNum == squareNum) {
		return visitedSquareNum;
	}

	// x-1, y-2
	if (curX - 1 >= 0 && curY - 2 >= 0) {
		int res = checkSquare(curX - 1, curY - 2, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x+1, y-2
	if (curX + 1 <= p - 1 && curY - 2 >= 0) {
		int res = checkSquare(curX + 1, curY - 2, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	// x-2, y-1
	if (curX - 2 >= 0 && curY - 1 >= 0) {
		int res = checkSquare(curX - 2, curY - 1, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x+2, y-1
	if (curX + 2 <= p - 1 && curY - 1 >= 0) {
		int res = checkSquare(curX + 2, curY - 1, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x-2, y+1
	if (curX - 2 >= 0 && curY + 1 <= q-1) {
		int res = checkSquare(curX - 2, curY + 1, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x+2, y+1
	if (curX + 2 <= p-1 && curY + 1 <= q-1) {
		int res = checkSquare(curX + 2, curY + 1, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x-1, y+2
	if (curX - 1 >= 0 && curY + 2 <= q-1) {
		int res = checkSquare(curX - 1, curY + 2, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	//x+1, y+2
	if (curX + 1 <= p-1 && curY + 2 <= q-1) {
		int res = checkSquare(curX + 1, curY + 2, visitedSquareNum);
		if (res == squareNum) {
			return squareNum;
		}
	}

	flag[curX][curY] = 0;
	return 0; // no find a path to pass all
}

int travel(int curX, int curY) {
	squareNum = p*q;
	return dfs(curX, curY, 1);
}

void printPath() {
	for (int i = 1; i <= squareNum; i++) {
		printf("%c%d", visitOrder[i].y + 'A', visitOrder[i].x + 1);
	}
	printf("\n\n");
}

char findPath() {
	int checkMaxX = p/2 + 1;
	int checkMaxY = q/2 + 1;
	char possible = 0;
	for (int i = 0; i < checkMaxX; i++) { // only need check 1/4 board
		for (int j = 0; j < checkMaxY; j++) {
			memset(flag, 0, sizeof(flag));
			if (travel(i, j)) { // if find a road, break;
				// printf("%d %d\n",i, j);
				return 1;
			}
		}
	}
	return 0;
}

void solve(int caseId) {
	printf("Scenario #%d:\n", caseId);
	if (findPath()) {
		// printf("possible\n");
		// printPath("");
		printPath();
	} else {
		printf("impossible\n\n");
	}
}

int main() {
	int caseNum;
	scanf("%d", &caseNum);
	for (int i = 1; i <= caseNum; i++) {
		scanf("%d %d", &p, &q);
		solve(i);
	}
}

流的题太飘忽了,先放放,刷刷简单的题。

题目本身DFS不难,坑的是字典序输出,不少好汉WA.

每次DFS有8个可选项,这8个选择要按照顺序进行判定,才能满足最后输出路径是字典序的要求。

每次DFS的8个判断都判断是否已经遍历完了所有的square,如果是,那么就直接return square的数量,否则return 0.

每次DFS除了传进去本次DFS开始的位置意外,还要传进去已经遍历的square的数量,这样才能在最后如果遍历完全部square的时候做判断是否完成。

同时搞一个数组来保存DFS的路径。最后输出做一下处理,将数字转化成ABCD。。即可.

这道题其实是验证了那个 国际象棋中马可以不重复跳完所有格子的 结论.

你可能感兴趣的:(poj,图论)