HDU1045

我的第一个二分匹配:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045

 

package D0710;

//二分匹配(DFS/BFS/匈牙利算法的实现)
import java.util.Scanner;

public class HDU1045 {
	static char[][] ch;// 保存开始的输入
	static boolean used[][];// 这个格子是否被使用过
	static int row[];// 行是否修筑了碉堡(修筑为1,未修筑为0)
	static int colunm[];// 该列是否修筑了碉堡(修筑为1,未修筑为0)
	static int count;// 最后的碉堡数(结果)

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		String[] arr;

		while (sc.hasNext()) {
			count = 0;
			n = sc.nextInt();
			if (n == 0)
				break;
			arr = new String[n];
			ch = new char[n][n];
			row = new int[n];
			colunm = new int[n];
			used = new boolean[n][n];
			for (int i = 0; i < n; i++) {
				arr[i] = sc.next();
			}
			// 将输入的字符保存在一个字符数组中
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					ch[i][j] = arr[i].charAt(j);
				}
			}
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (ch[i][j] == '.') {
						//用掉当前格子,并设置该格子在此次循环中不再可用
						used[i][j] = true;
						row[i] = 1;
						colunm[j] = 1;
						
						dfs(1);
						
						//下次循环时当前行可以和列可以继续使用
						used[i][j] = false;
						row[i] = 0;
						colunm[j] = 0;
					}
				}
			}
			System.out.println(count);
		}

	}

	// 二分匹配
	private static void dfs(int cnt) {
		if (cnt > count)
			count = cnt;
		int i,j;
		for (i = 0; i <row.length; i++) {
			for (j = 0; j <row.length; j++) {
				// 当前格子没有被使用并且当前行和列没有修筑碉堡
				if (ch[i][j] == '.' && !used[i][j] && row[i] == 0 && colunm[j] == 0) {
					//用掉当前格子,并且使当前行和列在此次循环中不再可用
					used[i][j] = true;
					row[i] = 1;
					colunm[j] = 1;
					
					dfs(cnt+1);//在此格子修筑碉堡(碉堡数目+1)
					
					//让当前行和列在下次循环使可以用
					used[i][j] = false;
					row[i] = 0;
					colunm[j] = 0;
				}
				// 遇到墙时当前行和列又可以使用
				else if(ch[i][j]=='X'){
					row[i] = 0;
					colunm[j] = 0;
				}
			}
		}
	}
}



我对二分匹配还不是很了解啊,那位大哥给我说说,或者来个网址给我留言我看看啊

 

 

谢了!!!!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怎么演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值