HDU1045
来源:互联网 发布:pic单片机汇编指令 编辑:程序博客网 时间:2024/04/29 14:56
我的第一个二分匹配:
题目链接: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;}}}}}
我对二分匹配还不是很了解啊,那位大哥给我说说,或者来个网址给我留言我看看啊
谢了!!!!
- HDU1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- HDU1045
- hdu1045
- hdu1045
- hdu1045 (DFS)
- HDU1045 DFS
- hdu1045--搜索
- hdu1045【dfs】
- 贪心之hdu1045
- DFS之hdu1045
- hdu1045 二分匹配
- Ext.net在GridPanel中通过链接查看图片
- UBUNTU下安装Broadcom 802.11g 网络适配器驱动
- vb.net利用system.data.sqlite.dll实现sqlite数据库编程示例
- 将apusic添加到windows服务
- 关于(gzip: stdin: not in gzip format tar: ...)问题的解决
- HDU1045
- CSS中通过import方式导入的方法
- C为什么不对数组进行下标越界检测
- std string使用
- sql server2005 中的表导入到oracle数据库
- JSF+Spring+Hibernate框架的搭建示例图解
- 永久地址映射
- QT 如何使图片像素变小的问题
- Ganglia之RPM包制作