#K2501. Palindromic Grid Transformation
Palindromic Grid Transformation
Palindromic Grid Transformation
Given an ( n \times n ) grid of lowercase letters, determine if it is possible to transform the grid so that every row and every column is a palindrome by changing at most ( k ) letters. A palindrome is a sequence of characters that reads the same forwards and backwards. The minimum number of changes required to make a string a palindrome can be computed as follows:
[ \text{cost}(s) = \sum_{i=0}^{\left\lfloor \frac{L}{2} \right\rfloor - 1} [s_i \neq s_{L-i-1}], ]
where ( L ) is the length of the string and ([\cdot]) denotes the indicator function that is 1 if the condition is true and 0 otherwise. Note that a single letter change may contribute simultaneously to fixing both a row and a column. Your task is to decide whether the grid can be transformed under the given constraint.
inputFormat
The input is read from standard input. The first line contains two integers ( n ) and ( k ) separated by a space. The next ( n ) lines each contain a string of length ( n ), representing the grid.
outputFormat
Output a single line containing either "YES" if it is possible to transform the grid under the allowed number of changes, or "NO" otherwise.## sample
3 2
aba
bcb
aba
YES