#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int MINLENGTH;
typedef struct Link {
int x, y;
struct Link* next;
}LK,*L;
int times = 0;
int row, col;
L getLNode() {
L LHead = new LK;
LHead->x = LHead->y = 0;
LHead->next = NULL;
return LHead;
}
bool IN(L LHead, L LNode) {
L Lptr = LHead;
while (Lptr && Lptr->next) {
if ((Lptr->next->x == LNode->x) && (Lptr->next->y == LNode->y)) {
return true;
}
Lptr = Lptr->next;
}
return false;
}
bool ADDLNode(L LHead,L LNode) {
L NewNode = getLNode();
NewNode->x = LNode->x;
NewNode->y = LNode->y;
NewNode->next = LHead->next;
LHead->next = NewNode;
return true;
}
int** getMap() {
scanf("%d%d", &row, &col);
MINLENGTH = row * col;
int** arr = new int* [row];
for (int i = 0; i < row; i++)
arr[i] = new int[col];
int sx, sy, ex, ey;
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
scanf("%2d", &arr[i][j]);
return arr;
}
int showMap(int** arr, int row, int col) {
printf("\n");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
printf("%2d ", arr[i][j]);
printf("\n");
}
return 0;
}
void showLink(L LHead) {
while (LHead && LHead->next) {
printf("(%d %d)", LHead->next->x, LHead->next->y);
LHead = LHead->next;
}
//printf("NULL\n");
}
void detect(int sx, int sy, int ex, int ey, int** arr,L PaPaNode) {
if (sx == ex && sy == ey) {
添加节点到全局变量中
/*
printf("i Found!!! times:%d\n",times++);*/
printf("(%d %d)", sx, sy);
showLink(PaPaNode);
//if(PaPaNode)
// printf("\nlength(%d)\n", PaPaNode->x+arr[sx,sy]);
//else
// printf("\nlength(%d)\n", 0);
MINLENGTH = PaPaNode->x + 1;
//printf("%d %d %d %d\n", sx, sy, ex, ey);
}
else {
if (sx == row || sx == -1 || sy == col || sy == -1 || arr[sx][sy] == -1)
return;
if (PaPaNode && PaPaNode->x == MINLENGTH||PaPaNode&&(MINLENGTH-PaPaNode->x<abs(sx-ex)+abs(sy-ey)))
return;
L NewNode = getLNode();
NewNode->x = sx;
NewNode->y = sy;
if (IN(PaPaNode, NewNode)) {
delete NewNode;
return;
}
L LHead = getLNode();
if (PaPaNode)
LHead->x = PaPaNode->x + arr[sx][sy];
else
LHead->x = 1;
L Lptr = LHead;
while (PaPaNode && PaPaNode->next) {
Lptr->next = getLNode();
Lptr->next->x = PaPaNode->next->x;
Lptr->next->y = PaPaNode->next->y;
Lptr = Lptr->next;
PaPaNode = PaPaNode->next;
}
ADDLNode(LHead, NewNode);
if (LHead && LHead->next) {
if (LHead->next->next)
{
if (!(LHead->next->x == sx + 1 && LHead->next->y == sy))
detect(sx + 1, sy, ex, ey, arr, LHead);
if (!(LHead->next->x == sx - 1 && LHead->next->y == sy))
detect(sx - 1, sy, ex, ey, arr, LHead);
if (!(LHead->next->x == sx && LHead->next->y == sy + 1))
detect(sx, sy + 1, ex, ey, arr, LHead);
if (!(LHead->next->x == sx && LHead->next->y == sy - 1))
detect(sx, sy - 1, ex, ey, arr, LHead);
}
else {
detect(sx + 1, sy, ex, ey, arr, LHead);
detect(sx - 1, sy, ex, ey, arr, LHead);
detect(sx, sy + 1, ex, ey, arr, LHead);
detect(sx, sy - 1, ex, ey, arr, LHead);
}
}
}
}
int main(void) {
int** arr = getMap();
showMap(arr, 10, 10);
detect(3, 6, 8, 8, arr, NULL);
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交