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);
}
}

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)的更多相关文章

  1. UVALive 6525 Attacking rooks 二分匹配 经典题

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...

  2. hive一行变多行及多行变一行

    hive一行变多行及多行变一行 场景 name alias zhaoqiansun abc def ghi 处理数据时需要将上表处理成为下面的形式: name alias zhaoqiansun ab ...

  3. ASP.NET_正则表达式_匹配HTML中的一行或多行

    一.匹配数字串/flash/([0-9]+).htm 二.匹配不含双引号的字符串<p class=\"w490\">([^\"]+)</p> 三. ...

  4. pandas dataframe 一行变多行 (query pv统计term pv)

    关键字: 用jieba切词 用expand 一列变多列 用stack 列转行 用group by + aggr 相同term的pv求和 上效果: query pv 今日新鲜事 今日头条 北京天气 上海 ...

  5. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  6. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  7. BZOJ 1191: [HNOI2006]超级英雄Hero 二分匹配

    1191: [HNOI2006]超级英雄Hero Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或 ...

  8. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

随机推荐

  1. Android学习笔记(一)开发环境搭建

    Android开发环境搭建 安装JDK 1.如果你还没有JDK的话,可以去这里http://www.oracle.com/technetwork/java/index.html ,接下来的工作就是安装 ...

  2. C# 查找指定名称的控件(转)

    请问我知道控件的名称如何得到这个控件对象呢? var button = this.FindName("button1") as Button; Button button = th ...

  3. [Swust OJ 403]--集合删数

    题目链接:http://acm.swust.edu.cn/problem/403/ Time limit(ms): 5000 Memory limit(kb): 65535   Description ...

  4. BZOJ 1191: [HNOI2006]超级英雄Hero(二分图匹配)

    云神说他二分图匹配从来都是用网络流水过去的...我要发扬他的精神.. 这道题明显是二分图匹配.网络流的话可以二分答案+最大流.虽然跑得很慢.... -------------------------- ...

  5. C#_socket拆包_封包_模拟乱序包

    拆包一直是个硬伤呀,MLGB的,服务端各种乱数据,果断整理下 拆包思路:设计一个网络协议,一般都会分包,一个包就相当于一个逻辑上的命令. .如果我们用udp协议,省事的多,一次会收到一个完整的包,但U ...

  6. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. python的内置函数bin()

    bin(x) 中文说明:将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法 ...

  8. 怎么给没链接的flash加超链接

    最近开始准备设计一个广告条,本想用阿里妈妈的的banner marker来设计,却遗憾的发现,banner marker已经实行收费模式了. 我不得不启用另一款在线banner生成工具,百度旗下的&q ...

  9. RFID介绍及电子标签成本预估

    What is RFID • RadioFrequencyIdentification, (無線射頻識別系統)• 通常是由感測器(Reader)和RFID標籤(Tag)所組成的系統 RFID分類 • ...

  10. Delphi函数指针的两种定义(对象方法存在一个隐藏参数self,所以不能相互赋值)

    delphi中经常见到以下两种定义 Type TMouseProc = procedure (X,Y:integer); TMouseEvent = procedure (X,Y:integer) o ...