249 lines
5.1 KiB
C++
249 lines
5.1 KiB
C++
/* Problem URL: https://codeforces.com/problemset/problem/2045/M */
|
|
|
|
#include <bits/stdc++.h>
|
|
#include <ext/pb_ds/assoc_container.hpp>
|
|
#include <ext/pb_ds/tree_policy.hpp>
|
|
|
|
using namespace std;
|
|
using namespace __gnu_pbds;
|
|
|
|
template <class T, class comp = less<>>
|
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
const int oo = INT32_MAX >> 1;
|
|
const ll OO = INT64_MAX >> 1;
|
|
|
|
|
|
void pre()
|
|
{
|
|
|
|
}
|
|
|
|
#define TEST 0
|
|
|
|
void solve()
|
|
{
|
|
int r, c;
|
|
cin >> r >> c;
|
|
|
|
V<string> a(r);
|
|
cin >> a;
|
|
|
|
auto getpos = [&](int i, int j, int k) {
|
|
return i * c * 4 + j * 4 + k;
|
|
};
|
|
|
|
int tot = 0;
|
|
|
|
vvi graph(r * c * 4);
|
|
vi ty(r * c * 4, -1);
|
|
rep(i, r) {
|
|
rep(j, c) {
|
|
int n = getpos(i, j, 0);
|
|
int e = getpos(i, j, 1);
|
|
int s = getpos(i, j, 2);
|
|
int w = getpos(i, j, 3);
|
|
|
|
if (i > 0) {
|
|
int ot = getpos(i - 1, j, 2);
|
|
graph[n].push_back(ot);
|
|
}
|
|
|
|
if (j > 0) {
|
|
int ot = getpos(i, j - 1, 1);
|
|
graph[w].push_back(ot);
|
|
}
|
|
|
|
if (i < r - 1) {
|
|
int ot = getpos(i + 1, j, 0);
|
|
graph[s].push_back(ot);
|
|
}
|
|
|
|
if (j < c - 1) {
|
|
int ot = getpos(i, j + 1, 3);
|
|
graph[e].push_back(ot);
|
|
}
|
|
|
|
if (a[i][j] == '.') {
|
|
graph[e].push_back(w);
|
|
graph[w].push_back(e);
|
|
graph[s].push_back(n);
|
|
graph[n].push_back(s);
|
|
continue;
|
|
}
|
|
tot++;
|
|
ty[n] = i * c + j;
|
|
ty[e] = i * c + j;
|
|
ty[s] = i * c + j;
|
|
ty[w] = i * c + j;
|
|
|
|
if (a[i][j] == '/') {
|
|
graph[n].push_back(w);
|
|
graph[w].push_back(n);
|
|
graph[s].push_back(e);
|
|
graph[e].push_back(s);
|
|
continue;
|
|
}
|
|
|
|
graph[n].push_back(e);
|
|
graph[e].push_back(n);
|
|
graph[w].push_back(s);
|
|
graph[s].push_back(w);
|
|
}
|
|
}
|
|
|
|
V<bool> vis(r * c * 4);
|
|
vi sz(r * c * 4, -1);
|
|
|
|
set<int> s;
|
|
function<void(int)> dfs = [&](int i) {
|
|
vis[i] = true;
|
|
if (ty[i] != -1) {
|
|
s.insert(ty[i]);
|
|
}
|
|
|
|
repv(j, graph[i]) {
|
|
if (vis[j]) {
|
|
continue;
|
|
}
|
|
|
|
dfs(j);
|
|
}
|
|
};
|
|
|
|
function<void(int)> dfs2 = [&](int i) {
|
|
sz[i] = s.size();
|
|
|
|
repv(j, graph[i]) {
|
|
if (sz[j] != -1) {
|
|
continue;
|
|
}
|
|
|
|
dfs2(j);
|
|
}
|
|
};
|
|
|
|
rep(i, r) {
|
|
rep(j, c) {
|
|
rep(k, 4) {
|
|
int ac = getpos(i, j, k);
|
|
if (vis[ac]) {
|
|
continue;
|
|
}
|
|
|
|
dfs(ac);
|
|
dfs2(ac);
|
|
s.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
V<string> ans;
|
|
rep(i, r) {
|
|
int w = getpos(i, 0, 3);
|
|
if (sz[w] == tot) {
|
|
ans.emplace_back("W" + to_string(i + 1));
|
|
}
|
|
int e = getpos(i, c - 1, 1);
|
|
if (sz[e] == tot) {
|
|
ans.emplace_back("E" + to_string(i + 1));
|
|
}
|
|
}
|
|
|
|
rep(i, c) {
|
|
int n = getpos(0, i, 0);
|
|
if (sz[n] == tot) {
|
|
ans.emplace_back("N" + to_string(i + 1));
|
|
}
|
|
int s = getpos(r - 1, i, 2);
|
|
if (sz[s] == tot) {
|
|
ans.emplace_back("S" + to_string(i + 1));
|
|
}
|
|
}
|
|
|
|
cout << ans.size() << '\n';
|
|
repv(i, ans) {
|
|
cout << i << ' ';
|
|
}
|
|
cout << '\n';
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
pre();
|
|
|
|
int t;
|
|
(TEST && cin >> t) || (t = 1);
|
|
while (t--) {
|
|
solve();
|
|
}
|
|
}
|