思路
把左右和上下选择分开讨论
先考虑左右的情况
如果右边的点选左方向,左边的点选右方向,这样的话需要计算w
其他情况求最大即可上下也是这种情况
代码
// Problem: 网格
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9986/E
// Memory Limit: 524288 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=1005;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
int a[N][N],dp1[N][N][2],dp2[N][N][2],ans;
int w(int x){
int num=x;
while(x){
if(x&1) num++;
x>>=1;
}
return num;
}
void solve(){
int n,m;cin>>n>>m;
rep(i,1,n) rep(j,1,m) cin>>a[i][j];
for(int i=1;i<=n;i++){
for(int j=2;j<=m;j++){
dp1[i][j][0]=max(dp1[i][j-1][0],dp1[i][j-1][1]+w(a[i][j-1]^a[i][j]));
dp1[i][j][1]=max(dp1[i][j-1][0],dp1[i][j-1][1]);
}
ans+=max(dp1[i][m][0],dp1[i][m][1]);
}
for(int j=1;j<=m;j++){
for(int i=2;i<=n;i++){
dp2[i][j][0]=max(dp2[i-1][j][0],dp2[i-1][j][1]+w(a[i-1][j]^a[i][j]));
dp2[i][j][1]=max(dp2[i-1][j][0],dp2[i-1][j][1]);
}
ans+=max(dp2[n][j][0],dp2[n][j][1]);
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
// int t;cin>>t;while(t--)
solve();
return 0;
}

京公网安备 11010502036488号