poj 2446:Chessboard
2446:Chessboard
- 总时间限制:
- 2000ms
- 内存限制:
- 65536kB
- 描述
-
Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below).
We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below:
1. Any normal grid should be covered with exactly one card.
2. One card should cover exactly 2 normal adjacent grids.Some examples are given in the figures below:
A VALID solution.
An invalid solution, because the hole of red color is covered with a card.
An invalid solution, because there exists a grid, which is not covered.
Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above. - 输入
- There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.
- 输出
- If the board can be covered, output "YES". Otherwise, output "NO".
- 样例输入
-
4 3 2 2 1 3 3
- 样例输出
-
YES
- 提示
-
A possible solution for the sample input. -
题意:
玩个游戏:给出一个m行n列的棋盘,里面有m*n个方格,其中有k个格子上有洞,我们称那些没洞的格子叫正常的格子(normal grid),Bob要遵循两个规则去玩:(1)任何一个正常的格子都要被一张卡覆盖,(卡片是1*2规格的)(2)一张卡要正好覆盖两个相邻的正常格子
我们的任务是帮助Bob决定是否棋盘在上述两个规则下能被覆盖。
思路:
因为棋盘上都是两个格子放一张卡片,所以到最后肯定是两个点两个点连着的。由此想到了二分匹配,具体是这样的:
给每个格子编号,从第一行到最后一行编号为1—12 ,然后每个点跟临近的正常点连接,这就建成了二分图
-
代码:
-
#include<stdio.h> #include<string.h> #define N 2050 int dx[5]={1,0,0,-1}; int dy[5]={0,1,-1,0}; int map[N][N],vis[N][N],ID[N][N],mode[N],used[N]; int n,m,k,t=0; int find(int x)//寻找增广路 {int i;for(i=0;i<t;i++){if(map[x][i]&&!used[i]){used[i]=1;if(mode[i]==0||find(mode[i])){mode[i]=x;return 1;}}}return 0; } int main() {while(scanf("%d%d%d",&n,&m,&k)!=EOF){memset(vis,0,sizeof(vis));memset(map,0,sizeof(map));memset(mode,0,sizeof(mode));int i,j,x,y;while(k--){scanf("%d%d",&x,&y);if(x&&y)vis[y-1][x-1]=1;}t=0;for(i=0;i<n;i++)for(j=0;j<m;j++){if(!vis[i][j])ID[i][j]=t++;}if(t%2!=0){printf("NO\n");continue;}for(i=0;i<n;i++){for(j=0;j<m;j++){if(!vis[i][j]){for(k=0;k<4;k++)//四个方向 (可能双向,所以匹配数变成2倍) {int xi=i+dx[k];int yi=j+dy[k];if(xi>=0&&xi<n&&yi>=0&&yi<m&&!vis[xi][yi])map[ID[i][j]][ID[xi][yi]]=1;//建图 }}}}int s=0;for(i=0;i<t;i++){memset(used,0,sizeof(used));if(find(i)){s++;//记录路的数量 }}if(s==t)//是否 是匹配数的二倍 printf("YES\n");elseprintf("NO\n");}return 0; }
poj 2446:Chessboard相关推荐
- POJ - 2446 Chessboard 二分匹配+建图
题目链接 题意很简单,是二分匹配的一种常见的题型,问题就在于怎样转换到二分图上来. 首先对对n*m-k正常点进行编号,然后遍历查找每一个正常点的上下左右是否能连接(就是判断另个点是否也是正常的),如果 ...
- poj 2446 Chessboard#匈牙利算法#
题目 求2*1的骨牌是否能覆盖棋盘. 分析 首先,如果抠掉后剩下的块数是奇数,就不会成立. 接着,如果奇数点和偶数点数量不同,也不会成立(留给读者思考) 特判结束后,二分图走起. 代码 #includ ...
- PKU 2446 Chessboard
题目 Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20511 Accepted: 6427 De ...
- python去除空行_python中去掉空行的问题
在python中处理空行时,经常会遇到一些问题.现总结经验如下: 1.遇到的空行如果只有换行符,直接使用=='\n'或者 len(line)==line.count('\n') 2.有多个空格+换行符 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- POJ 图论分类 + DP(较全 自己又加了点)
DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
- 一系列图论问题[转]
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 图论练习题(存起来练)
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Man ...
最新文章
- HNOI2019游记
- Python-OpenCV 笔记9 -- 模板匹配
- 用decimal模块增加python的浮点数精度
- 撬锁锤怎么用_安全锤是啥?消防蜀黍教你怎么选?如何快速破拆逃生?
- tinymce 富文本简单使用
- Javascript 学习笔记 - 函数 - 关于IIFE - 关于函数声明和函数表达式 - 个人总结
- 移动通信原理学习笔记之二—— 移动通信中的调制解调技术
- 协同办公软件竞品分析
- windows文件共享客户端的一些事——samba文件共享
- 解决swap file .swp already exists 问题
- 王者荣耀用什么开发的
- 使用C语言计算1+2+3+...+100
- 商业银行金融IC卡国密改造安全建设方案
- 个人购买的域名(需要公网服务器)快速配置免费的HTTPS证书【有手就行】
- GitHub和Git---仓库管理(三)
- 大数据学习----Javase----Day13【QQ号,二分查找】
- Golang 中的大顶堆或小顶堆
- 【电气专业知识问答】问:发生厂用母线故障时如何处理?
- 捷讯fw300r虚拟服务器口号,迅捷(Fast)FW300RM迷你路由器AP模式设置 | 192路由网
- 河南职称计算机证书领取,河南省职称改革领导小组办公室关于启用新版《河南省专业技术人员职务聘任证书》有关问题的通知...