Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Do not try to rebuild every desk rectangle. Ask a simpler question: when can some color possibly be a deputy? Only when it touches the President's color by a side.
Adjacency here means 4-directional only. For a President cell at , the only relevant cells are and . Diagonals don't count.
Because every desk color is unique, if a color appears next to the President even once, that whole desk should be counted exactly once. So you want distinct colors, not cells.
Scan the whole grid. Whenever you see a cell with color , inspect its 4 neighbors. If a neighbor is inside the grid and its color is neither '.' nor , add that color to a set or a small seen[26] array.
The answer is In other words: nested loops + 4 directions + deduplicate colors.
Brutally simple, which means it's dangerously easy to overthink.
A desk is a deputy iff it shares a side with the President's desk. Since every desk color is unique, we do not count touching cells - we count distinct colors.
If the grid is , then the wanted set is
The answer is just .
The whole 'each desk is a rectangle' part is basically a red herring here. Cute, but unnecessary.
You might think: find the President's rectangle first. Sure, you can. But that's extra paperwork for zero payoff. If another desk touches the President, then some cell of that desk must be orthogonally adjacent to some President cell. So scanning neighbors of all cells with color already finds everything.
Also, diagonal contact is useless office gossip. Only the 4 directions matter.
seen array or set for colors already counted.'.' nor , mark that color as seen.Because colors are uppercase Latin letters, there are only possibilities, so a boolean array of size is enough.
We prove that the algorithm counts exactly the President's deputies.
So the set collected by the algorithm is exactly the set of deputy colors. Therefore the final count is correct. No black magic, no geometry cosplay.
Each cell is processed once, and for each President cell we check only neighbors. So the time complexity is
and the extra memory is
This is miles within the limits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m;
char c;
cin >> n >> m >> c;
vector<string> g(n);
for (int i = 0; i < n; ++i) cin >> g[i];
array<bool, 26> seen{};
int ans = 0;
const int dr[4] = {-1, 1, 0, 0};
const int dc[4] = {0, 0, -1, 1};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (g[i][j] != c) continue;
for (int k = 0; k < 4; ++k) {
int ni = i + dr[k];
int nj = j + dc[k];
if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
char ch = g[ni][nj];
if (ch == '.' || ch == c) continue;
int id = ch - 'A';
if (!seen[id]) {
seen[id] = true;
++ans;
}
}
}
}
cout << ans << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m;
char c;
cin >> n >> m >> c;
vector<string> g(n);
for (int i = 0; i < n; ++i) cin >> g[i];
array<bool, 26> seen{};
int ans = 0;
const int dr[4] = {-1, 1, 0, 0};
const int dc[4] = {0, 0, -1, 1};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (g[i][j] != c) continue;
for (int k = 0; k < 4; ++k) {
int ni = i + dr[k];
int nj = j + dc[k];
if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
char ch = g[ni][nj];
if (ch == '.' || ch == c) continue;
int id = ch - 'A';
if (!seen[id]) {
seen[id] = true;
++ans;
}
}
}
}
cout << ans << '\n';
return 0;
}