A. Valera and X

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the English alphabet for his English lesson. Unfortunately, the English teacher decided to have a test on alphabet today. At the test Valera got a square piece of squared paper. The length of the side equals n squares (n is an odd number) and each unit square contains some small letter of the English alphabet.

Valera needs to know if the letters written on the square piece of paper form letter "X". Valera's teacher thinks that the letters on the piece of paper form an "X", if:

  • on both diagonals of the square paper all letters are the same;
  • all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals.

Help Valera, write the program that completes the described task for him.

Input

The first line contains integer n (3 ≤ n < 300n is odd). Each of the next n lines contains n small English letters — the description of Valera's paper.

Output

Print string "YES", if the letters on the paper form letter "X". Otherwise, print string "NO". Print the strings without quotes.

Sample test(s)
input
5
xooox
oxoxo
soxoo
oxoxo
xooox
output
NO
input
3
wsw
sws
wsw
output
YES
input
3
xpx
pxp
xpe
output
NO


解题说明:本题是一道图形题,判断这幅图中是否存在一个大X,大X的条件是对角线位置上有相同的字符,其他区域有另一种相同的字符。很显然,这两种字符分别就对应图中第一行中的前两个位置上面的字符,记录下这两个字符之后,接下来只有判断对角线上面的字符是不是都是第一种,其他区域的字符是不是都是第二种即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include <algorithm>
#include<cstring>
using namespace std;

char a[302][302];

int main()
{
	int n, i, m, fl = 0, j;
	char s1, s2;
	scanf("%d", &n);
	for (i = 0; i<n; i++)
	{
		getchar();
		for (j = 0; j<n; j++)
		{
			scanf("%c", &a[i][j]);
			if (i == 0 && j == 0)
			{
				s1 = a[i][j];
			}
			else if (i == 0 && j == 1)
			{
				s2 = a[i][j];
			}
			if ((i == j || i == n - 1 - j) && a[i][j] != s1)
			{
				fl = 1;
			}
			else if (a[i][j] != s2&&!(i == j || i == n - 1 - j))
			{
				fl = 1;
			}
		}
	}
	if (s1 == s2)
	{
		fl = 1;
	}
	printf("%s\n", fl == 0 ? "YES" : "NO");
	return 0;
}


你可能感兴趣的:(A. Valera and X)