#include #include #include #include #include #include using namespace std; using ll = long long; int n; char type; vector> exc; vector> pos; bool in(int x, int y){ return (0 <= x && x < n && 0 <= y && y < n) && exc[x][y]; } void neigh(int x, int y, vector>& out){ if(type == 'K'){ vector dx = {-1, 0, 1, 1, 1, 0, -1, -1}; vector dy = {-1, -1, -1, 0, 1, 1, 1, 0}; for(int i = 0; i < 8; ++i){ if(in(x+dx[i], y+dy[i])) out.push_back({x+dx[i], y+dy[i]}); } return; }else if(type == 'N'){ vector dx = {-2, -1, 1, 2, 2, 1, -1, -2}; vector dy = {-1, -2, -2, -1, 1, 2, 2, 1}; for(int i = 0; i < 8; ++i){ if(in(x+dx[i], y+dy[i])) out.push_back({x+dx[i], y+dy[i]}); } return; } if(type == 'R' || type == 'Q'){ for(int i = 0; i < n; ++i){ if(i != y && in(x, i)) out.push_back({x, i}); if(i != x && in(i, y)) out.push_back({i, y}); } } if(type == 'B' || type == 'Q'){ for(int i = 0; i < n; ++i){ if((i != x || y + (i-x) != y) && in(i, y + (i-x))) out.push_back({i, y + (i-x)}); if((i != x || y - (i-x) != y) && in(i, y - (i-x))) out.push_back({i, y - (i-x)}); } } } vector> visited; vector>> parent; vector> post; void explore(int x, int y){ //printf("exploring %d %d, parent %d %d\n", x, y, parent[x][y].first, parent[x][y].second); visited[x][y] = true; vector> n; neigh(x, y, n); for(pair p : n){ if(!visited[p.first][p.second]){ parent[p.first][p.second] = {x, y}; explore(p.first, p.second); } } post.push_back({x, y}); } bool dfs(){ visited.assign(n, vector(n, false)); parent.assign(n, vector>(n, {-1, -1})); int comp = 0; for(pair p : pos){ if(!visited[p.first][p.second]){ comp++; explore(p.first, p.second); } } return comp == 1; } int main(){ char c; scanf("%d %c", &n, &type); string str; int count = 0; exc.assign(n, vector(n, false)); vector> row(n, vector()), col(n, vector()), dl(2*n, vector()), ds(2*n, vector()); for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ scanf(" %c", &c); //printf("char %c scanned\n", c); if(c == type){ exc[i][j] = true; pos.push_back({i, j}); count++; } } } if(!dfs()){ printf("NO"); return 0; } printf("YES"); int num = post.size(); for(int i = 0; i < num-1; ++i){ pair& p = post[i]; pair& par = parent[p.first][p.second]; printf("\n%d %d %d %d", p.first + 1, p.second + 1, par.first + 1, par.second + 1); } }