Uncle Tom ' s inherited land*
Special Judge
Problem descriptionyour old Uncle Tom inherited a piece of the land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, he 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 is sure, for you has not been to the place, but he may has made so many ponds then the land could now consist of several disconnected islands.)
Your Uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle had been informed that, at his great-great-uncle's request, a law had been passed which establishes that proper Ty can only is sold in rectangular lots the size of the squares of your uncle ' s property. Furthermore, ponds is not the Salable property.
Your Uncle asked Your help to determine the largest number of properties he could sell (the remaining squares would become Recreational parks).
inputinput would include several test cases. The first line of a test case contains, integers N and M, representing, respectively, the number of rows and columns of The Land (1 <= N, M <= 100). The second line would contain an integer k indicating the number of squares that has been turned into ponds ((N x M)-K <= 50). Each of the next K lines contains, 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. outputfor All test case in the input your program should first output on E line, containing a 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 is 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. sample INPUT4 461 11 42 24 14 24 44 344 0 sample Output4 (1,3) (2,1)-(3,1) (2,3)-(3,3) (2,4)-(3,4) 3 (+)-(2,1)-(1,3) (2,3) --(3,3)
#include <cstdio>#include<cstring>#include<vector>#include<algorithm>#definePB Push_backusing namespacestd;Const intd[4][2]={1,0,0,1,-1,0,0,-1};intn,m,k,link[100005],vis[100005];BOOLmp[ the][ the];BOOLDfsintcur) { for(intI=0;i<4; i++) { intx=cur/m+d[i][0],y=cur%m+d[i][1]; if(x<0|| y<0|| x>=n| | Y>=M)Continue; intv=x*m+y; if(!vis[v]&&!Mp[x][y]) {Vis[v]=1; if(link[v]==-1||DFS (Link[v])) {Link[v]=cur; return 1; } } } return 0;}intmatch () {intans=0; memset (Link,-1,sizeof(link)); for(intI=0; i<n*m;i++) { intx=i/m,y=i%m; if((x+y) &1&&!Mp[x][y]) {memset (Vis,0,sizeof(VIS)); if(Dfs (i)) ans++; } } returnans;}intMain () { while(SCANF ("%d%d", &n,&m), n|m) {scanf ("%d",&k); Memset (MP,0,sizeof(MP)); while(k--) { intx, y; scanf ("%d%d",&x,&y); mp[--x][--y]=1; } printf ("%d\n", Match ()); for(intI=0; i<n*m;i++) if(link[i]!=-1) printf ("(%d,%d)-(%d,%d) \ n", link[i]/m+1, link[i]%m+1, i/m+1, i%m+1); } return 0;}
HDU 1507 Uncle Tom ' s inherited land*