2013
12-12

# Uncle Tom’s Inherited Land*

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

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.

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

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)

#include"stdio.h"
#include"string.h"
#define N 110
struct node
{
int x,y;
}aa[51];
int hash[N][N],a[N][N];
int map[N][N],v[N],mark[N];
int n,m,cnt;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void get_map()
{
int i,j,k,x,y;
scanf("%d",&k);
memset(hash,0,sizeof(hash));
while(k--)
{
scanf("%d%d",&x,&y);
hash[x][y]=1;
}
cnt=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
if(hash[i][j]==0)
{
a[i][j]=cnt;
aa[cnt].x=i;
aa[cnt].y=j;
cnt++;
}
}
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
if(hash[i][j]==0)
{
for(k=0;k<4;k++)
{
x=i+dx[k];
y=j+dy[k];
if(x>=1&&x<=n&&y>=1&&y<=m&&hash[x][y]==0)
{
map[a[i][j]][a[x][y]]=1;
map[a[x][y]][a[i][j]]=1;
}
}
}
}
}
int dfs(int k)
{
int i;
for(i=1;i<cnt;i++)
{
if((aa[i].x+aa[i].y)%2==1)
continue;
if(map[k][i]&&!v[i])
{
v[i]=1;
if(mark[i]==-1||dfs(mark[i]))
{
mark[i]=k;
return 1;
}
}
}
return 0;
}
void output()
{
int i;
for(i=1;i<cnt;i++)
if(mark[i]!=-1)
printf("(%d,%d)--(%d,%d)\n",aa[i].x,aa[i].y,aa[mark[i]].x,aa[mark[i]].y);
}
void solve()
{
int i,count;
count=0;
memset(mark,-1,sizeof(mark));
for(i=1;i<cnt;i++)
{
if((aa[i].x+aa[i].y)%2==0)
continue;
memset(v,0,sizeof(v));
if(dfs(i))
count++;
}
printf("%d\n",count);
output();
printf("\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
if(!n&&!m)
break;
get_map();
solve();
}
return 0;
}

1. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.

2. “再把所有不和该节点相邻的节点着相同的颜色”，程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的