2014東北農大校賽--A. Paint it! (預先處理),2014--a

來源:互聯網
上載者:User

2014東北農大校賽--A. Paint it! (預先處理),2014--a

1141: A.Paint it!Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 13  Solved: 10
[Submit][Status][Web Board]Description

有一個n*m的棋盤,每個格子只可能是黑和白兩種顏色,你可以使用染料將某個格子染成黑色或者白色。求最少需要染多少個格子,
能使當前的棋盤變成類似於國際象棋的棋盤(每個格子臨邊的四個格子顏色都不與該格子的顏色相同)。

Input

n m(n,m<=100)
接下來輸入n行,每行m個0/1字元

Output

最少需要染的格子數

Sample Input
2 200002 20110
Sample Output
20
HINT

Source

2014農大校賽





解題思路:這題最簡單的思路是預先處理,就是每個棋盤,只要一個點的狀態確定了,其他的就都確定了,所以,總共的狀態也就只有兩種。不過,只要求出來一種ans,另一種的結果就是n*m - ans,其實最簡單的方法也不必要畫出所有的棋盤,只要按第一個點的狀態,判斷所有需要翻轉的點,將其翻轉並記錄個數即可。





AC代碼:

#include <iostream>#include <cstdio>#include <algorithm> using namespace std; string s[105];          //棋盤 int main(){//  freopen("in.txt", "r", stdin);    int n, m;     while(scanf("%d%d", &n, &m)!=EOF){        for(int i=0; i<n; i++)            cin>>s[i];        int ans = 0;        for(int i=0; i<n; i++)            for(int j=0; j<m; j++){                if(!i && !j) continue;                if(!j){                        //特殊處理第一列                    if(s[i][j] == s[i-1][j]){                        s[i][j] ^= 1;          //取反                        ans ++;                //記錄                    }                }                else{                    if(s[i][j] == s[i][j-1]){                        s[i][j] ^= 1;                        ans ++;                    }                 }            }        ans = min(ans, m*n - ans);              //取兩種情況中的最小值        printf("%d\n", ans);    }    return 0;}/**************************************************************    Problem: 1141    User: sxk    Language: C++    Result: Accepted    Time:1 ms    Memory:1692 kb****************************************************************/




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.