参看点击打开链接,或者黑书
这题就是在该题的基础上增加了一维。。。。
悲剧的是,输入M,N弄反了,WA了半天
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int K = 10;
int dp[2][1<<K][1<<K], prestatu1, prestatu2;
int num[110][55], M, N;
int now, nowi;
void DFS( int statu, int pos, int sum ){
if( pos > N ){
return;
}
dp[now][statu][prestatu1] = max( dp[now][statu][prestatu1], sum );
DFS( statu, pos + 1, sum );
int flag = 1;
if( pos > 2 ){
if( ( statu & ( 1 << ( pos - 3 ) ) ) ){
flag = 0;
}
}
if( pos > 1 ){
if( ( prestatu1 & ( 1 << ( pos - 2 ) ) ) ){
flag = 0;
}
}
if( pos < N ){
if( ( prestatu1 & ( 1 << ( pos ) ) ) ){
flag = 0;
}
}
if( prestatu2 & ( 1 << ( pos - 1 ) ) ){
flag = 0;
}
if( flag && num[nowi][pos] == 1 ){
dp[now][statu|(1<<(pos-1))][prestatu1] = max( dp[now][statu|(1<<(pos-1))][prestatu1], sum + 1 );
DFS( statu|(1<<(pos-1)), pos + 1, sum + 1 );
}
}
int main(){
while( scanf( "%d%d", &M, &N ) != EOF ){
memset( dp, -1, sizeof( dp ) );
for( int i = 1; i <= M; i++ ){
for( int j = 1; j <= N; j++ ){
scanf( "%d", &num[i][j] );
}
}
dp[0][0][0] = 0;
now = 0;
for( int i = 1; i <= M; i++ ){
now ^= 1;
memset( dp[now], -1, ( 1 << K ) * ( 1 << K ) * sizeof( int ) );
nowi = i;
for( int j = 0; j < ( 1 << N ); j++ ){
for( int k = 0; k < ( 1 << N ); k++ ){
if( dp[now^1][j][k] != -1 ){
prestatu1 = j;
prestatu2 = k;
DFS( 0, 1, dp[now^1][j][k] );
}
}
}
}
int ans = 0;
for( int i = 0; i < ( 1 << N ); i++ ){
for( int k = 0; k < ( 1 << N ); k++ ){
ans = max( ans, dp[now][i][k] );
}
}
cout << ans << endl;
}
return 0;
}