第八天 有向路径检查
来源:互联网 发布:云购源码 编辑:程序博客网 时间:2024/06/05 08:56
题目中数据结构下一点的指针,让我想到了广度优先的思路。
大致想法:1. 从任一个点出发,找路径,有向共两次; 2. 用队列的思想,不断把新邻居压人; 3. 注意哪些点已经访问过了,避免环以及提高效率; 4. 注意边界情况。
/*struct UndirectedGraphNode { int label; vector<struct UndirectedGraphNode *> neighbors; UndirectedGraphNode(int x) : label(x) {}};*/class Path {public: bool checkPath(UndirectedGraphNode* a, UndirectedGraphNode* b) { // write code here if (!a) return false; if (!b) return false; if (a == b) return true; // a->b bool meet[1000];// 只是随意取了一个较大的值 memset(meet, false, sizeof(bool) * 1000); queue<UndirectedGraphNode*> q; q.push(a); while (!q.empty()) { UndirectedGraphNode* temp = q.front(); q.pop(); for (int i = 0; i < temp->neighbors.size(); i ++) { if (b == temp->neighbors[i]) return true; if (!meet[temp->neighbors[i]->label]) { q.push(temp->neighbors[i]); meet[temp->neighbors[i]->label] = true; } } } // b->a memset(meet, false, sizeof(bool) * 1000); q.push(b); while (!q.empty()) { UndirectedGraphNode* temp = q.front(); q.pop(); for (int i = 0; i < temp->neighbors.size(); i ++) { if (a == temp->neighbors[i]) return true; if (!meet[temp->neighbors[i]->label]) { q.push(temp->neighbors[i]); meet[temp->neighbors[i]->label] = true; } } } return false; }};
0 0
- 第八天 有向路径检查
- 有向路径检查
- 有向路径检查
- 有向路径检查
- 有向路径检查
- 有向路径检查
- 有向路径检查
- 检查图中的有向路径
- 《程序员面试金典》有向路径检查
- 程序员面试金典题解 有向路径检查
- 程序员面试金典-4.2有向路径检查
- 程序员面试金典:二叉树平衡检查、有向路径检查
- 有向图最短路径
- 有向图 路径
- 程序员面试金典——有向路径检查(图)
- 第八天
- 第八天
- 第八天
- <LeetCode OJ> 382. Linked List Random Node
- UVA - 10820 Send a Table(欧拉筛法)
- 实现一堆栈,要求三个操作,Pop,Push,GetMaxValue,时间均为O(1)
- Activiti工作流的初步学习
- 开发时 团队出现的严重问题 放在这儿警戒
- 第八天 有向路径检查
- AsyncTask的用法
- 设计模式六大原则: 狸猫换太子 -- 里氏替换原则
- 基础UI组件-spinner
- UVA - 12230 Crossing Rivers
- 死亡历险,Java做的安卓调用.net Webservice进行修改密码
- 面试机器学习、大数据岗位时遇到的各种问题
- C++实现二位数组加减法
- Leetcode no. 96