Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths

Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths_第1张图片
Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths_第2张图片
解题思路:
可发现固定步数到达的点都会在一条对角线上,,因此找到一条对角线i+j-1,然后与在回文路上对应的另一条对角线n+m+1-i-j对比,取0,1数量最小的加到ans里面。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define inf 0x7fffffff
#define ll long long

int main(){
    int i, n, j, k, t, m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        int a[70][2]={0};
        int lim = n+m+1;
        for(i=1; i<=n; i++){
            for(j=1; j<=m; j++){
                scanf("%d",&k);
                if(i+j-1==lim-i-j) continue;
                a[min(i+j-1,lim-i-j)][k]++;
            }
        }
        int ans=0;
        for(i=0;i<n+m;i++) ans+=min(a[i][0],a[i][1]);
        printf("%d\n",ans);
    }
}

你可能感兴趣的:(CF写题小技巧)