poj 1719 Shooting Contest
2015-09-12 11:28
281 查看
Shooting Contest
Description
Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a target which is a rectangular grid. The target consists of r*c squares located in r rows and c columns. The squares are coloured white or black. There
are exactly two white squares and r-2 black squares in each column. Rows are consecutively labelled 1,..,r from top to bottom and columns are labelled 1,..,c from left to right. The shooter has c shots.
A volley of c shots is correct if exactly one white square is hit in each column and there is no row without white square being hit. Help the shooter to find a correct volley of hits if such a volley exists.
Example
Consider the following target:
Volley of hits at white squares in rows 2, 3, 1, 4 in consecutive columns 1, 2, 3, 4 is correct.
Write a program that: verifies whether any correct volley of hits exists and if so, finds one of them.
Input
The first line of the input contains the number of data blocks x, 1 <= x <= 5. The following lines constitute x blocks. The first block starts in the second line of the input file; each next block starts directly after the previous
one.
The first line of each block contains two integers r and c separated by a single space, 2 <= r <= c <= 1000. These are the numbers of rows and columns, respectively. Each of the next c lines in the block contains two integers separated by a single space. The
integers in the input line i + 1 in the block, 1 <= i <= c, are labels of rows with white squares in the i-th column.
Output
For the i-th block, 1 <= i <= x, your program should write to the i-th line of the standard output either a sequence of c row labels (separated by single spaces) forming a correct volley of hits at white squares in consecutive
columns 1, 2, ..., c, or one word NO if such a volley does not exists.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 4127 | Accepted: 1516 | Special Judge |
Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a target which is a rectangular grid. The target consists of r*c squares located in r rows and c columns. The squares are coloured white or black. There
are exactly two white squares and r-2 black squares in each column. Rows are consecutively labelled 1,..,r from top to bottom and columns are labelled 1,..,c from left to right. The shooter has c shots.
A volley of c shots is correct if exactly one white square is hit in each column and there is no row without white square being hit. Help the shooter to find a correct volley of hits if such a volley exists.
Example
Consider the following target:
Volley of hits at white squares in rows 2, 3, 1, 4 in consecutive columns 1, 2, 3, 4 is correct.
Write a program that: verifies whether any correct volley of hits exists and if so, finds one of them.
Input
The first line of the input contains the number of data blocks x, 1 <= x <= 5. The following lines constitute x blocks. The first block starts in the second line of the input file; each next block starts directly after the previous
one.
The first line of each block contains two integers r and c separated by a single space, 2 <= r <= c <= 1000. These are the numbers of rows and columns, respectively. Each of the next c lines in the block contains two integers separated by a single space. The
integers in the input line i + 1 in the block, 1 <= i <= c, are labels of rows with white squares in the i-th column.
Output
For the i-th block, 1 <= i <= x, your program should write to the i-th line of the standard output either a sequence of c row labels (separated by single spaces) forming a correct volley of hits at white squares in consecutive
columns 1, 2, ..., c, or one word NO if such a volley does not exists.
Sample Input
2 4 4 2 4 3 4 1 3 1 4 5 5 1 5 2 4 3 4 2 4 2 3
Sample Output
2 3 1 4 NO
题目大意:给你一个r*c的矩阵,然后这个矩阵每一列有两个格子是白色的,然后你在每列上开一枪,可以打这列的任意的白色格子,问你能否消除全部的行,如果包括所有行输出你在1-c列打抢的格子的行,当然这可以是不唯一的,记好!!!是不唯一!!!! 如果打不到就输出NO 题目首先给出有几组数据,对于每组数据第一行代表r和c,接下来c行有两个数,代表这行的哪两列是白色格子 解题思路:和以前那个经典的二分匹配差不多,就是在一行打一枪可以消灭所有行那个题目,很容易想到二分匹配,我们将白色格子的行指向列,然后求最大匹配,用行去匹配列,看是否能够得到的匹配数是r,如果是r的话则代表所有的行都可以被打到,然后每列对应的行,如果匹配数不是r就输出NO 当然有特例,如果r>c,这种情况开c抢根本不可能达到r行,所以直接输出NO 代码: #include<stdio.h> #include<string.h> bool map[1005][1005]; bool used[1005]; int link[1005]; int n,m; bool dfs(int u)//寻找增广路! 找到返回1,否则返回0! { int i; for(i=1;i<=m;i++) { if(map[u][i]&&!used[i])])//与x相连点,并且没有遍历到 { used[i]=true;;//标记为遍历过 if(link[i]==-1||dfs(link[i]))//i 点 没有和另一部分匹配或 和i配对的点 没有匹配! { link[i]=u; return true; } } } return false; } int main() { int i,j,x,y,t,num; scanf("%d",&t); while(t--) { num=0; scanf("%d%d",&n,&m); memset(map,0,sizeof(map)); memset(link,-1,sizeof(link)); if(n>m)//行大于列 { printf("NO\n"); continue; } for(i=1;i<=m;i++)//建图 { scanf("%d%d",&x,&y); map[x][i]=true; map[y][i]=true; } for(i=1;i<=n;i++) { memset(used,0,sizeof(used)); if(dfs(i)) num++; } if(num<n)//不是完美匹配 { printf("NO\n"); continue; } else { for(i=1;i<=m;i++) { if(link[i]!=-1) printf("%d ",link[i]); else { for(j=1;j<=n;j++) if(map[j][i]) { printf("%d ",j); break; } } } printf("\n"); } } return 0; }
相关文章推荐
- 多级放大电路耦合方式的优缺点
- ios 动态计算文本的高度或宽度
- setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lable1.'
- Android:Tab切换方法整理
- IOS 按钮(button)用法与属性实例
- linux内核裁剪及编译可加载模块
- Linux环境下安装mysql
- 数据结构实践项目——链表
- 【Win10 应用开发】集成文件打开选择器
- ASP.NET验证控件详解
- iOS中键值编码KVC的学习
- Rythem——Mac OS下简单易用类似fiddler的抓包工具
- 如何实施敏捷开发
- kamailio服务器内容补充
- UIpickerView
- Altium Designer原理图中放置文本、图片、直线、拷贝、智能粘贴
- Android Https相关完全解析 当OkHttp遇到Https
- Android Https相关完全解析 当OkHttp遇到Https
- 安装配置PHP 5.6 + Nginx 1.8
- php文件扩展名判断及获取文件扩展名的N种方法