直接贴代码,我自己都觉得有点丑陋,,
我建了点的类,把点放到一个数组里来表示图,剩下的就按照老师给的代码写
另,求教,我的迭代函数的参数太多了,每次使用数组的时候都要分别传数组地址和下标,有什么优化方法吗
#includeusing namespace std; //怎么样不用邻接矩阵和邻接表表示一个图 class Point { public: Point() :x(0),y(0) {}; bool first_jump(int step) { if ((step + 7.5) * (step + 7.5) >= (x * x + y * y)) return true; else return false; } bool then_jump(Point p2, int step) { if ((step* step) >= ((p2.x - x) * (p2.x - x) + (p2.y - y) * (p2.y - y))) return true; else return false; } bool is_safe(int step) { if ((50 - abs(x) <= step) || (50 - abs(y) <= step)) return true; else return false; } Point* Creat_arr(int num) { Point* arr_p = new Point[num]; for (int i = 0; i < num; i++) { cin >> arr_p[i].x >> arr_p[i].y; } return arr_p; } public: int x; int y; }; int DFS007(Point* arr_point, int num, int* visited, int step, int cur) { visited[cur] = 1; if (arr_point[cur].is_safe(step)) { return 1; } int answer = 0; for (int i = 0; i < num; i++) { if (!visited[i] && arr_point[cur].then_jump(arr_point[i], step)) { answer = DFS007(arr_point, num, visited, step , i); if (answer) { return 1; } } } if (!answer) { return 0; } } void save_007(Point* arr_point, int* visited, int num, int step) { bool answer; for (int i = 0; i < num; i++) { if (visited[i] == 0 && arr_point[i].first_jump(step)) { answer = DFS007(arr_point, num, visited, step, i); if (answer) break; } } if (answer) cout << "Yes" << endl; else cout << "No" << endl; } int main() { int num = 0, step = 0; cin >> num >> step; int* visited = new int[num]; for (int i = 0; i < num; i++) { visited[i] = 0; } Point p; Point* arr_p = p.Creat_arr(num); save_007(arr_p, visited, num, step); system("pause"); return 0; }



