| #pragma warning(disable:4996) #include "stdafx.h" #include <stdio.h> #include <malloc.h> const int move_x[] = { 0,0,0,1,-1 }; const int move_y[] = { 0,1,-1,0,0 }; int map[100][100],check[100][100]; struct node  { 	int x, y, from; 	struct node *next; }; struct node *top; void show(); void Go(int n, int m); void Pop(); struct node* Top(); void Push(struct node* p); int Empty(); void show() { 	int i; 	struct node *p; 	int x[1000], y[1000]; 	int len; 	len = 0; 	while (Empty() == 0) 	{ 		len++; 		p = Top(); 		x[len] = p->x; 		y[len] = p->y; 		Pop(); 	} 	printf("steps=%d\n", len); 	for (i = len; i > 0; i--) 		printf("(%d, %d)\n", x[i],y[i]); } int Empty() { 	if (top==NULL) return 1; 	return 0; } void Pop() { 	struct node *p; 	p = top; 	top = p->next; 	free(p); } struct node* Top() { 	return top; } void Push(struct node *p) { 	p->next = top; 	top = p; } void Go(int n, int m) { 	struct node *p; 	int i; 	int nx, ny, come, find; 	 	p = (struct node *)malloc(sizeof(struct node)); 	p->from = 0; 	p->x = 1; 	p->y = 1; 	Push(p); 	check[1][1] = 1; 	top->next = NULL; 	come = 0; 	while (1) 	{ 		find = 0; 		for (i = come + 1; i <= 4; i++) 		{ 			nx = top->x + move_x[i]; 			ny = top->y + move_y[i]; 			if (check[nx][ny] == 0 && map[nx][ny] == 0) 			{ 				p = (struct node *)malloc(sizeof(struct node)); 				p->x = nx; 				p->y = ny; 				p->from = i; 				Push(p); 				come = 0; 				find = 1; 				check[nx][ny] = 1; 				if (nx == n && ny == m) 				{ 					show(); 					return; 				} 				break; 			} 		} 		if (find == 0) 		{ 			p = Top(); 			nx = p->x; 			ny = p->y; 			come = p->from; 			Pop(); 		} 		find = 0; 		if (top == NULL) 		{ 			printf("There's no way out!\n"); 			return; 		} 	} } int main() { 	int n, m; 	int i, j; 	char ch; 	while (scanf("%d%d", &n, &m) && n && m) 	{ 		memset(check, 0, sizeof(check)); 		for (i = 0; i < 100; i++) 			for (j = 0; j < 100; j++) map[i][j] = 1; 		for (i = 1; i <= n; i++) 			for (j = 1; j <= m; j++) 				scanf("%d", &map[i][j]); 		if (map[1][1] == 1 || map[n][m] == 1) 		{ 			printf("There is no way out!\n"); 			continue; 		} 		Go(n, m); 	}     return 0; }
 |