2013 TCO round2B EllysFigurines

题目的分析在http://apps.topcoder.com/wiki/display/tc/TCO+2013+Round+1B

当时比赛的时候用贪心做的,也就是每次都找X数目最多的行或者列,结果挂了。。。没经过证明的贪心算法的确是不靠谱啊。。。

注意到行的最大值仅仅是15,因此可以进行枚举,具体可以用位掩膜来实现,更重要的一点是:

一旦行的消除确定下来,那么要除去那些列是唯一确定的!这是当时困扰我的最大地方,怎么这么点破事都没想清楚。。。

最后一个问题:如果确定那些行需要删除,最小的列删除数目是多少?可以用贪心法解决。

代码如下,已经通过了system test:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class EllysFigurines {
public:
	int getMoves(vector <string>, int, int);
	//using greedy method to get minimal moves
	int solve(int mask, int L){
		int index = 0;
		int res = 0;
		while(index < 15){
			if((mask&(1<<index)) != 0){
				res++;
				index += L;
			}
			else index++;
		}
		return res;
	}
};

int EllysFigurines::getMoves(vector <string> board, int R, int C) {
	int row = board.size();
	int col = board[0].size();
	int res = 20;
	//use bit mask to search minimal result
	for(int i=0; i<(1<<row); i++){
		int mask2 = 0;
		for(int j=0; j<row; j++){
			if((i&(1<<j)) == 0){
				for(int k=0; k<col; k++){
					if(board[j][k] == 'X')
						mask2 |= (1<<k);
				}
			}//end if
		}//end for loop
		res = min(res, solve(i, R)+solve(mask2, C));
	}
	return res;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值