Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
2014-07-30 09:28
585 查看
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1869 Accepted Submission(s): 777
Special Judge
[align=left]Problem Description[/align]
Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small
squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected
islands.)
Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the
size of two squares of your uncle's property. Furthermore, ponds are not salable property.
Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).
[align=left]Input[/align]
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer
K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input
is indicated by N = M = 0.
[align=left]Output[/align]
For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity.
If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.
[align=left]Sample Input[/align]
4 4 6 1 1 1 4 2 2 4 1 4 2 4 4 4 3 4 4 2 3 2 2 2 3 1 0 0
[align=left]Sample Output[/align]
4 (1,2)--(1,3) (2,1)--(3,1) (2,3)--(3,3) (2,4)--(3,4) 3 (1,1)--(2,1) (1,2)--(1,3) (2,3)--(3,3)
题意就是找出最多的1*2的方块来,UP主的主要思路为:以i+j的奇偶性为分类,因为每个1*2的方块肯定是由一个奇点和一个偶点构成,二分图是思路都是某个东西由2类东西构成,再去用匈牙利算法,然后再用map数组偶点存匹配的方向
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int map[105][105]; bool vis[105][105]; int d[5][2]={{0,0},{1,0},{0,1},{-1,0},{0,-1}}; int x, y; void myscanf() { memset(map,0,sizeof(map)); int m, a, b; scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); map[a][b] = -1; } } void myprintf(int xi, int yi) { int f = map[xi][yi]; int fx = d[f][0] + xi; int fy = d[f][1] + yi; printf("(%d,%d)--(%d,%d)\n",xi,yi,fx,fy); } bool pd(int xi, int yi) { if(xi<=0 || xi>x || yi<=0 || yi>y || map[xi][yi]==-1) return false; return true; } bool dfs(int xi,int yi) { for(int i=1;i<=4;i++) { int fx = xi+d[i][0]; int fy = yi+d[i][1]; if(pd(fx,fy) && !vis[fx][fy]) { vis[fx][fy] = true; int f = map[fx][fy]; int ex = fx + d[f][0]; int ey = fy + d[f][1]; if(!f || dfs(ex,ey)) { map[fx][fy] = (i+2)%4==0?4:(i+2)%4; return true; } } } return false; } int find() { int res = 0; for(int i=1;i<=x;i++) { for(int j=1;j<=y;j++) { if((i+j)%2) { memset(vis,0,sizeof(vis)); if(map[i][j]==-1) continue; if(dfs(i,j)) res++; } } } return res; } int main() { // freopen("in.txt","r+",stdin); // freopen("out1.txt","w+",stdout); while(scanf("%d%d",&x,&y)!=EOF) { if(x==0 && y==0) break; myscanf(); cout<<find()<<endl; for(int i=1;i<=x;i++) { for(int j=1;j<=y;j++) { if(map[i][j]>0) { myprintf(i,j); } } } printf("\n"); } // fclose(stdin); // fclose(stdout); return 0; }
相关文章推荐
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
- Brush Mode --- Nyoj 236 分类: Brush Mode 2014-04-02 06:56 116人阅读 评论(0) 收藏
- Nodejs开发web环境搭建 分类: Android网络 2014-06-29 12:14 112人阅读 评论(0) 收藏
- 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
- Poj 1029 分类: Translation Mode 2014-04-04 10:18 112人阅读 评论(0) 收藏
- CWnd *和HWnd转换 分类: VC++ 2013-11-08 09:28 456人阅读 评论(0) 收藏
- Beautiful People 分类: Brush Mode 2014-10-01 14:33 100人阅读 评论(0) 收藏
- Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
- Android项目中如何正确的引入第三方jar包(工程) 分类: Android安装及配置 2014-06-27 22:39 112人阅读 评论(0) 收藏
- python apply()函数 分类: python学习 2014-07-30 15:33 180人阅读 评论(0) 收藏
- Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏
- Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
- C# WinForm 透明控件 PictureBox透明 分类: WinForm 2014-07-30 13:27 591人阅读 评论(0) 收藏
- 【solr基础教程之九】客户端 分类: H4_SOLR/LUCENCE 2014-07-30 15:28 904人阅读 评论(0) 收藏
- NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20:56 101人阅读 评论(0) 收藏