14254. Wall Painting
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
A wall looks dirty, so Alpha has been sent there to repaint it in order to make it clean.
The wall is H feet tall and W feet wide, so Alpha first divide it into H*W regions per square foot and then starts his repaint, in other words, the wall‘s surface is divide into H rows and W columns.
Alpha is a lazy boy, he have found that some regions looks not so dirty so it does not need to be cleaned, and because he is a lazy boy, he will declare his work is done when he notice that each column of the wall has no more
than 2 regions remains dirty.
The way that Alpha cleans this wall is special. At each step, Alpha will choose a row and repaint it from left to right. After he has finished this step, all regions in this row will become white and clean. He just keeps repeating
this kind of step several times until he thinks his job is done.
Now Alpha wants to know how many rows he should repaint at least. This is an easy problem, could you tell him the answer?
Input
Input contains multiple test cases, terminated by EOF.
Each test case starts with two integer H and W (3<=H<=15, 3<=W<=40), indicating the wall‘s height and width. The following H lines each have W digits ‘0‘ or ‘1‘, describing the status of the wall‘s regions. If the j-th character
in the i-th line is ‘1‘, it means that the corresponding region on the wall (the j-th column in the i-th row) looks not so clean and might need to be repainted; otherwise it looks not so dirty and Alpha can either repaint or not repaint it.
Output
For each case, output the minimum number of rows Alpha should repaint in a line.
Sample Input
3 31111111114 510100110100110100011
Sample Output
10
Problem Source
SYSUCPC 2014 Preliminary (Online) Round
#include <stdio.h> #include <string.h> int H, W; char wall[20][45]; int ans; bool cleaned[20]; bool isOK() { for (int i = 0; i < W; i++) { int num = 0; for (int j = 0; j < H; j++) { if (wall[j][i] == '1') num++; if (num > 2) break; } if (num > 2) return false; } return true; } void dfs(int cleanedPos, int cleanedNum) { if (cleanedPos == H) { if (isOK() && cleanedNum < ans) ans = cleanedNum; return; } dfs(cleanedPos + 1, cleanedNum); char record[45]; for (int j = 0; j < W; j++) record[j] = wall[cleanedPos][j]; for (int j = 0; j < W; j++) wall[cleanedPos][j] = '0'; dfs(cleanedPos + 1, cleanedNum + 1); for (int j = 0; j < W; j++) wall[cleanedPos][j] = record[j]; } int main() { while (scanf("%d%d\n", &H, &W) != EOF) { for (int i = 0; i < H; i++) gets(wall[i]); ans = H; memset(cleaned, false, sizeof(cleaned)); dfs(0, 0); printf("%d\n", ans); } return 0; }