Sicily 14254. Wall Painting

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值