Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用c++ bitset写了一遍效率略搓。。。 代码如下: #include <cstdio> #include <iostream> #include <vector> #include <bitset> #include <algorithm> #include <limits> #define MAX_M 10 #define MAX_N 100 using namespace std; int main() { int N,M; cin >> N >> M; vector<bitset<MAX_M>> map(MAX_N,bitset<MAX_M>()); //input string str; getline(cin,str);//\n for(int i=0;i<N;i++){ getline(cin,str); for(int j=0;j<M;j++) if(str[j] == 'H') map[i][j] = true; } //get all valid state for a single horizontal line vector<bitset<MAX_M>> valid_state; unsigned all_state = 1<<M; for(unsigned i = 0;i<all_state;i++){ bitset<MAX_M> bs(i); if((bs&(bs<<1)).none() && (bs&(bs<<2)).none()) valid_state.push_back(bs); } int valid_state_size = valid_state.size(); vector<vector<vector<int>>> dp(N,vector<vector<int>>(valid_state_size,vector<int>(valid_state_size,-1))); //process the first line for(int state=0;state<valid_state_size;state++){ if((valid_state[state]&map[0]).none()) dp[0][0][state] = valid_state[state].count(); } //for each line for(int line = 1;line<N;line++){ //state:state index of current line for(unsigned int state = 0;state < valid_state_size;state++){ //if state conflicts with map[line] if((valid_state[state] & map[line]).any()) continue; //state_1:state index of line-1 for(unsigned int state_1 = 0;state_1 < valid_state_size;state_1++) { //if states conflict with each other if((valid_state[state_1] & valid_state[state]).any()) continue; //state_2:state index of line-2 for(unsigned int state_2 = 0;state_2 < valid_state_size;state_2++) { //if states conflict with each other if((valid_state[state] & valid_state[state_2]).any()) continue; //not calculated if(dp[line-1][state_2][state_1] == -1) continue; //dp here dp[line][state_1][state] = max(dp[line][state_1][state], dp[line-1][state_2][state_1]+(int)valid_state[state].count()); } } } } int ans = numeric_limits<int>::min(); for(int i=0;i<N;i++) for(int j=0;j<valid_state_size;j++) for(int k=0;k<valid_state_size;k++) ans = max(ans,dp[i][j][k]); cout << ans; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator