图1 列出连通集
用邻接矩阵存图。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 15;
int n, m;
int list[N][N];
bool st[N];
void dfs(int u)
{
st[u] = true;
printf("%d ", u);
for (int i = 0; i < n; i ++)
if (!st[i] && list[u][i])
dfs(i);
}
void bfs(int u)
{
queue<int> q;
q.push(u);
st[u] = true;
while (q.size())
{
int p = q.front();
q.pop();
printf("%d ", p);
for (int i = 0; i < n; i ++)
{
if (!st[i] && list[p][i])
{
st[i] = true;
q.push(i);
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i ++)
{
int a, b;
cin >> a >> b;
list[a][b] = list[b][a] = 1;
}
for (int i = 0; i < n; i ++)
{
if (!st[i])
{
printf("{ ");
dfs(i);
printf("}\n");
}
}
memset(st, false, sizeof st);
for (int i = 0; i < n; i ++)
{
if (!st[i])
{
printf("{ ");
bfs(i);
printf("}\n");
}
}
return 0;
}
图2 Saving James Bond - Easy Version
构造一个新结构,存放坐标、是否能直接上岸、是否已经遍历过、是否能第一步跳上的信息。 然后将所有鳄鱼信息存放到数组中,依次从每个能第一步跳上的鳄鱼开始作 BFS 或 DFS 。
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
const int N = 110;
int n, d;
bool res;
struct Node
{
int x, y;
bool safe;
bool st;
bool first;
} node[N];
double get_d(int x1, int y1, int x2, int y2)
{
return sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));
}
void dfs(int u)
{
if (node[u].safe)
{
res = true;
return;
}
node[u].st = true;
for (int i = 0; i < n; i ++)
if (!node[i].st && (get_d(node[i].x, node[i].y, node[u].x, node[u].y) <= d))
dfs(i);
}
void bfs(int u)
{
queue<int> q;
q.push(u);
node[u].st = true;
while (q.size())
{
int p = q.front();
q.pop();
if (node[p].safe)
{
res = true;
return;
}
for (int i = 0; i < n; i ++)
if (get_d(node[p].x, node[p].y, node[i].x, node[i].y) <= d && !node[i].st)
{
node[i].st = true;
q.push(i);
}
}
}
int main()
{
cin >> n >> d;
for (int i = 0; i < n; i ++)
{
int x, y;
cin >> x >> y;
node[i].x = x, node[i].y = y;
if (abs(x - 50) <= d || abs(x + 50) <= d || abs(y + 50) <= d || abs(y - 50) <= d)
node[i].safe = true;
else node[i].safe = false;
if (get_d(x, y, 0, 0) <= d + (double)15 / 2) node[i].first = true;
else node[i].first = false;
node[i].st = false;
}
for (int i = 0; i < n; i ++)
{
if (node[i].first && !node[i].st)
{
dfs(i);
}
}
if (res) puts("Yes");
else puts("No");
return 0;
}
图3 六度空间
构建邻接表,然后对每个节点做一次 BFS 操作,返回距离小于等于 6 的节点个数。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1010;
typedef struct Node *List;
struct Node
{
int data;
List next;
};
List g[N];
bool st[N];
int n, m;
int bfs(int u)
{
queue<int> q;
q.push(u);
st[u] = true;
int res = 1, level = 0, last = u, tail = u;
while (q.size())
{
int p = q.front();
q.pop();
List cur = g[p]->next;
while (cur)
{
if (!st[cur->data])
{
q.push(cur->data);
st[cur->data] = true;
res ++;
tail = cur->data;
}
cur = cur->next;
}
if (p == last)
{
last = tail;
level ++;
}
if (level == 6) break;
}
return res;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
g[i] = (List)malloc(sizeof (struct Node));
g[i]->data = i;
g[i]->next = NULL;
}
for (int i = 0; i < m; i ++)
{
int x, y;
cin >> x >> y;
List cur = (List)malloc(sizeof (struct Node));
cur->data = y;
cur->next = g[x]->next;
g[x]->next = cur;
cur = (List)malloc(sizeof (struct Node));
cur->data = x;
cur->next = g[y]->next;
g[y]->next = cur;
}
for (int i = 1; i <= n; i ++)
{
memset(st, false, sizeof st);
int cnt = bfs(i);
printf("%d: %.2f%%\n", i, (double)(100.0 * cnt / n));
}
return 0;
}
|