HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)
2016-02-17 14:39
274 查看
Attacking rooks |
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB |
Total submit users: 12, Accepted users: 7 |
Problem 13028 : No special judgement |
Problem description |
Chess inspired problems are a common source of exercises in algorithms classes. Starting with the well known 8-queens problem, several generalizations and variations were made. One of them is the N-rooks problem, which consists of placing N rooks in an N by N chessboard in such a way that they do not attack each other. Professor Anand presented the N-rooks problem to his students. Since rooks only attack each other when they share a row or column, they soon discovered that the problem can be easily solved by placing the rooks along a main diagonal of the board. So, the professor decided to complicate the problem by adding some pawns to the board. In a board with pawns, two rooks attack each other if and only if they share a row or column and there is no pawn placed between them. Besides, pawns occupy some squares, which gives an additional restriction on which squares the rooks may be placed on. Given the size of the board and the location of the pawns, tell Professor Anand the maximum number of rooks that can be placed on empty squares such that no two of them attack each other. |
Input |
The first line contains an integer N (1 ≤ N ≤ 100) representing the number of rows and columns of the board. Each of the next N lines contains a string of N characters. In the i-th of these strings, the j-th character represents the square in the i-th row and j-th column of the board. The character is either "." (dot) or the uppercase letter "X", indicating respectively an empty square or a square containing a pawn. |
Output |
Output a line with an integer representing the maximum number of rooks that can be placed on the empty squares of the board without attacking each other. |
Sample Input |
Sample input 1 5 X.... X.... ..X.. .X... ....X Sample input 2 4 .... .X.. .... .... Sample input 3 1 X |
Sample Output |
Sample output 1 7 Sample output 2 5 Sample output 3 0 |
Problem Source |
ICPC Latin American Regional 2013#include<stdio.h> #include<iostream> #include<string.h> #include<vector> using namespace std; vector<int>map[10005]; int match[10005],vist[10005]; int find(int x) { int len=map[x].size(); for(int i=0;i<len;i++) if(vist[map[x][i]]==0) { vist[map[x][i]]=1; if(match[map[x][i]]==0||find(match[map[x][i]])) { match[map[x][i]]=x; return 1; } } return 0; } int main() { int n,rn,ln,t=0,mpr[105][105],mpl[105][105]; char c; while(scanf("%d",&n)>0) { for(int i=1;i<=n;i++) { getchar(); for(int j=1;j<=n;j++) { mpr[i][j]=mpl[i][j]=0; scanf("%c",&c); if(c=='X') mpr[i][j]=mpl[i][j]=-1; } } rn=0; for(int i=1;i<=n;i++)//一行变多行 for(int j=1;j<=n;j++) { while(mpr[i][j]==-1&&j<=n)j++; rn++; while(mpr[i][j]!=-1&&j<=n) { mpr[i][j]=rn; j++; } } ln=0; for(int j=1;j<=n;j++)//一列变多列 for(int i=1;i<=n;i++) { while(mpl[i][j]==-1&&i<=n)i++; ln++; while(mpl[i][j]!=-1&&i<=n) { mpl[i][j]=ln; i++; } } for(int i=1;i<=rn;i++) map[i].clear(); for(int i=1;i<=n;i++)//行列建图 for(int j=1;j<=n;j++) if(mpr[i][j]!=-1) map[mpr[i][j]].push_back(mpl[i][j]); memset(match,0,sizeof(match)); int ans=0; for(int i=1;i<=rn;i++) { for(int j=0;j<=ln;j++) vist[j]=0; ans+=find(i); } printf("%d\n",ans); } } |
相关文章推荐
- 【译】使用内容安全策略(CSP)加固应用
- 使用VIM编写Makefile时如何避免tab被扩展成4个空格
- centos源码下载地址
- Java笔试题目知识点汇总
- LeetCode Count Primes
- 离职面谈,何时谈最佳?
- 安卓位置服务locationManager使用时候遇到的坑
- gcc优化等级-O0 -O1 -O2 -Os -O3
- 深度解析Linux根文件系统的挂载过程
- 入门
- iOS之数组的排序(升序、降序及乱序)
- 以libevent网络库为引:网络通信和多线程
- 5.6中使用字符串存放时间,导致隐式转换发生的记录
- Dynamic AX 快捷键
- UIImagePickerController使用小结
- 轻松解决Win7与IE8浏览器不兼容的问题
- angularJs自定义指令
- Ubuntu 14.04 文件服务器--samba的安装和配置
- UIView的bounds和frame学习
- 互联网时代人力资源管理者的四大功能