棋盘覆盖_CH6801_ACWING_372

题目:
给定一个N行N列的棋盘,已知某些格子禁止放置。

求最多能往棋盘上放多少块的长度为2、宽度为1的骨牌,骨牌的边界与格线重合(骨牌占用两个格子),并且任意两张骨牌都不重叠。

输入格式
第一行包含两个整数N和t,其中t为禁止放置的格子的数量。

接下来t行每行包含两个整数x和y,表示位于第x行第y列的格子禁止放置,行列数从1开始。

输出格式
输出一个整数,表示结果。

数据范围
1≤N≤100
输出样例:
8 0
输出样例:
32

解题思路:
这道题目中 让我们在一个棋盘中放置1*2的长方形,并且规定棋盘中有一些位置不能放,这道题可以用状压DP来做,也可以用二分图求最大匹配来做,此处我是用的是二分图求最大匹配来做。
注意点:
1.我们在给2个节点添加边的时候如果我们的maxn定义为105的话,我们的边的数组的大小起码需要开到 4 * maxn * maxn
2.我们在给节点添加边的时候,添加的是一条边而不是2条边,即有向边而不是无向边;
3.在创建边 ,即创图的过程中最好别用BFS去创建图,因为整个图可能并不是一个连通图,可能有多个连通块;
3.我们门再写匈牙利算法的时候 ,我们的match需要写2个,因为2边的点是等价的。、

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 105;

int n, t, ans;
bool vis[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool st[maxn * maxn];
int match[maxn * maxn];
int h[maxn * maxn], e[maxn * maxn * 4], ne[maxn * maxn * 4], idx;

inline void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

inline bool dfs(int u) {
	for(int i = h[u]; i != -1; i = ne[i]) {
		int j = e[i];
		if(!st[j]) {
			st[j] = true;
			if(!match[j] || dfs(match[j])) {
				match[j] = u;
				match[u] = j;
				return true;
			}
		}
	}
	
	return false;
}

inline void build(void) {
	
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= n; j ++) {
			if(!vis[i][j]) {
				int u = (i - 1) * n + j;
				
				for(int k = 0; k < 4; k ++) {
					int x = i + dx[k];
					int y = j + dy[k];
					
					if(x >= 1 && x <= n && y >= 1 && y <= n && !vis[x][y]) {
						int v = (x - 1) * n + y;
						add(u, v);
					}
				}	
			}	
		}
	}	
}

int main(void) {
//	freopen("in.txt", "r", stdin);
	
	scanf("%d%d", &n, &t);
	memset(h, -1, sizeof h);
	while(t --) {
		int x, y;
		scanf("%d%d", &x, &y);
		vis[x][y] = true;
	}
	
	build();
	
	for(int i = 1; i <= n * n; i ++) {
		if(!match[i]) {
			if(dfs(i)) ans ++;
			memset(st, false, sizeof st);
		}
	}
	
	printf("%d\n", ans);
	
//	fclose(stdin);
	return 0;
} 

写法2:
思路:通过每一个点最多只可能有4个子节点并且,这4个子节点刚好在该点的上,下, 左, 右四个方向,故可以直接得到二分图中一边的点集。
与上面写法不同之处:不用每次去匹配2次。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef pair<int, int> PII;

const int maxn = 110;
int n, t, ans;
bool vis[maxn][maxn];
bool st[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int match[maxn * maxn];
PII tmp[maxn][maxn];

inline bool find(int u) {
    int x = u / n + 1 , y = u % n;
    if(y == 0) x -= 1, y = n;
	for(int i = 0; i < 4; i ++) {
		int tx = x + dx[i];
		int ty = y + dy[i];
		
		int v = (tx - 1) * n + ty;
		
		if(tx >= 1 && tx <= n && ty >= 1 && ty <= n && !vis[tx][ty]) {
			if(!st[tx][ty]) {
				st[tx][ty] = true;
				
				if(!match[v] || find(match[v])) {
					match[v] = u;
					return true;
				}
			}
		}
	}
	
	return false;
} 


inline bool find2(PII x) {
	int xx = x.first, yy = x.second;
	
	for(int i = 0; i < 4; i ++){
		int tx = xx + dx[i];
		int ty = yy + dy[i];
		
		if(tx >= 1 && tx <= n && ty >= 1 && ty <= n && !vis[tx][ty]) {
			if(!st[tx][ty]) {
				st[tx][ty] = true;
				int p = tmp[tx][ty].first;
				
				if(!p || find2(tmp[tx][ty])) {
					tmp[tx][ty] = x;
					return true;
				}
			}
		}
	}
	
	return false;
}

int main(void) {
	scanf("%d%d", &n, &t);
	while(t --) {
		int a, b;
		scanf("%d%d", &a, &b);
		vis[a][b] = true;
	}
	
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= n; j ++) {
			if((i + j) % 2 == 0 || vis[i][j]) continue;
			memset(st, false, sizeof st);
			if(find((i - 1) * n + j)) ans ++;
		}
	}
	
	printf("%d\n", ans);
	
	return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值