uvalive 6525——Attacking rooks(二分图匹配,好题!)

22 篇文章 0 订阅

Chess inspired problems are a common source of exercises in algorithms classes. Starting with the wellknown 8-queens problem, several generalizations and variations were made. One of them is the N-rooksproblem, which consists of placing N rooks in an N by N chessboard in such a way that they do notattack each other.

Professor Anand presented the N-rooks problem to his students. Since rooks only attack each otherwhen they share a row or column, they soon discovered that the problem can be easily solved by placingthe rooks along a main diagonal of the board. So, the professor decided to complicate the problem byadding some pawns to the board. In a board with pawns, two rooks attack each other if and only ifthey share a row or column and there is no pawn placed between them. Besides, pawns occupy somesquares, 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 maximumnumber of rooks that can be placed on empty squares such that no two of them attack each other.

Input

The input file contains several test cases, each of them as described below.
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 characteris either ‘.’ (dot) or the uppercase letter ‘X’, indicating respectively an empty square or a squarecontaining a pawn.

Output

For each test case, output a line with an integer representing the maximum number of rooks that canbe placed on the empty squares of the board without attacking each other.

Sample Input

5
X....
X....
..X..
.X...
....X
4
....
.X..
....
....
1
X

Sample Output

750 



插个好的题解好了,建图非常奇妙http://blog.csdn.net/hitwhacmer1/article/details/46731219


代码如下,我用网络流写得。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXV=101000;
const int INF=1000000007;
struct edge{int to,cap,rev,is_reverse;};
vector <edge> G[MAXV];
int level[MAXV],iter[MAXV];
void add_edge(int from,int to,int cap){
    G[from].push_back((edge){to,cap,(int)G[to].size(),0});
    G[to].push_back((edge){from,0,(int)G[from].size()-1,1});
}

void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f){
    if(v==t)
        return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    for(;;){
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}
int n;
char a[110][110];
int cnt=1;
int x[110][110],y[110][110];
const int s=0,t=100000;
void solve(){
    for(int i=0;i<MAXV;i++){
        G[i].clear();
    }
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    cnt=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            x[i][j]=cnt;
            if(a[i][j]=='X'||j==n-1){
                cnt++;
            }
        }
    }
    int temp=cnt;
    for(int i=1;i<cnt;i++){
        add_edge(s,i,1);
    }
    for(int j=0;j<n;j++){
        for(int i=0;i<n;i++){
            y[i][j]=cnt;
            if(a[i][j]=='X'||i==n-1){
                cnt++;
            }
        }
    }
    for(int i=temp;i<cnt;i++){
        add_edge(i,t,1);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(a[i][j]=='.'){
                add_edge(x[i][j], y[i][j], 1);
            }
        }
    }
    printf("%d\n",max_flow(s, t));
}

int main(int argc, char *argv[]) {
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%s",a[i]);
        }
        solve();
    }
}



### VRRP 攻击数据包的处理方案 为了有效应对VRRP协议可能遭遇的安全威胁,特别是针对攻击数据包的情况,采取一系列措施来增强系统的安全性至关重要。 #### 1. 数据来源验证 确保仅接受来自合法设备的数据包。这可以通过设置严格的ACL(访问控制列表),只允许特定网段内的主机向VRRP组发送消息[^4]。此外,在支持的情况下启用双向认证机制,进一步确认通信双方的身份合法性。 #### 2. 数据加密传输 对于敏感环境下的应用,考虑使用IPsec或其他安全隧道技术对整个VRRP流量进行封装保护,使得即使在网络层面上截获了这些报文也无法轻易解析其中的内容。 #### 3. 数据完整性校验 利用哈希算法计算并附加于每条发出的消息之后,接收端再次执行相同运算并与附带值对比,以此判断信息是否遭到中途篡改。任何发现异常的情形都将立即丢弃相应报文,并记录日志以便后续分析调查。 #### 4. 抗重放攻击策略 实施序列号检查逻辑,即每次成功接收到一个新的通告后更新本地存储的最大已知序号;一旦遇到小于等于此数值的新到来者则视为非法尝试而予以屏蔽。同时配合较短的有效期设定,超出时限范围外的一切重复提交均不予理会。 ```bash # 示例:配置抗重放功能 (具体命令取决于实际使用的操作系统/平台) set protocols vrrp group <group-name> authentication-type simple-password "secret" set protocols vrrp group <group-name> accept-data-mode strict ``` 以上方法综合运用可显著提升VRRP部署场景中的网络安全防护水平,减少因外部干扰因素造成的潜在风险隐患。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值