Problem:
Square Carpets

Mr. Frugal bought a new house.
He feels deeply in love with his new house
because it has a comfortable living room
in which he can put himself completely at ease.
He thinks his new house is a really good buy.

But, to his disappointment,
the floor of its living room has some scratches on it.

The floor has a rectangle shape, covered with square panels.
He wants to replace all the scratched panels with flawless panels,
but he cannot afford to do so.
Then, he decides to cover all the scratched panels
with carpets.

The features of the carpets he can use are as follows.

Carpets are square-shaped.

Carpets may overlap each other.

Carpets cannot be folded.

Different sizes of carpets are available.
Lengths of sides of carpets are multiples of that of the panels.

The carpets must cover all the scratched panels,
but must not cover any of the flawless ones.

For example,
if the scratched panels are as shown in Figure 1,
at least 6 carpets are needed.

Figure 1: Example Covering

As carpets cost the same irrespective of their sizes,
Mr. Frugal would like to use as few number of carpets as possible.

Your job is to write a program which tells the minimum number
of the carpets to cover all the scratched panels.

Input

The input consists of multiple data sets.

As in the following, the end of the input is indicated by a line
containing two zeros.

DataSet1

DataSet2

...

DataSetn

0 0

Each data set (DataSeti) represents the
state of a floor. The format of a data set is as follows.

W H

P11 P12 P13 ... P1W

P21 P22 P23 ... P2W

...

PH1 PH2
PH3 ... PHW

The positive integers W and H are the numbers of panels
on the living room
in the x- and y- direction, respectively.
The values of W and H are no more than 10.
The integer Pyx represents the state of the panel.
The value of Pyx means,

0: flawless panel (must not be covered),

1: scratched panel (must be covered).

Output

For each data set,
your program should output a line containing one integer which
represents the minimum number of the carpets to cover
all of the scratched panels.

Sample Input

4 3
0 1 1 1
1 1 1 1
1 1 1 1
8 5
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1
0 1 1 1 0 1 1 1
8 8
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
10 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
0 0

Output for the Sample Input

2
6
14
29

Solution in JAVA (judge verdict: Wrong Answer)



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main
{
	static List<Integer> ans = new ArrayList<Integer>();
	static Scanner scan = new Scanner(System.in);
	static boolean imm = true;
	
	public static void main(String[] args) 
	{
		int w;
		int h;
		
		while(true)
		{
			w = scan.nextInt();
			h = scan.nextInt();
			
			if(h==0 && w == 0)
			{
				if(imm == false)
				{
					for(int i : ans)
					{
						System.out.println(i);
					}
				}
				return;
			}
			else
			{
				new DataSet(w, h);
			}
		}
	}

	static class DataSet 
	{
		int width;
		int height;
		int floor[][] = new int[10][10];
		
		public DataSet(int w, int h)
		{
			width = w;
			height = h;
			
			for(int i = 0; i < height; i++)
			{
				for(int j = 0; j < width; j++)
				{
					floor[j][i] = scan.nextInt();
				}
			}
			
			int anss = compute();
			ans.add(anss);			
			if(imm) {System.out.println(anss);}
		}
		
		public int compute()
		{
			int carpets = 0;
			
			while(true)
			{
				//find biggest uncovered rectangle
				int maxSize = 0;
				int maxCovered = 0;
				int maxRecovered = 999;
				int maxAdditionalPriority = 0;
				int maxX = -1;
				int maxY = -1;
				
				for(int i = 0; i < height; i++)//y
				{
					for(int j = 0; j < width; j++)//x
					{
						if(floor[j][i] == 1 || floor[j][i] == 2) //1 scratched, 2 covered
						{
							//check rectangle from this point
							boolean go = true;
							int size = 1;
							boolean hasUncoveredPanel = false;
							int covered = 0;
							int recovered = 0;
							int additionalPriority = 0;
							
							if(floor[j][i] == 1)
							{
								if(covered == 0) {covered += 1;}
//								if(covered != 0) {System.out.println("Apaan nih!!?");}
								hasUncoveredPanel = true;
							}
							
							if(j > 0 && floor[j-1][i] == 0) {additionalPriority += 1;}
							if(i > 0 && floor[j][i-1] == 0) {additionalPriority += 1;}
								
								while(go == true)
								{
									
									size += 1;
									
									if((i + size-1 > height-1) || (j + size-1 > width-1))
									{
										//out of bounds
										go = false;
										size -= 1;
									}
									else
									{
										//check if flawless panel exists under expanded carpet
										boolean prevHadUnc = false;
										int prevUnc = 0;
										int prevRec = 0;
										
										for(int z = 0; z < size && go == true; z++)
										{
											//+z
											int x1 = j + size-1;
											int y1 = i + z;
											int x2 = j + z;
											int y2 = i + size-1;
											
											if(floor[x1][y1] == 1 || floor[x2][y2] == 1)
											{
												prevHadUnc = true;
											}
											if(floor[x1][y1] == 1)
											{
												prevUnc += 1;
											}
											if(floor[x2][y2] == 1)
											{
												if(x1 == x2 && y1 == y2)
												{
												}
												else
												{
													prevUnc += 1;
												}
											}
											
											//rec
											if(floor[x1][y1] == 2)
											{
												prevRec += 1;
											}
											if(floor[x2][y2] == 2)
											{
												if(x1 == x2 && y1 == y2)
												{
												}
												else
												{
													prevRec += 1;
												}
											}
											
											
											if(floor[x1][y1] == 0 || floor[x2][y2] == 0)
											{
												go = false;
												size -= 1;
											}
										}
										
										if(go == true && prevHadUnc == true) 
										{
											hasUncoveredPanel = true;
											covered += prevUnc;
											recovered += prevRec;
										}
									}
								}
								
							//rectangle from this point checked
//							if((covered > maxCovered && hasUncoveredPanel) || (covered == maxCovered && additionalPriority > maxAdditionalPriority && hasUncoveredPanel) || (covered == maxCovered && additionalPriority == maxAdditionalPriority && size > maxSize && hasUncoveredPanel))
								
							if((covered > maxCovered && hasUncoveredPanel) || (covered == maxCovered && size > maxSize && hasUncoveredPanel) || (covered == maxCovered && size == maxSize && additionalPriority > maxAdditionalPriority && hasUncoveredPanel))
							{
								maxSize = size;
								maxCovered = covered;
								maxRecovered = recovered;
								maxAdditionalPriority = additionalPriority;
								maxX = j;
								maxY = i;
							}
						}
					}
				}	
				
				//now we know where to put the next carpet
//					if(maxSize == 0)
				if(maxCovered == 0)
				{
					//no more carpets
					return carpets;
				}
				else
				{
//					System.out.println("[" + (carpets+1) + "] Put carpet on " + maxX + ", " + maxY + " of size " + maxSize + ", covering " + maxCovered + " recovering " + maxRecovered);
					//cover with carpet
					for(int x = maxX; x < (maxX + maxSize); x++)
					{
						for(int y = maxY; y < (maxY + maxSize); y++)
						{
							floor[x][y] = 2;
						}
					}
					carpets += 1;
				}
			}
		}
	}
}