# 861.Score After Flipping Matrix

**861.Score After Flipping Matrix**

难度:Medium

> 有一个二维矩阵 A 其中每个元素的值为 0 或 1 。

移动是指选择任一行或列，并转换该行或列中的每一个值：将所有 0 都更改为 1，将所有 1 都更改为 0。

在做出任意次数的移动后，将该矩阵的每一行都按照二进制数来解释，矩阵的得分就是这些数字的总和。

返回尽可能高的分数。

```
示例：

输入：[[0,0,1,1],[1,0,1,0],[1,1,0,0]]
输出：39
解释：
转换为 [[1,1,1,1],[1,0,0,1],[1,1,1,1]]
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
 

提示：

1 <= A.length <= 20
1 <= A[0].length <= 20
A[i][j] 是 0 或 1
```

先对每行来说，保证最高位为1，然后对每列来说，保证至少大于一半的数为1.最后求和即可。

```
class Solution {
public:
    int matrixScore(vector<vector<int>>& A) {
        for(auto &a:A)
        {
            if(a[0] == 0 )
            {
                for(auto &n:a)
                 n=1-n;
            }
        }
        int len=A.size();
        for(int i=0;i<A[0].size();i++)
        {
            int sum=0;
            for(int j=0;j<len;j++)
            sum+=A[j][i];

            if(sum<= len/2)
            {
                for(int j=0;j<len;j++)
                A[j][i] = 1-A[j][i];
            }
        }
        vector<int>tmp(A[0].size());
        for(int j=0;j<A[0].size();j++)
        {
            for(int i=0;i<A.size();i++)
            tmp[j] +=A[i][j];
        }
        
        int res=0;
        for(int i=0;i<tmp.size();i++)
            res= res*2+ tmp[i];

        return res;
    }
};
```

> 执行用时 :4 ms, 在所有 C++ 提交中击败了86.54%的用户\
> 内存消耗 :8.7 MB, 在所有 C++ 提交中击败了100.00%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/861.score_after_flipping_matrix.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
