Attacking rooks |
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB |
Total submit users: 15, Accepted users: 11 |
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 |
这题跟放机器人的那题很类似 主要是这题给的N大了些 在控制数组大小的时候出现问题 mp数组
要么MLE 要么RE 后来用vector写 然后就过了。。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define INF 0x7fffffff
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOV(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REV(i,a,b) for(int i=a-1;i>=b;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
int n;
char ch[110][110];
int vis[10005];
int link[10005];
//int mp[55*110][55*110];
vector<int> mp[10005];
int nx,ny;
int x[110][110],y[110][110];
int ans;
bool dfs(int u)
{
// for(int i=1;i<=ny;i++)
// {
// if(mp[u][i]&&!vis[i])
// {
// vis[i]=1;
// if(link[i]==-1||dfs(link[i]))
// {
// link[i]=u;
// return 1;
// }
// }
// }
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
void hungary()
{
MEM(link,-1);
for(int i=1;i<=nx;i++)
{
MEM(vis,0);
if(dfs(i))
ans++;
}
}
int main()
{
//freopen("ceshi.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
getchar();
for(int i=0;i<n;i++)
scanf("%s",ch[i]);
int num=0;
MEM(x,0); MEM(y,0);
for(int i=0;i<n;i++)
{
int flag=0;
for(int j=0;j<n;j++)
{
if(ch[i][j]=='.')
{
if(flag==0) num++;
x[i][j]=num; flag=1;
}
if(ch[i][j]=='X')
flag=0;
}
}
nx=num; num=0;
for(int i=0;i<n;i++)
{
int flag=0;
for(int j=0;j<n;j++)
{
if(ch[j][i]=='.')
{
if(!flag) num++;
y[j][i]=num; flag=1;
}
if(ch[j][i]=='X')
flag=0;
}
}
ny=num;
MEM(mp,0);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(x[i][j])
mp[x[i][j]].push_back(y[i][j]);
// mp[x[i][j]][y[i][j]]=1;
}
}
ans=0;
hungary();
printf("%d\n",ans);
}
return 0;
}