K6B editorial

PROBLEM LINK:

Practice

Contest

Author: Chandan Boruah

Tester: Chandan Boruah

Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Brute Force

PROBLEM:

Given a 2D boolean array, what is the minimum number of values to be changed to have all the array elements alternate in true and false both wrt row and column.

QUICK EXPLANATION:

Fix the first element to be true and then alternate the entire array, check the difference with the original. Then fix the first element to be false and do the same. Print the minimum of the two.

EXPLANATION:

The problem is brute force, there are two possibilities either first element is true and rest of the elements alternate based on that, or the first element is false and so on. So compare both the cases with the original array and find the minimum of both, print it.

AUTHOR’S SOLUTION

using System;
class some
{
	public static int find(int [,]arr,int start)
	{
		int count=0;
		for(int i=0;i<=arr.GetUpperBound(0);i++)
		{	
			for(int j=0;j<=arr.GetUpperBound(1);j++)
			{
				if(i%2==0)
				{
					if(j%2==0)
					{
						if(arr[i,j]!=start)count++;
					}
					else
					{
						if(arr[i,j]==start)count++;
					}
				
				}
				else
				{
					if(j%2==0)
					{
						if(arr[i,j]==start)count++;
					}
					else
					{
						if(arr[i,j]!=start)count++;
					}
				
				}
			}
		}
		return count;
	}				
	public static void Main()
	{
		int t=int.Parse(Console.ReadLine());
		for(int l=0;l<t;l++)
		{
			string[]rc=Console.ReadLine().Split();
			int r=int.Parse(rc[0]);
			int c=int.Parse(rc[1]);
			int[,]arr=new int[r,c];
			for(int i=0;i<r;i++)
			{
				string[]ss=Console.ReadLine().Split();
				for(int j=0;j<c;j++)
				{
					arr[i,j]=(ss[j]=="T"?1:0);
				}
			}
			Console.WriteLine(Math.Min(find(arr,1),find(arr,0)));
		}
	}
}
//