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 ************************************** **************************/