洛谷 P2307 迷宫

题目传送门:https://www.luogu.com.cn/problem/P2307

题目思路:

1. 判断是否有环,如果有环,则不符合条件

2. 如果没有环,判断图是否是连通图,

3. 如果不是连通图,则不符合条件,否则符合条件。

总结:

1. 符合条件的情况:无环且为连通图。

2. 不符合条件的情况:有环或者不是连通图。

判断方式:

1. 判断是否存在环使用并查集。2. 判断是不是连通图也使用并查集。

判断是否存在环:

当向并查集中加入边时,如果边的两端点U和V的祖先已经相同,说明存在环。

判断是否为连通图:

如果存在超过2个点,他们的祖先都是自己,则不是连通图。

#include<iostream>
#include<vector>
#include<set>
using namespace std;
const int MAXN = 100005;

struct Edge{
	int u,v;
	Edge(int u,int v){
		this->u = u;
		this->v = v; 
	}	
};
vector<Edge> edges;
bool vis[MAXN];

int father[MAXN];
void initFather(int n){
	for(int i=0;i<=n;i++){
		father[i] = i;
	}
}

int findFather(int x){
	int z= x;
	while(x != father[x]){
		x = father[x];
	}
	int temp;
	while(z != father[z]){
		temp = z;
		z = father[z];
		father[temp] = x;
	}
	return x;
}

bool unionFather(int x,int y){
	int fx = findFather(x);
	int fy = findFather(y);
	if(fx != fy){
		father[fy] = fx;
		return false;
	}
	return true;
}

int judge(int n){
	initFather(n);
	//判断是否有环 
	for(int i=0;i<edges.size();i++){
		Edge edge = edges[i];
		if(unionFather(edge.u,edge.v)){  
			return 0;  //存在环 
		}
	}
	int flag = 1;
	int cnt = 0;
	//判断是否连通 
	for(int i=1;i<=n;i++){
		if(vis[i] && father[i] == i){
			cnt++;
			if(cnt == 2){  //图不连通 
				flag = 0;
				break;
			}
		}
	}
	return flag;
}

int main(){
	int u,v;
	int maxNum = -1;
	while(cin>>u>>v){
		if(u == -1 && v == -1){
			break;
		}
		if(u == 0 && v ==0 ){
			cout<<judge(maxNum)<<endl;
			edges.clear();
			maxNum = -1;
		}else{
			maxNum = max(max(u,v),maxNum);
			edges.push_back(Edge(u,v));	
			vis[u] = vis[v] = true;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷p1238是一个题目,具体要求是给定一个迷宫,求从起点到终点的最短路径。这个问题可以使用链表来表示迷宫,并使用广度优先搜索算法来求解最短路径。 以下是一个示例代码,演示了如何使用链表和广度优先搜索算法来解决洛谷p1238题目中的迷宫问题: ```python from collections import deque # 定义迷宫的链表节点类 class Node: def __init__(self, x, y, val): self.x = x self.y = y self.val = val self.next = None # 构建迷宫的链表 def build_maze(maze): m = len(maze) n = len(maze[0]) head = Node(0, 0, maze[0][0]) curr = head for i in range(m): for j in range(n): if i == 0 and j == 0: continue node = Node(i, j, maze[i][j]) curr.next = node curr = node return head # 广度优先搜索算法求解最短路径 def bfs(maze): m = len(maze) n = len(maze[0]) visited = [[False] * n for _ in range(m)] queue = deque([(0, 0, 0)]) # (x, y, step) visited[0][0] = True while queue: x, y, step = queue.popleft() if x == m - 1 and y == n - 1: return step for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: nx, ny = x + dx, y + dy if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny] and maze[nx][ny] == 0: queue.append((nx, ny, step + 1)) visited[nx][ny] = True return -1 # 示例迷宫 maze = [ [0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 1, 0] ] # 构建迷宫的链表 maze_head = build_maze(maze) # 使用广度优先搜索算法求解最短路径 shortest_path = bfs(maze) print("最短路径长度为:", shortest_path) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值