「CF404A」Valera and X
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.
1 2 3 4 5 6 |
5 xooox oxoxo soxoo oxoxo xooox |
1 |
NO |
1 2 3 4 |
3 wsw sws wsw |
1 |
YES |
1 2 3 4 |
3 xpx pxp xpe |
1 |
NO |
题解
注意全部都相等的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<iostream> #include<cstdio> using namespace std; char ch[501]; char a,b; int n; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",ch); if(i==1)a=ch[0],b=ch[1]; if(a==b){printf("NO");return 0;} for(int j=1;j<=n;j++) { if(i==j||i+j==n+1) { if(ch[j-1]!=a) {printf("NO");return 0;} } else if(ch[j-1]!=b) {printf("NO");return 0;} } } printf("YES"); return 0; } |