设为首页 加入收藏

TOP

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)
2015-07-20 17:33:50 来源: 作者: 【 】 浏览:2
Tags:HNU13028Attacking rooks 二分 匹配 一行 多列
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
      
       
#include
       
         #include
        
          #include
         
           using namespace std; vector
          
           map[10005]; int match[10005],vist[10005]; int find(int x) { int len=map[x].size(); for(int i=0;i
           
            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); } } 
           
          
         
        
       
      

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HYSBZ 2243 染色 (树链剖分) 下一篇HDU 3172 Virtual Friends(带权并..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)