#include #include using namespace std; int n,counte=0,x=0,y; char c; char t[120][120]; bool odw[120][120],odw2[120][120]; vector > V[120][120]; int dfs(int x, int y, int counter) { odw[x][y]=true; counter++; int maks = counter; for(int i=0; i<(int)V[x][y].size(); i++) { if(odw[V[x][y][i].first][V[x][y][i].second]==false) { maks = max(maks, dfs(V[x][y][i].first,V[x][y][i].second, counter)); } } return maks; } int dfs2(int x, int y) { odw2[x][y]=1; for(int i=0; i<(int)V[x][y].size(); i++) { if(odw2[V[x][y][i].first][V[x][y][i].second]==0) { cout<>n>>c; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>t[i][j]; if(t[i][j]==c) { counte++; } } } if(c=='K') { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]=='K') { if(i-1>0) { if(t[i-1][j]!='.') { V[i][j].push_back({i-1,j}); } } if(j-1>0) { if(t[i][j-1]!='.') { V[i][j].push_back({i,j-1}); } } if(i-1>0&&j-1>0) { if(t[i-1][j-1]!='.') { V[i][j].push_back({i-1,j-1}); } } if(i+1<=n&&j+1<=n) { if(t[i+1][j+1]!='.') { V[i][j].push_back({i+1,j+1}); } } if(i+1<=n) { if(t[i+1][j]!='.') { V[i][j].push_back({i+1,j}); } } if(j+1<=n) { if(t[i][j+1]!='.') { V[i][j].push_back({i,j+1}); } } if(i+1<=n&&j-1>0) { if(t[i+1][j-1]!='.') { V[i][j].push_back({i+1,j-1}); } } if(i-1>0&&j+1<=n) { if(t[i-1][j+1]!='.') { V[i][j].push_back({i-1,j+1}); } } } } } } if(c=='R') { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]=='R') { x=i-1; while(x>0) { if(t[x][j]!='.') { V[i][j].push_back({x,j}); } x--; } x=j-1; while(x>0) { if(t[i][x]!='.') { V[i][j].push_back({i,x}); } x--; } x=i+1; while(x<=n) { if(t[x][j]!='.') { V[i][j].push_back({x,j}); } x++; } x=j+1; while(x<=n) { if(t[i][x]!='.') { V[i][j].push_back({i,x}); } x++; } } } } } if(c=='Q') { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]=='Q') { x=i-1; while(x>0) { if(t[x][j]!='.') { V[i][j].push_back({x,j}); } x--; } x=j-1; while(x>0) { if(t[i][x]!='.') { V[i][j].push_back({i,x}); } x--; } x=i+1; while(x<=n) { if(t[x][j]!='.') { V[i][j].push_back({x,j}); } x++; } x=j+1; while(x<=n) { if(t[i][x]!='.') { V[i][j].push_back({i,x}); } x++; } x=i-1; y=j-1; while(x>0&&y>0) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x--; y--; } x=i-1; y=j+1; while(x>0&&y<=n) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x--; y++; } x=i+1; y=j-1; while(x<=n&&y>0) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x++; y--; } x=i+1; y=j+1; while(x<=n&&y<=n) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x++; y++; } } } } } if(c=='B') { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]=='B') { x=i-1; y=j-1; while(x>0&&y>0) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x--; y--; } x=i-1; y=j+1; while(x>0&&y<=n) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x--; y++; } x=i+1; y=j-1; while(x<=n&&y>0) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x++; y--; } x=i+1; y=j+1; while(x<=n&&y<=n) { if(t[x][y]!='.') { V[i][j].push_back({x,y}); } x++; y++; } } } } } if(c=='N') { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]=='N') { if(i-2>0&&j-1>0) { V[i][j].push_back({i-2,j-1}); } if(i-2>0&&j+1<=n) { V[i][j].push_back({i-2,j+1}); } if(i-1>0&&j-2>0) { V[i][j].push_back({i-1,j-2}); } if(i+1<=n&&j-2>0) { V[i][j].push_back({i-2,j-1}); } if(i+2<=n&&j-1>0) { V[i][j].push_back({i+2,j-1}); } if(i+2<=n&&j+1<=n) { V[i][j].push_back({i+2,j+1}); } if(i-1>0&&j+2<=n) { V[i][j].push_back({i-1,j+2}); } if(i+1<=n&&j+2<=n) { V[i][j].push_back({i+1,j+2}); } } } } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(t[i][j]!='.') { x=dfs(i,j,0); for(int a=1;a<=100;a++) { for(int b=1;b<=100;b++) { odw[a][b]=0; } } if(x==counte) { cout<<"YES"<