图-> DFS

图论模型  (DFS )

用DFS求连通块

****@

*@@*@

*@**@

@@@*@

@@**@

// 题意:输入一个字符矩阵,统计字符@组成多少个四连块
#include<cstdio>
#include<cstring>
const int maxn = 100 + 5;

char pic[maxn][maxn];
int m, n, idx[maxn][maxn];

void dfs(int r, int c, int id) {
  if(r < 0 || r >= m || c < 0 || c >= n) return;//格子出界
  if(idx[r][c] > 0 || pic[r][c] != '@') return;//访问过或超界
  idx[r][c] = id;
  for(int dr = -1; dr <= 1; dr++)
    for(int dc = -1; dc <= 1; dc++)
      if(dr != 0 || dc != 0) dfs(r+dr, c+dc, id);//&&是才是在本位置
}

int main() {
  while(scanf("%d%d", &m, &n) == 2 && m && n) {
    for(int i = 0; i < m; i++) scanf("%s", pic[i]);
    memset(idx, 0, sizeof(idx));
    int cnt = 0;
    for(int i = 0; i < m; i++)
      for(int j = 0; j < n; j++)
        if(idx[i][j] == 0 && pic[i][j] == '@') dfs(i, j, ++cnt);
    printf("%d\n", cnt);
  }
  return 0;
}




先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
#include<stdio.h> #include<stdlib.h> #define MAX 10 typedef int vertex; typedef int weighttype; typedef struct gnode *ptrtognode; struct gnode { int Nv; int Ne; weighttype weight[MAX][MAX]; int visit[MAX]; }; typedef ptrtognode Mgraph; typedef struct edge *ptrtoedge; struct edge { vertex v1, v2; weighttype weight; }; typedef ptrtoedge Edge; typedef struct queue *ptrtoqueue; struct queue { vertex que[MAX]; int front; int last; }; typedef ptrtoqueue Queue; Mgraph createmgraph(int nv); void insertedge(Mgraph M, Edge e); void buildmgraph(Mgraph M, int ne); void DFS(Mgraph M,vertex v); void listDFS(Mgraph M); void reset(Mgraph M); Queue createqueue(); void inqueue(vertex v,Queue q); int isempty(Queue q); vertex outqueue(Queue q); void BFS(Mgraph M,vertex v); void listBFS(Mgraph M); int main() { int nv; scanf("%d", &nv); Mgraph M = createmgraph(nv); scanf("%d", &M->Ne); buildmgraph(M, M->Ne); listDFS(M); reset(M); listBFS(M); return 0; } Mgraph createmgraph(int nv) { Mgraph M; M = (Mgraph)malloc(sizeof(struct gnode)); M->Nv = nv; M->Ne = 0; int i, j; for (i = 0; i < nv; i++) for (j = 0; j < nv; j++) M->weight[i][j] = 0; for (i = 0; i < nv; i++) M->visit[i] = 0; return M; } void insertedge(Mgraph M, Edge e) { M->weight[e->v1][e->v2] = e->weight; M->weight[e->v2][e->v1] = e->weight; return; } void buildmgraph(Mgraph M, int ne) { int i; Edge e; e = (Edge)malloc(sizeof(struct edge)); for (i = 0; i < ne; i++) { scanf("%d %d", &e->v1, &e->v2); e->weight = 1; insertedge(M, e); } return; } void DFS(Mgraph M,vertex v) { M->visit[v] = 1; printf("%d ", v); int i; for (i = 0; i < M->Nv; i++) { if (M->visit[i] == 0 && M->weight[v][i] == 1)//如果v的临界点没被访问过 DFS(M, i); } return; } void listDFS(Mgraph M) { int i; for (i = 0; i < M->Nv; i++) { if (M->visit[i] == 0) { DFS(M, i); printf("\n"); } } return; } void reset(Mgraph M) { int i; for (i = 0; i <M->Nv; i++) M->visit[i] = 0; return; } Queue createqueue() { Queue q; q = (Queue)malloc(sizeof(struct queue)); q->front = 0; q->last = 0; int i; for (i = 0; i < MAX; i++) q->que[i] = -1; return q; } void inqueue(vertex v, Queue q) { if (q->last == MAX - 1)return; q->que[q->last++] = v; return; } int isempty(Queue q) { if (q->front == q->last) return 1; else return 0; } vertex outqueue(Queue q) { if (isempty(q) == 1)return -1; vertex v = q->que[q->front++]; return v; } void BFS(Mgraph M, vertex v) { Queue q; vertex tep; q = createqueue(); inqueue(v, q); M->visit[v] = 1; while (isempty(q) == 0) { tep = outqueue(q); printf("%d ", tep); int i; for (i = 0; i < M->Nv; i++) { if (M->visit[i] == 0 && M->weight[i][tep] == 1) { inqueue(i, q); M->visit[i] = 1; } } } } void listBFS(Mgraph M) { int i; for (i = 0; i < M->Nv; i++) { if (M->visit[i] == 0) { BFS(M, i); printf("\n"); } } return; } 这个代码能不能简洁一下
11-26
以下是简化后的在无向中使用 DFS 和 BFS 列出所有连通集的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_VERTICES 10 // 邻接表节点 typedef struct AdjListNode { int dest; struct AdjListNode* next; } AdjListNode; // 邻接表头节点 typedef struct AdjList { AdjListNode *head; } AdjList; // 结构 typedef struct Graph { int V; AdjList* array; } Graph; // 创建新的邻接表节点 AdjListNode* newAdjListNode(int dest) { AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // 创建 Graph* createGraph(int V) { Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->V = V; graph->array = (AdjList*)malloc(V * sizeof(AdjList)); for (int i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } // 添加边 void addEdge(Graph* graph, int src, int dest) { AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } // DFS 辅助函数 void DFSUtil(Graph* graph, int v, bool visited[]) { visited[v] = true; printf("%d ", v); AdjListNode* pCrawl = graph->array[v].head; while (pCrawl != NULL) { int adjVertex = pCrawl->dest; if (!visited[adjVertex]) DFSUtil(graph, adjVertex, visited); pCrawl = pCrawl->next; } } // DFS 遍历 void DFS(Graph* graph) { bool visited[MAX_VERTICES] = {false}; for (int v = 0; v < graph->V; ++v) { if (!visited[v]) { DFSUtil(graph, v, visited); printf("\n"); } } } // BFS 遍历 void BFS(Graph* graph) { bool visited[MAX_VERTICES] = {false}; int queue[MAX_VERTICES]; int front = 0, rear = 0; for (int v = 0; v < graph->V; ++v) { if (!visited[v]) { queue[rear++] = v; visited[v] = true; while (front < rear) { int s = queue[front++]; printf("%d ", s); AdjListNode* pCrawl = graph->array[s].head; while (pCrawl != NULL) { int adjVertex = pCrawl->dest; if (!visited[adjVertex]) { queue[rear++] = adjVertex; visited[adjVertex] = true; } pCrawl = pCrawl->next; } } printf("\n"); } } } int main() { int N, E; scanf("%d %d", &N, &E); Graph* graph = createGraph(N); for (int i = 0; i < E; ++i) { int u, v; scanf("%d %d", &u, &v); addEdge(graph, u, v); } // 输出 DFS 结果 DFS(graph); // 输出 BFS 结果 BFS(graph); return 0; } ``` 该代码简化了的创建、边的添加以及 DFS 和 BFS 遍历的实现。通过使用邻接表来存储,减少了不必要的结构和操作,使代码更加简洁易懂。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值