Paint it!
description |
silver__bullet likes playing chess,and he got a n*m board.This board is not a normal chessboard,each gird may be white or black.silver__bullet can paint a gird white or black.He want to paint the girds as littel as possible so that this board can be a normal chessboard.
Notice.A normal chessboard should meet the following conditions:
Any adjacent grids have different color.
If two grids share a edge ,we call them adjacent. |
input |
The input contains several test cases. The first line of each test case contains two integers n (0 < n <= 100) and m (0 <= m <= 100) which indicate the size of the board.Then a n*m two-dimensional array follows , which describe the board.The input is terminated by the end of file. More details in the Sample Input. |
output |
For each test case, you should output an integer in a line,which indicate the minimum number of grids silver__bullet should paint so that hecan make the board to be a normal chessboard. |
sample_input |
2 2
00
00
2 2
01
10 |
sample_output |
2
0 |
此题只要找出做题方法 那么就会很轻松
要完成的目标是:彼此相互挨着的单元不同(分别为0 或 1)那么只要先处理第一行的元素使其满足要求 接下来的 n-1 行都对上一行的同列元素进行相同处理就OK了
需要注意的是:1.当每行判断完以后不要忘记将存的元素进行更改 1->0 或0->1
2.要求输出的是最小值 所以最后得出的结论需要进行处理
3.数据的输入是按照字符串进行输入的
代码如下:
<span style="font-size:14px;">#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char data[105][105];
int n,m;
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
cin>>data[i];
int ans=0;
for(int i=1;i<m;i++)
{
if(data[0][i]==data[0][i-1])
{
data[0][i]=(!(data[0][i-1]-'0')+'0');
ans++;
}
}
for(int i=1;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(data[i][j]==data[i-1][j])
{
data[i][j]=(!(data[i-1][j]-'0')+'0');
ans++;
}
}
}
if(m*n>2*ans)
cout<<ans<<endl;
else
cout<<m*n-ans<<endl;
}
return 0;
}</span>
吾之小白一枚 代码如有疏漏 还请各位大牛指点~
THX~