2014 Northeast Agricultural University competition-A. Paint it! (Preprocessing), 2014 --

Source: Internet
Author: User

2014 Northeast Agricultural University competition-A. Paint it! (Preprocessing), 2014 --

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

There is an n * m chessboard. Each lattice can only be black or white. You can use a dye to dye a grid into black or white. Find the minimum number of grids to be stained,
It can make the current checkerboard look similar to a chess board (the four sides of each grid are not the same color as the color of the grid ).

Input

N m (n, m <= 100)
Next, enter n rows. Each line contains m 0/1 characters.

Output

Minimum number of grids to be stained

Sample Input
2 200002 20110
Sample Output
20
HINT

Source

2014 Agricultural University Competition





Solution: the simplest way to solve this problem is preprocessing, that is, every chessboard. If the status of a vertex is determined, all others are determined. Therefore, there are only two statuses in total. However, only one ans is required, and the other result is n * m-ans. In fact, the simplest method does not need to draw all the boards, you only need to determine the status of the first vertex, flip it, and record the number of all vertices to be flipped.





AC code:

# Include <iostream> # include <cstdio> # include <algorithm> using namespace std; string s [105]; // checkboard int main () {// freopen ("in.txt", "r", stdin); int n, m; while (scanf ("% 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) {// special processing the first column if (s [I] [j] = s [I-1] [j]) {s [I] [j] ^ = 1; // retrieve reverse ans ++; // record} else {if (s [I] [j] = s [I] [J-1]) {s [I] [j] ^ = 1; ans ++ ;}} ans = min (ans, m * n-ans ); // obtain the minimum value printf ("% d \ n", ans) in two cases;} return 0 ;} /*************************************** * *********************** Problem: 1141 User: sxk Language: C ++ Result: Accepted Time: 1 MS Memory: 1692 kb ************************************** **************************/




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.