Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2437 Accepted Submission(s): 1007
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)
代码实现:
/*
* 解题心得:博主说由于英文不好,题目都是翻译过来的,看了好久才知道怎么回事,其他的我不多说了,实际上就是一块土地,分成了很多块小正方形,然后又的小正方形改造成了池塘,
* 这种土地是不能出售的,只能出售其他的土地,要求是:两个块小正方形是连着的变成一个长方形,这就可以卖出去一块土地了。而且题目正方形怎么连的都只能自己根据后面的测试数据去推
* 敲了,基本上是这样的,从左至右,从上至下开始搜索,首先找到一块不是池塘的地,然后先从这块地的下方开始搜索一块不是池塘的地,这样就算是可以出售一种情况了,如果下方是池塘,
* 则从右方搜索一块不是池塘的地(这里和上面都要注意,只需要搜索相邻的地,意思就是走一步),而且我发现,只要搜索下面和右边就可以了,而且只要走一步,不需要用深搜,简单的遍历
* 一下就好了。
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt(); // 输入行
int m = sc.nextInt(); // 输入列
if (m == 0 && n == 0) { // 如果输入的行和列是0,就直接退出
return;
}
int[][] map = new int[n + 1][m + 1]; // 地图
int k = 0; // 用来表示多少满足要求的地可以出售
int[][] result = new int[50][4]; // 用来存储满足条件的一块地的位置(x1,y1,x2,y2),根据题意只需要开50,其实感觉25也可以的,
// "(N x M) - K <= 50"
int count = sc.nextInt(); // 输入池塘数
while (count-- > 0) {
int y = sc.nextInt();
int x = sc.nextInt();
map[y][x] = 1; // 把是池塘的地方写1
}
// 遍历地图
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (map[i][j] == 0) {// 条件一:该出不是池塘
if (i + 1 <= n && map[i + 1][j] == 0) { // 条件二:该地的下面一块地没有越界,且不是池塘
// 把满足条件的的放进数组result[]中
result[k][0] = i;
result[k][1] = j;
result[k][2] = i + 1;
result[k][3] = j;
map[i][j] = 1;
map[i + 1][j] = 1;
k++;
continue;
}
if (j + 1 <= m && map[i][j + 1] == 0) { // 条件二:该地的右面一块地没有越界,且不是池塘
// 把满足条件的的放进数组result[]中
result[k][0] = i;
result[k][1] = j;
result[k][2] = i;
result[k][3] = j + 1;
map[i][j] = 1;
map[i][j + 1] = 1;
k++;
continue;
}
}
}
}
// 输出结果
System.out.println(k);// 输出满足条件的地的数量
for (int i = 0; i < k; i++) {
System.out.println("(" + result[i][0] + "," + result[i][1]+ ")--(" + result[i][2] + "," + result[i][3] + ")");
}
}
}
}
结果截图: