# Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2013    Accepted Submission(s): 830 Special Judge

Problem Description

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).

Input

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.

Output

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.

Sample Input

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

Sample Output

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)

Source

``` 1 #include<cstring>
2 #include<cstdio>
3 #include<cstdlib>
4 #define init(a) memset(a,0,sizeof(a))
5 const int maxn=105;
6 bool map[maxn][maxn];
7 bool vis[maxn][maxn];
8 struct node{
9   int x,y;
10 };
11 node mat[maxn][maxn];
12 int n,m;
13 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
14 int match(node aa){
15
16       node tem;
17      if(!map[aa.x][aa.y]){
18       for(int ll=0;ll<4;ll++)  //扫描它的四周...
19       {
20         tem=(node){aa.x+dir[ll][0],aa.y+dir[ll][1]};
21         if(tem.y>0&&tem.x>0&&tem.x<=n&&tem.y<=m&&!map[tem.x][tem.y]&&!vis[tem.x][tem.y]){
22              vis[tem.x][tem.y]=1;
23              if(!mat[tem.x][tem.y].x||match(mat[tem.x][tem.y])){
24               mat[tem.x][tem.y]=aa;
25               return 1;
26             }
27           }
28         }
29        }
30  return 0;
31 }
32 int main()
33 {
34     int k,x,y;
35     //freopen("test.in","r",stdin);
36     while(scanf("%d%d",&n,&m)!=EOF,n+m){
37      scanf("%d",&k);
38      init(map);
39     while(k--){
40          scanf("%d%d",&x,&y);
41           map[x][y]=1;  //代表一个池塘
42     }
43     init(mat);
44     int ans=0;
45     for(int i=1;i<=n;i++){
46    for(int j=1;j<=m;j++){
47       if(((i+j)&1)){  //不娶相邻的点...取奇数或者偶数都一样,,
48       init(vis);
49       ans+=match((node){i,j});
50       }
51    }
52    }
53    printf("%d\n",ans);
54       for(int i=1;i<=n;i++){
55    for(int j=1;j<=m;j++){
56        if(mat[i][j].x){
57            printf("(%d,%d)--(%d,%d)\n",i,j,mat[i][j].x,mat[i][j].y);
58        }
59    }
60    }
61   }
62   return 0;
63 }```

0 条评论

## 相关文章

### HDUOJ--1865 1string

1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

23460

### HDU 1885 Key Task(BFS)

Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

36540

### hdu 4009 Transfer water（最小型树图）

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/657...

37990

### HDUOJ--1058HangOver

HangOver Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (...

29360

### HDU-1054 Strategic Game(树形DP)

Strategic Game Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32...

35180

### HDUOJ 1099——Lottery

Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

37170

### HDU 5651 xiaoxin juju needs help

xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:...

30370

40450

### POJ-2039 To and Fro

To and Fro Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8...

22140

### HDUOJ---(4708)Rotation Lock Puzzle

Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327...

25240