C++ 链表实现八方向遍历 - 解决死循环问题
#include
using namespace std;
struct node { int x, y; node *next; };
bool include(node n, int x, int y) { node *p = &n; while (p != nullptr) { if (p->x == x && p->y == y) return true; p = p->next; } return false; }
bool validPosition(int n, int m, node pos, int adjX, int adjY) { return pos.x + adjX >= 0 && pos.x + adjX < n && pos.y + adjY >= 0 && pos.y + adjY < m; }
void appendNode(node *&front, int adjX, int adjY) { node *temp = new node; temp->x = front->x + adjX; temp->y = front->y + adjY; temp->next = nullptr; front->next = temp; front = temp; }
int num(int n, int m, int x1, int y1, int x2, int y2) { int ans = 0; node temp = {x1, y1}, *head = &temp, *front, *x, *old; old = front = head; do { cout << front->x << " " << front->y << endl; if (front->x == x2 && front->y == y2)ans++; else { if (validPosition(n, m, *front, 1, 0) && !include(*old, front->x + 1, front->y + 0)) appendNode(front, 1, 0); if (validPosition(n, m, *front, 1, 1) && !include(*old, front->x + 1, front->y + 1)) appendNode(front, 1, 1); if (validPosition(n, m, *front, 0, 1) && !include(*old, front->x + 0, front->y + 1)) appendNode(front, 0, 1); if (validPosition(n, m, *front, -1, 1) && !include(*old, front->x - 1, front->y + 1)) appendNode(front, -1, 1); if (validPosition(n, m, *front, -1, 0) && !include(*old, front->x - 1, front->y + 0)) appendNode(front, -1, 0); if (validPosition(n, m, *front, -1, -1) && !include(*old, front->x + 1, front->y - 1)) appendNode(front, -1, -1); if (validPosition(n, m, *front, 0, -1) && !include(*old, front->x + 0, front->y - 1)) appendNode(front, 0, -1); if (validPosition(n, m, *front, 1, -1) && !include(*old, front->x + 1, front->y - 1)) appendNode(front, 1, -1); } x = head; while (x->next->next)x = x->next; front = x; } while (front != head); return ans; }
int main() { cout << num(4, 4, 1, 1, 3, 3); return 0; }
中文
一直死循环在0,3是为什么
内容:这个问题是由于循环中的条件判断有误导致的。在循环中,条件判断应该是while (front != head)而不是while (head != front),因为你想要循环直到front指向head节点,而不是head指向front节点。修正后的代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
struct node {
int x, y;
node *next;
};
bool include(node n, int x, int y) {
node *p = &n;
while (p != nullptr) {
if (p->x == x && p->y == y) return true;
p = p->next;
}
return false;
}
bool validPosition(int n, int m, node pos, int adjX, int adjY) {
return pos.x + adjX >= 0 && pos.x + adjX < n && pos.y + adjY >= 0 && pos.y + adjY < m;
}
void appendNode(node *&front, int adjX, int adjY) {
node *temp = new node;
temp->x = front->x + adjX;
temp->y = front->y + adjY;
temp->next = nullptr;
front->next = temp;
front = temp;
}
int num(int n, int m, int x1, int y1, int x2, int y2) {
int ans = 0;
node temp = {x1, y1}, *head = &temp, *front, *x, *old;
old = front = head;
do {
cout << front->x << " " << front->y << endl;
if (front->x == x2 && front->y == y2)ans++;
else {
if (validPosition(n, m, *front, 1, 0) && !include(*old, front->x + 1, front->y + 0))
appendNode(front, 1, 0);
if (validPosition(n, m, *front, 1, 1) && !include(*old, front->x + 1, front->y + 1))
appendNode(front, 1, 1);
if (validPosition(n, m, *front, 0, 1) && !include(*old, front->x + 0, front->y + 1))
appendNode(front, 0, 1);
if (validPosition(n, m, *front, -1, 1) && !include(*old, front->x - 1, front->y + 1))
appendNode(front, -1, 1);
if (validPosition(n, m, *front, -1, 0) && !include(*old, front->x - 1, front->y + 0))
appendNode(front, -1, 0);
if (validPosition(n, m, *front, -1, -1) && !include(*old, front->x + 1, front->y - 1))
appendNode(front, -1, -1);
if (validPosition(n, m, *front, 0, -1) && !include(*old, front->x + 0, front->y - 1))
appendNode(front, 0, -1);
if (validPosition(n, m, *front, 1, -1) && !include(*old, front->x + 1, front->y - 1))
appendNode(front, 1, -1);
}
x = head;
while (x->next->next)x = x->next;
front = x;
} while (front != head);
return ans;
}
int main() {
cout << num(4, 4, 1, 1, 3, 3);
return 0;
}
这样修正后的代码会正确地输出结果。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/poFq 著作权归作者所有。请勿转载和采集!