哈密顿回路 Java题解 (图,模拟)【PAT甲级1122】

58 篇文章 3 订阅
32 篇文章 1 订阅

输入样例:

6 10
6 2
3 4
1 5
2 5
3 1
4 1
1 6
6 3
1 2
4 5
6
7 5 1 4 3 6 2 5
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 3 4 5 2 6
7 6 1 2 5 4 3 1

输出样例:

YES
NO
NO
NO
YES
NO

解题思路:

满足哈密顿回路的条件是:相邻两点都有边相连,每个点只能遍历一次,图中所有的点都遍历过。

Java代码:

import java.io.*;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		
		int n = Integer.parseInt(split[0]);
		int m = Integer.parseInt(split[1]);
		
		boolean [][]g = new boolean[n + 1][n + 1];
		boolean []vis = new boolean[n + 1];
		
		while(m-- > 0) {
			split = br.readLine().split(" ");
			int a = Integer.parseInt(split[0]);
			int b = Integer.parseInt(split[1]);
			g[a][b] = g[b][a] = true;
		}
		
		m = Integer.parseInt(br.readLine());
		StringBuilder ans = new StringBuilder();
		while(m-- > 0) {
			Arrays.fill(vis, false);
			split = br.readLine().split(" ");
			int []arr = new int[split.length]; 
			for(int i = 1; i < arr.length; i++) 
				arr[i] = Integer.parseInt(split[i]);
			
			int i = 0, cnt = n; // 判断是否遍历到了正好n个点
			for(i = 1; i < arr.length - 1; i++) {
				if(g[arr[i]][arr[i + 1]] && !vis[arr[i + 1]]) { // 图中有路径并且没有被访问过
					vis[arr[i + 1]] = true;
					cnt--;
				}else break;
			}
			if(i == arr.length - 1 && cnt == 0) ans.append("YES\n");
			else ans.append("NO\n");
		}
		System.out.print(ans);
	}
}

哈密顿回路算法(Hamiltonian cycle algorithm)是解决哈密顿回路问题的一种算法。该问题是指在给定的无向中是否存在一条简单回路,经过每个顶点恰好一次。以下是一个简单的Java实现: ```java public class HamiltonianCycle { private int numOfVertexes; private int[] hamiltonianPath; private int[][] adjacencyMatrix; public HamiltonianCycle(int[][] adjacencyMatrix) { this.adjacencyMatrix = adjacencyMatrix; this.numOfVertexes = adjacencyMatrix.length; this.hamiltonianPath = new int[numOfVertexes]; this.hamiltonianPath[0] = 0; } public void solve() { if (findFeasibleSolution(1)) { printHamiltonianPath(); } else { System.out.println("No feasible solution found"); } } private boolean findFeasibleSolution(int position) { if (position == numOfVertexes) { if (adjacencyMatrix[hamiltonianPath[position - 1]][hamiltonianPath[0]] == 1) { return true; } else { return false; } } for (int vertexIndex = 1; vertexIndex < numOfVertexes; ++vertexIndex) { if (isFeasible(vertexIndex, position)) { hamiltonianPath[position] = vertexIndex; if (findFeasibleSolution(position + 1)) { return true; } //backtrack } } return false; } private boolean isFeasible(int vertexIndex, int actualPosition) { // first criterion: whether the two nodes are connected? if (adjacencyMatrix[hamiltonianPath[actualPosition - 1]][vertexIndex] == 0) { return false; } // second criterion: whether we have already added this given node? for (int i = 0; i < actualPosition; ++i) { if (hamiltonianPath[i] == vertexIndex) { return false; } } return true; } private void printHamiltonianPath() { System.out.print("Hamiltonian cycle: "); for (int i = 0; i < numOfVertexes; ++i) { System.out.print(hamiltonianPath[i] + " "); } System.out.println(hamiltonianPath[0]); } } ``` 这个实现中,我们使用邻接矩阵来表示,并且在每一步中使用回溯算法来寻找哈密顿回路
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值