View Code of Problem 1001

#include <cstdio>
#include <cstring>
using namespace std;

#define MAX_R 8 
#define DIR_NUM 8
#define MAX_QUE 2000
const int dir[DIR_NUM][2] = {{1,2}, {-1,2}, {1,-2}, {-1,-2}, {2,1}, {-2,1}, {2,-1}, {-2,-1}};

struct point
{
	int x;
	int y;
	int times;
};

point star, end;
bool vis[MAX_R + 3][MAX_R + 3];

int BFS();

int main()
{
	char x1, x2;
	int y1, y2;
	while (scanf("%c%d %c%d%*c", &x1, &y1, &x2, &y2) == 4) {
		// init
		memset(vis, 0, sizeof(vis));
		star.x = x1 - 96;		// 'a' - 1 = 96	
		star.y = y1;
		star.times = 0;
		end.x = x2 - 96;
		end.y = y2;
		end.times = 0;
		
		// output
		printf("To get from %c%d to %c%d takes %d knight moves.\n", x1, y1, x2, y2, BFS());
	}

	return 0;
}

int BFS()
{
	if (star.x == end.x && star.y == end.y) {
		return 0;
	}

	point *que[MAX_QUE];
	int head = 0;
    int	tail = 0;
	que[tail++] = ☆

	while (head < tail) {
		point *root = que[head++];

		if (root->x == end.x && root->y == end.y) {
			return root->times;
		}

		for (int i = 0; i < DIR_NUM; i++) {
			point *child = new point;
			child->x = root->x + dir[i][0];
			child->y = root->y + dir[i][1];
			child->times = root->times;

			if (child->x >= 1 && child->x <= MAX_R
			 && child->y >= 1 && child->y <= MAX_R
			 && vis[child->x][child->y] == 0) {
				vis[child->x][child->y] = 1;
				child->times++;
				que[tail++] = child;
			}
		}
	}
}

/*
F:\temp\16139539.54556\Main.cc:53: error: stray '\241' in program
F:\temp\16139539.54556\Main.cc:53: error: stray '\356' in program
F:\temp\16139539.54556\Main.cc: In function 'int BFS()':
F:\temp\16139539.54556\Main.cc:55: error: expected primary-expression before 'while'
F:\temp\16139539.54556\Main.cc:55: error: expected ';' before 'while'
F:\temp\16139539.54556\Main.cc:77: error: expected '}' at end of input
*/

Double click to view unformatted code.


Back to problem 1001