本文共 3234 字,大约阅读时间需要 10 分钟。
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 15X....X......X...X.......XSample input 24.....X..........Sample input 31X |
Sample Output |
Sample output 17Sample output 25Sample output 30 |
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); }} |
转载于:https://www.cnblogs.com/mengfanrong/p/5195267.html