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.
The first line contains integer n (3 ≤ n < 300; n is odd). Each of the next n lines contains n small English letters — the description of Valera's paper.
Print string "YES", if the letters on the paper form letter "X". Otherwise, print string "NO". Print the strings without quotes.
5 xooox oxoxo soxoo oxoxo xooox
NO
3 wsw sws wsw
YES
3 xpx pxp xpe
NO
题目分析:判断给出的字符矩阵的对角线是否是全都一样的字符,除了对角线外的字符是否全都是一样的。
!!!!注意:对角线与除对角线外的字符是不能相同的。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <algorithm> using namespace std; char a[310][310]; int main() { int n; while (cin >> n) { for (int i = 0; i < n; i++) cin >> a[i]; int f = 1; char c = a[0][0], d = a[0][1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i == j) || (i + j == n - 1)) { if (a[i][j] != a[0][0]) { f = 0; break; } } else { if (a[i][j] != a[0][1]) { f = 0; break; } } } } if (f == 1) { if (c == d) cout << "NO" << endl; else cout << "YES" << endl; } else cout << "NO" << endl; } return 0; }