text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int maxn = 5001; struct gtr { int x; int y; }; int m, n, i, j, res, x, y, ll, rr, xt, yt, h, k, sl; int tren[maxn], duoi[maxn], trai[maxn], phai[maxn], up[maxn], down[maxn], leftt[maxn], rightt[maxn]; string s[5001]; int main() { cin >> n >> m; for (i = 1; i <= n; i++) { cin >> s[i]; s[i] = " " + s[i]; } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { ll = (i - 1) * m + j; if (i - 1 == 0) tren[ll] = 0; else if (s[i - 1][j] == '.') tren[ll] = tren[(i - 2) * m + j]; else { tren[ll] = (i - 2) * m + j; } if (j - 1 == 0) trai[ll] = 0; else if (s[i][j - 1] == '.') trai[ll] = trai[(i - 1) * m + j - 1]; else { trai[ll] = (i - 1) * m + j - 1; } } for (i = n; i >= 1; i--) for (j = m; j >= 1; j--) { ll = (i - 1) * m + j; if (i + 1 > n) duoi[ll] = 0; else if (s[i + 1][j] == '.') duoi[ll] = duoi[i * m + j]; else { duoi[ll] = i * m + j; } if (j + 1 > m) phai[ll] = 0; else if (s[i][j + 1] == '.') phai[ll] = phai[(i - 1) * m + j + 1]; else { phai[ll] = (i - 1) * m + j + 1; } } res = 0; sl = 1; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if (s[i][j] != '.') { for (x = 1; x <= n; x++) for (y = 1; y <= m; y++) { ll = (x - 1) * m + y; up[ll] = tren[ll]; down[ll] = duoi[ll]; leftt[ll] = trai[ll]; rightt[ll] = phai[ll]; } rr = 0; x = i; y = j; do { ll = (x - 1) * m + y; if (s[x][y] == 'U') xt = up[ll]; else if (s[x][y] == 'L') xt = leftt[ll]; else if (s[x][y] == 'R') xt = rightt[ll]; else xt = down[ll]; rr++; yt = leftt[ll]; h = (yt - 1) / m + 1; k = (yt - 1) % m + 1; if (h != 0) { if (rightt[ll] == 0) rightt[yt] = 0; else rightt[yt] = rightt[ll]; } yt = rightt[ll]; h = (yt - 1) / m + 1; k = (yt - 1) % m + 1; if (h != 0) { if (leftt[ll] == 0) leftt[yt] = 0; else leftt[yt] = leftt[ll]; } yt = up[ll]; h = (yt - 1) / m + 1; k = (yt - 1) % m + 1; if (h != 0) { if (down[ll] == 0) down[yt] = 0; else down[yt] = down[ll]; } yt = down[ll]; h = (yt - 1) / m + 1; k = (yt - 1) % m + 1; if (h != 0) { if (up[ll] == 0) up[yt] = 0; else up[yt] = up[ll]; } x = (xt - 1) / m + 1; y = (xt - 1) % m + 1; } while (xt != 0); if (res < rr) { res = rr; sl = 1; } else if (res == rr) sl++; } cout << res << " " << sl; }
#include <bits/stdc++.h> using namespace std; char M[5050]; int U[5050], D[5050], L[5050], R[5050]; int nU[5050], nD[5050], nL[5050], nR[5050]; int m, n, ans; void DELETE(int a) { nU[nD[a]] = nU[a]; nD[nU[a]] = nD[a]; nL[nR[a]] = nL[a]; nR[nL[a]] = nR[a]; } void f(int a) { if (a < 0 || a >= m * n) return; ans++; DELETE(a); if (M[a] == 'U') f(nU[a]); if (M[a] == 'D') f(nD[a]); if (M[a] == 'L') f(nL[a]); if (M[a] == 'R') f(nR[a]); } int main(int argc, char const *argv[]) { cin >> n >> m; int i; for (i = 0; i < n * m; i++) { U[i] = i < m ? -1 : i - m; D[i] = i + m > n * m ? -1 : i + m; L[i] = i % m == 0 ? -1 : i - 1; R[i] = (i + 1) % m == 0 ? -1 : i + 1; } for (i = 0; i <= m * n; i++) { cin >> M[i]; if (M[i] == '.') { U[D[i]] = U[i]; D[U[i]] = D[i]; L[R[i]] = L[i]; R[L[i]] = R[i]; } } int num = 0, max = 0; for (i = 0; i < m * n; i++) { if (M[i] == '.') continue; ans = 0; memcpy(nU, U, sizeof(U)); memcpy(nD, D, sizeof(D)); memcpy(nL, L, sizeof(L)); memcpy(nR, R, sizeof(R)); f(i); if (ans > max) { max = ans; num = 1; } else if (max == ans) num++; } cout << max << ' ' << num << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long M = 5005; char maze[M][M]; int n, m; int U[M], D[M], L[M], R[M]; void getUDLR() { for (int i = 0; i < n; i++) { int l = -1; for (int j = 0; j < m; j++) { if (maze[i][j] != '.') { int index = i * m + j; L[index] = l; if (l != -1) R[l] = index; l = index; } } R[l] = -1; } for (int j = 0; j < m; j++) { int u = -1; for (int i = 0; i < n; i++) { if (maze[i][j] != '.') { int index = i * m + j; U[index] = u; if (u != -1) D[u] = index; u = index; } } D[u] = -1; } } void Delete(int x, int y) { int index = x * m + y; if (U[index] != -1) D[U[index]] = D[index]; if (D[index] != -1) U[D[index]] = U[index]; if (L[index] != -1) R[L[index]] = R[index]; if (R[index] != -1) L[R[index]] = L[index]; } int dfs(int x, int y) { Delete(x, y); int index = x * m + y; switch (maze[x][y]) { case 'U': if (U[index] == -1) return 1; else return 1 + dfs(U[index] / m, y); break; case 'D': if (D[index] == -1) return 1; else return 1 + dfs(D[index] / m, y); break; case 'L': if (L[index] == -1) return 1; else return 1 + dfs(x, L[index] % m); break; case 'R': if (R[index] == -1) return 1; else return 1 + dfs(x, R[index] % m); break; } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", maze[i]); int maxV = -1, num = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (maze[i][j] != '.') { getUDLR(); int res = dfs(i, j); if (res > maxV) { maxV = res; num = 1; } else if (res == maxV) { num++; } } } } printf("%d %d\n", maxV, num); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, ans, cnt; vector<string> str; vector<vector<pair<int, int> > > chi[4], par[4]; void build(int x, int y, int parx, int pary, int dir) { if (x < 0 || y < 0 || x == n || y == m) return; int nparx = parx, npary = pary; if (str[x][y] != '.') { if (parx != -1) { chi[dir][parx][pary] = pair<int, int>(x, y); par[dir][x][y] = pair<int, int>(parx, pary); } nparx = x, npary = y; } if (dir == 0) build(x - 1, y, nparx, npary, dir); else if (dir == 1) build(x + 1, y, nparx, npary, dir); else if (dir == 2) build(x, y - 1, nparx, npary, dir); else build(x, y + 1, nparx, npary, dir); } void init() { str.resize(n); for (int i = int(0); i <= int(n - 1); i++) cin >> str[i]; for (int i = int(0); i <= int(3); i++) { chi[i].resize(n); par[i].resize(n); for (int j = int(0); j <= int(n - 1); j++) { chi[i][j].resize(m); par[i][j].resize(m); for (int k = int(0); k <= int(m - 1); k++) { chi[i][j][k] = pair<int, int>(-1, -1); par[i][j][k] = pair<int, int>(-1, -1); } } } for (int i = int(0); i <= int(m - 1); i++) { for (int j = int(n - 1); j >= int(0); j--) { if (str[j][i] != '.') { build(j, i, -1, -1, 0); break; } } for (int j = int(0); j <= int(n - 1); j++) { if (str[j][i] != '.') { build(j, i, -1, -1, 1); break; } } } for (int i = int(0); i <= int(n - 1); i++) { for (int j = int(m - 1); j >= int(0); j--) { if (str[i][j] != '.') { build(i, j, -1, -1, 2); break; } } for (int j = int(0); j <= int(m - 1); j++) { if (str[i][j] != '.') { build(i, j, -1, -1, 3); break; } } } ans = 0; cnt = 0; } void recurse(int x, int y, int dep) { if (dep > ans) { ans = dep; cnt = 1; } else if (dep == ans) cnt++; pair<int, int> nex; if (str[x][y] == 'U') nex = chi[0][x][y]; else if (str[x][y] == 'D') nex = chi[1][x][y]; else if (str[x][y] == 'L') nex = chi[2][x][y]; else nex = chi[3][x][y]; for (int i = int(0); i <= int(3); i++) { if (par[i][x][y].first != -1) chi[i][par[i][x][y].first][par[i][x][y].second] = chi[i][x][y]; if (chi[i][x][y].first != -1) par[i][chi[i][x][y].first][chi[i][x][y].second] = par[i][x][y]; } if (nex.first != -1) recurse(nex.first, nex.second, dep + 1); for (int i = int(0); i <= int(3); i++) { if (par[i][x][y].first != -1) chi[i][par[i][x][y].first][par[i][x][y].second] = pair<int, int>(x, y); if (chi[i][x][y].first != -1) par[i][chi[i][x][y].first][chi[i][x][y].second] = pair<int, int>(x, y); } } int main() { scanf("%d", &n); scanf("%d", &m); init(); for (int i = int(0); i <= int(n - 1); i++) { for (int j = int(0); j <= int(m - 1); j++) { if (str[i][j] != '.') recurse(i, j, 1); } } printf("%d %d\n", ans, cnt); return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int I, J; int Key; node *Left; node *Right; node *Down; node *Up; }; node **mas; int **error; int n, m; void update(); bool NotNull(node N) { if (N.Key != -1) return true; return false; }; bool NotNullKey(int Cell, int Key) { if (Cell != Key) return true; return false; }; int getKey(int i, int j) { return i * m + j; }; void set_left(int iNode, int jNode, int iLink, int jLink) { if (iNode == -1 || jNode == -1) return; if (NotNull(mas[iNode][jNode]) && (iLink != -1 && jLink != -1)) mas[iNode][jNode].Left = mas[iLink] + jLink; }; void set_right(int iNode, int jNode, int iLink, int jLink) { if (iNode == -1 || jNode == -1) return; if (NotNull(mas[iNode][jNode]) && (iLink != -1 && jLink != -1)) mas[iNode][jNode].Right = mas[iLink] + jLink; }; void set_down(int iNode, int jNode, int iLink, int jLink) { if (iNode == -1 || jNode == -1) return; if (NotNull(mas[iNode][jNode]) && (iLink != -1 && jLink != -1)) mas[iNode][jNode].Down = mas[iLink] + jLink; }; void set_up(int iNode, int jNode, int iLink, int jLink) { if (iNode == -1 || jNode == -1) return; if (NotNull(mas[iNode][jNode]) && (iLink != -1 && jLink != -1)) mas[iNode][jNode].Up = mas[iLink] + jLink; }; node *getNext(node *Node, int Key) { node *Current; switch (Node->Key) { case 2: Current = Node->Down; while (Current != (node *)NULL) { if (NotNullKey(error[Current->I][Current->J], Key)) { return Current; } else { Current = Current->Down; } } return Current; break; case 3: Current = Node->Up; while (Current != (node *)NULL) { if (NotNullKey(error[Current->I][Current->J], Key)) { return Current; } else { Current = Current->Up; } } return Current; break; case 0: Current = Node->Right; while (Current != (node *)NULL) { if (NotNullKey(error[Current->I][Current->J], Key)) { return Current; } else { Current = Current->Right; } } return Current; break; case 1: Current = Node->Left; while (Current != (node *)NULL) { if (NotNullKey(error[Current->I][Current->J], Key)) { return Current; } else { Current = Current->Left; } } return Current; break; default: return (node *)NULL; break; } return (node *)NULL; }; void solve(int &Max, int &Num) { Max = 0; Num = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int key = getKey(i, j); if (NotNull(mas[i][j])) { int temp = 0; node *Current = mas[i] + j; while (Current != (node *)NULL) { temp++; error[Current->I][Current->J] = key; node *Pred = Current; Current = getNext(Current, key); if (Current != (node *)NULL) { if (Pred->Left != (node *)NULL) Pred->Left->Right = Pred->Right; if (Pred->Right != (node *)NULL) Pred->Right->Left = Pred->Left; if (Pred->Down != (node *)NULL) Pred->Down->Up = Pred->Up; if (Pred->Up != (node *)NULL) Pred->Up->Down = Pred->Down; } } if (temp > Max) { Max = temp; Num = 1; } else { if (temp == Max) { Num++; } } update(); } else if (Max == 0) Num++; } } }; void update() { for (int i = 0; i < n; i++) { int iPred = -1, jPred = -1; int iCurrent = -1, jCurrent = -1; for (int j = 0; j < m; j++) { if (NotNull(mas[i][j])) { iPred = iCurrent; jPred = jCurrent; iCurrent = i; jCurrent = j; set_right(iPred, jPred, iCurrent, jCurrent); set_left(iCurrent, jCurrent, iPred, jPred); } } } for (int j = 0; j < m; j++) { int iPred = -1, jPred = -1; int iCurrent = -1, jCurrent = -1; for (int i = 0; i < n; i++) { if (NotNull(mas[i][j])) { iPred = iCurrent; jPred = jCurrent; iCurrent = i; jCurrent = j; set_down(iPred, jPred, iCurrent, jCurrent); set_up(iCurrent, jCurrent, iPred, jPred); } } } } int main() { scanf("%d %d", &n, &m); mas = (node **)calloc(n, sizeof(node *)); error = (int **)calloc(n, sizeof(int *)); for (int i = 0; i < n; i++) { mas[i] = (node *)calloc(m, sizeof(node)); error[i] = (int *)calloc(m, sizeof(int)); }; for (int i = 0; i < n; i++) { char *str = new char[m + 2]; scanf("%s", str); int len = strlen(str); for (int j = 0; j < len; j++) { error[i][j] = -1; mas[i][j].I = i; mas[i][j].J = j; mas[i][j].Left = NULL; mas[i][j].Right = NULL; mas[i][j].Down = NULL; mas[i][j].Up = NULL; switch (str[j]) { case 'R': mas[i][j].Key = 0; break; case 'L': mas[i][j].Key = 1; break; case 'D': mas[i][j].Key = 2; break; case 'U': mas[i][j].Key = 3; break; default: mas[i][j].Key = -1; break; } } delete[] str; } int Max, Num; update(); solve(Max, Num); printf("%d %d\n", Max, Num); return 0; }
#include <bits/stdc++.h> using namespace std; char matr[5000][5000]; bool used[5000][5000]; int res[5000][5000]; int main() { int n, m; cin >> n >> m; int** lft = new int*[n]; for (int i = 0; i < n; i++) lft[i] = new int[m]; int** rght = new int*[n]; for (int i = 0; i < n; i++) rght[i] = new int[m]; int** up = new int*[n]; for (int i = 0; i < n; i++) up[i] = new int[m]; int** down = new int*[n]; for (int i = 0; i < n; i++) down[i] = new int[m]; int** lft1 = new int*[n]; for (int i = 0; i < n; i++) lft1[i] = new int[m]; int** rght1 = new int*[n]; for (int i = 0; i < n; i++) rght1[i] = new int[m]; int** up1 = new int*[n]; for (int i = 0; i < n; i++) up1[i] = new int[m]; int** down1 = new int*[n]; for (int i = 0; i < n; i++) down1[i] = new int[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { lft[i][j] = -1; rght[i][j] = -1; up[i][j] = -1; down[i][j] = -1; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> matr[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (matr[i][j] != '.') { for (int k = 1; i + k < n; k++) { if (matr[i + k][j] != '.') { down[i][j] = i + k; break; } } for (int k = 1; i - k >= 0; k++) { if (matr[i - k][j] != '.') { up[i][j] = i - k; break; } } for (int k = 1; j + k < m; k++) { if (matr[i][j + k] != '.') { rght[i][j] = j + k; break; } } for (int k = 1; j - k >= 0; k++) { if (matr[i][j - k] != '.') { lft[i][j] = j - k; break; } } } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (matr[i][j] == '.') continue; for (int k = 0; k < n; k++) for (int l = 0; l < m; l++) { lft1[k][l] = lft[k][l]; rght1[k][l] = rght[k][l]; up1[k][l] = up[k][l]; down1[k][l] = down[k][l]; } int cnt = 0, i1 = i, j1 = j; while (matr[i1][j1] != '.') { if (up1[i1][j1] != -1) down1[up1[i1][j1]][j1] = down1[i1][j1]; if (down1[i1][j1] != -1) up1[down1[i1][j1]][j1] = up1[i1][j1]; if (lft1[i1][j1] != -1) rght1[i1][lft1[i1][j1]] = rght1[i1][j1]; if (rght1[i1][j1] != -1) lft1[i1][rght1[i1][j1]] = lft1[i1][j1]; if (matr[i1][j1] == 'U') { i1 = up1[i1][j1]; cnt++; if (i1 == -1) goto nxt; } else if (matr[i1][j1] == 'D') { i1 = down1[i1][j1]; cnt++; if (i1 == -1) goto nxt; } else if (matr[i1][j1] == 'L') { j1 = lft1[i1][j1]; cnt++; if (j1 == -1) goto nxt; } else if (matr[i1][j1] == 'R') { j1 = rght1[i1][j1]; cnt++; if (j1 == -1) goto nxt; } } nxt:; res[i][j] = cnt; } int maxi = 0, cnt1 = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) maxi = max(maxi, res[i][j]); cout << maxi << " "; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (res[i][j] == maxi) cnt1++; cout << cnt1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5010; char g[MAXN][MAXN]; int l[MAXN], r[MAXN], u[MAXN], d[MAXN]; int n, m; int id(int x, int y) { return (x - 1) * m + y; } int main() { scanf("%d%d\n", &n, &m); for (int i = 1; i <= n; i++) { scanf("%s", g[i] + 1); } int res = -1, ans; for (int X = 1; X <= n; X++) for (int Y = 1; Y <= m; Y++) { if (g[X][Y] == '.') continue; for (int i = 1; i <= n; i++) for (int j = 1, pre = 0; j <= m; j++) { if (g[i][j] == '.') continue; int v = id(i, j); l[v] = pre; if (pre) r[pre] = v; r[v] = 0; pre = v; } for (int j = 1; j <= m; j++) for (int i = 1, pre = 0; i <= n; i++) { if (g[i][j] == '.') continue; int v = id(i, j); u[v] = pre; if (pre) d[pre] = v; d[v] = 0; pre = v; } int x = X, y = Y, step = 0; while (1) { int now = id(x, y); r[l[now]] = r[now]; l[r[now]] = l[now]; u[d[now]] = u[now]; d[u[now]] = d[now]; if (g[x][y] == 'D') now = d[now]; if (g[x][y] == 'R') now = r[now]; if (g[x][y] == 'U') now = u[now]; if (g[x][y] == 'L') now = l[now]; step++; if (!now) break; x = (now - 1) / m + 1; y = (now - 1) % m + 1; } if (step > res) res = step, ans = 1; else if (step == res) ans++; } printf("%d %d\n", res, ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v); template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } template <typename A, typename B> istream &operator>>(istream &cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } long long seed = std::chrono::steady_clock::now().time_since_epoch().count(); mt19937_64 rng(seed); void usaco(string filename) { freopen((filename + ".in").c_str(), "r", stdin); freopen((filename + ".out").c_str(), "w", stdout); } const long double pi = 3.14159265358979323846; long long n, m, k, q, l, r, x, y, z; const long long template_array_size = 1e6 + 8432; long long a[template_array_size]; long long b[template_array_size]; long long c[template_array_size]; string second, t; long long ans = 0; const long long dr[4] = {0, 0, 1, -1}; const long long dc[4] = {-1, 1, 0, 0}; long long gv(char c) { if (c == 'L') return 0; if (c == 'R') return 1; if (c == 'D') return 2; return 3; } bool valid(long long r, long long c) { return r >= 0 && r < n && c >= 0 && c < m; } void solve(int tc = 0) { cin >> n >> m; long long mat[n][m]; for (long long i = 0; i < n; i++) { cin >> second; for (long long j = 0; j < m; j++) { if (second[j] == '.') mat[i][j] = -1; else mat[i][j] = gv(second[j]); } } pair<long long, long long> nxt[n][m][4]; long long v = -1, cv = 0; for (long long sr = 0; sr < n; sr++) { for (long long sc = 0; sc < m; sc++) { if (mat[sr][sc] == -1) continue; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { for (long long d = 0; d < 4; d++) { long long ni = i + dr[d], nj = j + dc[d]; nxt[i][j][d] = make_pair(ni, nj); } } } for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { if (mat[i][j] == -1) { pair<long long, long long> l = nxt[i][j][0], r = nxt[i][j][1]; if (valid(l.first, l.second)) nxt[l.first][l.second][1] = r; if (valid(r.first, r.second)) nxt[r.first][r.second][0] = l; pair<long long, long long> d = nxt[i][j][2], u = nxt[i][j][3]; if (valid(d.first, d.second)) nxt[d.first][d.second][3] = u; if (valid(u.first, u.second)) nxt[u.first][u.second][2] = d; } } } pair<long long, long long> pos = make_pair(sr, sc); long long cnt = 0; while (valid(pos.first, pos.second)) { pair<long long, long long> l = nxt[pos.first][pos.second][0], r = nxt[pos.first][pos.second][1]; if (valid(l.first, l.second)) nxt[l.first][l.second][1] = r; if (valid(r.first, r.second)) nxt[r.first][r.second][0] = l; pair<long long, long long> d = nxt[pos.first][pos.second][2], u = nxt[pos.first][pos.second][3]; if (valid(d.first, d.second)) nxt[d.first][d.second][3] = u; if (valid(u.first, u.second)) nxt[u.first][u.second][2] = d; pos = nxt[pos.first][pos.second][mat[pos.first][pos.second]]; ++cnt; } if (cnt > v) { v = cnt; cv = 1; } else if (cnt == v) ++cv; } } cout << v << " " << cv << '\n'; } int main() { { ios_base::sync_with_stdio(false); } { cin.tie(NULL); } cout << setprecision(12) << fixed; int tc = 1; for (int t = 0; t < tc; t++) solve(t); }
#include <bits/stdc++.h> using namespace std; char g[5010][5010]; short lx[5010][5010], rx[5010][5010]; short ly[5010][5010], ry[5010][5010]; int n, m; template <class T> void getarr(T ***a) { *a = (T **)malloc((n + 10) * sizeof(T *)); for (int i = 0; i < n + 10; i++) { (*a)[i] = (T *)malloc((m + 10) * sizeof(T)); } } void del(int x, int y) { rx[x][lx[x][y]] = rx[x][y]; lx[x][rx[x][y]] = lx[x][y]; ry[ly[x][y]][y] = ry[x][y]; ly[ry[x][y]][y] = ly[x][y]; } void init() { for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { lx[i][j] = j - 1; rx[i][j] = j + 1; } lx[i][0] = m; rx[i][m] = 0; } for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { ly[j][i] = j - 1; ry[j][i] = j + 1; } ly[0][i] = n; ry[n][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] == '.') { del(i, j); } } } } int to[300]; int solve(int x, int y) { int cc = 0; while (x != 0 && y != 0) { int d = to[g[x][y]]; cc++; del(x, y); if (d == 0) { x = ly[x][y]; } else if (d == 1) { y = rx[x][y]; } else if (d == 2) { x = ry[x][y]; } else { y = lx[x][y]; } } return cc; } int main() { to['U'] = 0; to['R'] = 1; to['D'] = 2; to['L'] = 3; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%s", g[i] + 1); } int ans1 = -1, ans2 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] != '.') { init(); int ret = solve(i, j); if (ans1 < ret) { ans1 = ret; ans2 = 1; } else if (ans1 == ret) { ans2++; } } } } printf("%d %d\n", ans1, ans2); return 0; }
#include <bits/stdc++.h> using namespace std; enum { UP, DOWN, LEFT, RIGHT }; const int MAX = 15500; struct Node { int dir; Node* pt[4]; } node[MAX], *cur, *row[MAX], *col[MAX], *st[MAX]; char s[MAX]; int r, c, top; int getDir(char c) { switch (c) { case 'U': return UP; case 'D': return DOWN; case 'L': return LEFT; case 'R': return RIGHT; } return -1; } void init() { cur = node; top = 0; for (int i = 0; i < r; i++) { row[i] = cur++; row[i]->pt[LEFT] = row[i]->pt[RIGHT] = row[i]; row[i]->pt[UP] = row[i]->pt[DOWN] = NULL; row[i]->dir = -1; } for (int i = 0; i < c; i++) { col[i] = cur++; col[i]->pt[UP] = col[i]->pt[DOWN] = col[i]; col[i]->pt[LEFT] = col[i]->pt[RIGHT] = NULL; col[i]->dir = -1; } } Node* add(int x, int y, int dir) { cur->pt[RIGHT] = row[x]->pt[RIGHT]; cur->pt[LEFT] = row[x]; cur->pt[DOWN] = col[y]->pt[DOWN]; cur->pt[UP] = col[y]; cur->pt[LEFT]->pt[RIGHT] = cur; cur->pt[RIGHT]->pt[LEFT] = cur; cur->pt[UP]->pt[DOWN] = cur; cur->pt[DOWN]->pt[UP] = cur; cur->dir = dir; return cur++; } int dfs(Node* t) { if (t->dir == -1) return 0; t->pt[LEFT]->pt[RIGHT] = t->pt[RIGHT]; t->pt[RIGHT]->pt[LEFT] = t->pt[LEFT]; t->pt[UP]->pt[DOWN] = t->pt[DOWN]; t->pt[DOWN]->pt[UP] = t->pt[UP]; int ret = dfs(t->pt[t->dir]) + 1; t->pt[LEFT]->pt[RIGHT] = t; t->pt[RIGHT]->pt[LEFT] = t; t->pt[UP]->pt[DOWN] = t; t->pt[DOWN]->pt[UP] = t; return ret; } int main() { int ret, cnt; while (scanf("%d%d", &r, &c) == 2) { ret = -1; cnt = 0; init(); for (int i = 0; i < r; i++) { scanf("%s", &s[i * c]); } for (int i = r - 1; i >= 0; i--) { for (int j = c - 1; j >= 0; j--) { if (s[i * c + j] != '.') st[top++] = add(i, j, getDir(s[i * c + j])); } } for (int i = 0; i < top; i++) { int sub = dfs(st[i]); if (sub > ret) { ret = sub; cnt = 1; } else if (sub == ret) { cnt++; } } printf("%d %d\n", ret, cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; char **m; int N, M; typedef struct chip { int l, r, u, d; } C; C **o, **p; int run(int i, int j) { if (p[i][j].r != -1) { if (p[i][j].l == -1) p[i][j + p[i][j].r].l = -1; else p[i][j + p[i][j].r].l = p[i][j].l + p[i][j].r; } if (p[i][j].l != -1) { if (p[i][j].r == -1) p[i][j - p[i][j].l].r = -1; else p[i][j - p[i][j].l].r = p[i][j].l + p[i][j].r; } if (p[i][j].d != -1) { if (p[i][j].u == -1) p[i + p[i][j].d][j].u = -1; else p[i + p[i][j].d][j].u = p[i][j].u + p[i][j].d; } if (p[i][j].u != -1) { if (p[i][j].d == -1) p[i - p[i][j].u][j].d = -1; else p[i - p[i][j].u][j].d = p[i][j].u + p[i][j].d; } if (m[i][j] == 'U') if (p[i][j].u != -1) return 1 + run(i - p[i][j].u, j); if (m[i][j] == 'D') if (p[i][j].d != -1) return 1 + run(i + p[i][j].d, j); if (m[i][j] == 'L') if (p[i][j].l != -1) return 1 + run(i, j - p[i][j].l); if (m[i][j] == 'R') if (p[i][j].r != -1) return 1 + run(i, j + p[i][j].r); return 1; } int main(int argc, char *argv[]) { scanf("%d%d", &N, &M); int i, j, k, l; m = (char **)malloc(sizeof(char *) * N); for (i = 0; i < N; i++) m[i] = (char *)malloc(sizeof(char) * (M + 1)); o = (C **)malloc(sizeof(C *) * N); for (i = 0; i < N; i++) o[i] = (C *)malloc(sizeof(C) * (M + 1)); p = (C **)malloc(sizeof(C *) * N); for (i = 0; i < N; i++) p[i] = (C *)malloc(sizeof(C) * (M + 1)); int best = 0, num = 0; for (i = 0; i < N; i++) scanf("%s", m[i]); for (i = 0; i < N; i++) for (j = 0; j < M; j++) { o[i][j].l = -1; o[i][j].r = -1; o[i][j].u = -1; o[i][j].d = -1; } for (i = 0; i < N; i++) for (j = 0; j < M; j++) if (m[i][j] != '.') { for (k = 1; j - k >= 0; k++) if (m[i][j - k] != '.') { o[i][j].l = k; o[i][j - k].r = k; break; } for (k = 1; i - k >= 0; k++) if (m[i - k][j] != '.') { o[i][j].u = k; o[i - k][j].d = k; break; } } for (i = 0; i < N; i++) for (j = 0; j < M; j++) { if (m[i][j] == '.') continue; for (k = 0; k < N; k++) for (l = 0; l < M; l++) { p[k][l].l = o[k][l].l; p[k][l].r = o[k][l].r; p[k][l].u = o[k][l].u; p[k][l].d = o[k][l].d; } k = run(i, j); if (k > best) { best = k; num = 0; } if (k == best) num++; } printf("%d %d\n", best, num); return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { short left, right, down, up; Node(short l = -1, short r = -1, short u = -1, short d = -1) : left(l), right(r), down(d), up(u) {} }; char field[5000][5000]; int main() { short n, m; cin >> n >> m; vector<vector<Node> > table(n, vector<Node>(m)); vector<vector<Node> > grid(n, vector<Node>(m)); for (int _n(n), i(0); i < _n; i++) for (int _n(m), j(0); j < _n; j++) cin >> field[i][j]; for (int _n(n), i(0); i < _n; i++) for (int _n(m), j(0); j < _n; j++) { if (field[i][j] == '.') continue; short left = -1; for (short k = j - 1; k >= 0; --k) if (field[i][k] != '.') { left = k; break; } short right = -1; for (short k = j + 1; k < m; ++k) if (field[i][k] != '.') { right = k; break; } short up = -1; for (short k = i - 1; k >= 0; --k) if (field[k][j] != '.') { up = k; break; } short down = -1; for (short k = i + 1; k < n; ++k) if (field[k][j] != '.') { down = k; break; } grid[i][j] = Node(left, right, up, down); } int ans = 0, cnt = 0; for (int _n(n), i(0); i < _n; i++) for (int _n(m), j(0); j < _n; j++) if (field[i][j] != '.') { for (int _n(n), k(0); k < _n; k++) memcpy(&table[k][0], &grid[k][0], sizeof(Node) * m); int score = 0; short row = i, col = j; while (row != -1 && col != -1) { ++score; const Node &node = table[row][col]; if (node.left != -1) table[row][node.left].right = node.right; if (node.right != -1) table[row][node.right].left = node.left; if (node.up != -1) table[node.up][col].down = node.down; if (node.down != -1) table[node.down][col].up = node.up; switch (field[row][col]) { case 'L': { col = node.left; break; } case 'R': { col = node.right; break; } case 'D': { row = node.down; break; } default: { row = node.up; break; } } } if (score > ans) { ans = score; cnt = 1; } else if (score == ans) cnt++; } cout << ans << ' ' << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long n, m; string st; long bst, bc, ans; long qi, qj, ti, tj; vector<vector<long> > u, l, r, d, ar; void dodel(long a, long b) { if (l[a][b] != -1) { r[a][l[a][b]] = r[a][b]; } if (r[a][b] != -1) { l[a][r[a][b]] = l[a][b]; } if (u[a][b] != -1) { d[u[a][b]][b] = d[a][b]; } if (d[a][b] != -1) { u[d[a][b]][b] = u[a][b]; } } bool outside(long a, long b) { return (a == -1 || b == -1); } long gi(long a, long b) { if (ar[a][b] == 'L' || ar[a][b] == 'R') return a; if (ar[a][b] == 'U') return u[a][b]; return d[a][b]; } long gj(long a, long b) { if (ar[a][b] == 'D' || ar[a][b] == 'U') return b; if (ar[a][b] == 'L') return l[a][b]; return r[a][b]; } void recalc() { long ls = -1; for (int i = 1; i <= n; i++) { ls = -1; for (int j = 1; j <= m; j++) { l[i][j] = ls; if (ar[i][j]) ls = j; } } for (int i = 1; i <= n; i++) { ls = -1; for (int j = m; j >= 1; j--) { r[i][j] = ls; if (ar[i][j]) ls = j; } } for (int j = 1; j <= m; j++) { ls = -1; for (int i = 1; i <= n; i++) { u[i][j] = ls; if (ar[i][j]) ls = i; } } for (int j = 1; j <= m; j++) { ls = -1; for (int i = n; i; i--) { d[i][j] = ls; if (ar[i][j]) ls = i; } } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; ar.resize(n + 5); for (int i = 0; i < ar.size(); i++) ar[i].resize(m + 5); l.resize(n + 5); for (int i = 0; i < l.size(); i++) l[i].resize(m + 5); r.resize(n + 5); for (int i = 0; i < r.size(); i++) r[i].resize(m + 5); u.resize(n + 5); for (int i = 0; i < u.size(); i++) u[i].resize(m + 5); d.resize(n + 5); for (int i = 0; i < d.size(); i++) d[i].resize(m + 5); for (int i = 1; i <= n; i++) { cin >> st; for (int j = 1; j <= m; j++) { if (st[j - 1] != '.') ar[i][j] = st[j - 1]; else ar[i][j] = 0; } } bst = -1; bc = -1; for (int ri = 1; ri <= n; ri++) for (int rj = 1; rj <= m; rj++) { if (ar[ri][rj] == 0) continue; recalc(); qi = ri; qj = rj; ans = 1; while (true) { ti = gi(qi, qj); tj = gj(qi, qj); dodel(qi, qj); if (outside(ti, tj)) break; qi = ti; qj = tj; ++ans; } if (ans >= bst) { if (ans == bst) bc++; else bc = 1; bst = ans; } } cout << bst << " " << bc << endl; cin.get(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; int up[10010], dw[10010], rt[10010], lt[10010]; int upp[10010], dww[10010], rtt[10010], ltt[10010]; char mp[5005][5005]; int n, m; void init() { int i, j, k; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { up[i * m + j] = upp[i * m + j]; dw[i * m + j] = dww[i * m + j]; rt[i * m + j] = rtt[i * m + j]; lt[i * m + j] = ltt[i * m + j]; } } void delet(int k) { if (dw[k] > 0) up[dw[k]] = up[k]; if (up[k] > 0) dw[up[k]] = dw[k]; if (rt[k] > 0) lt[rt[k]] = lt[k]; if (lt[k] > 0) rt[lt[k]] = rt[k]; } int main() { int i, j, k; int len, ans, num; while (scanf("%d%d", &n, &m) != EOF) { for (i = 1; i <= n; i++) { scanf("%s", &mp[i][1]); } memset(up, -1, sizeof(up)); memset(dw, -1, sizeof(dw)); memset(rt, -1, sizeof(rt)); memset(lt, -1, sizeof(lt)); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { for (k = i - 1; k >= 1; k--) if (mp[k][j] != '.') { up[i * m + j] = k * m + j; break; } for (k = i + 1; k <= n; k++) if (mp[k][j] != '.') { dw[i * m + j] = k * m + j; break; } for (k = j - 1; k >= 1; k--) if (mp[i][k] != '.') { lt[i * m + j] = i * m + k; break; } for (k = j + 1; k <= m; k++) if (mp[i][k] != '.') { rt[i * m + j] = i * m + k; break; } } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { upp[i * m + j] = up[i * m + j]; dww[i * m + j] = dw[i * m + j]; rtt[i * m + j] = rt[i * m + j]; ltt[i * m + j] = lt[i * m + j]; } ans = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { if (mp[i][j] == '.') continue; len = 0; init(); int x = i, y = j; while (1) { k = x * m + y; len++; if (mp[x][y] == 'U' && up[k] > 0) x = up[k] / m; else if (mp[x][y] == 'D' && dw[k] > 0) x = dw[k] / m; else if (mp[x][y] == 'L' && lt[k] > 0) y = lt[k] % m; else if (mp[x][y] == 'R' && rt[k] > 0) y = rt[k] % m; else break; if (y == m) x--; if (y == 0) y = m; delet(k); } if (len > ans) { ans = len; num = 1; } else if (len == ans) num++; } printf("%d %d\n", ans, num); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, M, Tot, Ans, Count; char Map[5005][5005]; int Mark[5005][5005]; char Arrow[5005]; int LPrev[5005], LNext[5005], RPrev[5005], RNext[5005]; int LPCpy[5005], LNCpy[5005], RPCpy[5005], RNCpy[5005]; inline void Update(int Val) { if (Val > Ans) { Ans = Val; Count = 0; } if (Val == Ans) ++Count; } inline int Goes(int V) { memcpy(LPrev, LPCpy, sizeof(LPrev)); memcpy(LNext, LNCpy, sizeof(LNext)); memcpy(RPrev, RPCpy, sizeof(RPrev)); memcpy(RNext, RNCpy, sizeof(RNext)); int Result = 0; for (int W = 0; V != 0; V = W, W = 0, ++Result) { if (Arrow[V] == 'L') W = LPrev[V]; if (Arrow[V] == 'R') W = LNext[V]; if (Arrow[V] == 'U') W = RPrev[V]; if (Arrow[V] == 'D') W = RNext[V]; LPrev[LNext[V]] = LPrev[V]; LNext[LPrev[V]] = LNext[V]; RPrev[RNext[V]] = RPrev[V]; RNext[RPrev[V]] = RNext[V]; } return Result; } int main() { scanf("%d%d", &N, &M); Tot = 0; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) { char Ch = getchar(); while (Ch != '.' && !isupper(Ch)) Ch = getchar(); Map[i][j] = Ch; if (isupper(Ch)) { Mark[i][j] = ++Tot; Arrow[Tot] = Ch; } } for (int i = 1; i <= N; ++i) { int Last = 0; for (int j = 1; j <= M; ++j) if (isupper(Map[i][j])) { LPCpy[Mark[i][j]] = Last; LNCpy[Last] = Mark[i][j]; Last = Mark[i][j]; } LNCpy[Last] = 0; } for (int j = 1; j <= M; ++j) { int Last = 0; for (int i = 1; i <= N; ++i) if (isupper(Map[i][j])) { RPCpy[Mark[i][j]] = Last; RNCpy[Last] = Mark[i][j]; Last = Mark[i][j]; } RNCpy[Last] = 0; } Ans = 0, Count = 0; for (int i = 1; i <= Tot; ++i) Update(Goes(i)); printf("%d %d\n", Ans, Count); return 0; }
#include <bits/stdc++.h> using std::abs; using std::bitset; using std::cerr; using std::cin; using std::cout; using std::map; using std::max; using std::min; using std::pair; using std::set; using std::sort; using std::string; using std::swap; using std::unordered_map; using std::unordered_set; using std::vector; using ll = long long; using uint = unsigned int; using pii = pair<int, int>; using pll = pair<ll, ll>; using ull = unsigned long long; using ld = long double; template <typename T> void _dbg(const char* s, T h) { cerr << s << " = " << h << "\n"; } template <typename T, typename... Ts> void _dbg(const char* s, T h, Ts... t) { int b = 0; while (((b += *s == '(') -= *s == ')') != 0 || *s != ',') cerr << *s++; cerr << " = " << h << ","; _dbg(s + 1, t...); } struct init { init() { cin.tie(0); std::iostream::sync_with_stdio(0); cout << std::fixed << std::setprecision(10); cerr << std::fixed << std::setprecision(5); } ~init() {} } init; template <typename T, typename U> void upx(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> void upn(T& x, U y) { if (x > y) x = y; } const ll N = 5000; struct kal { char go; ll u = -1, d = -1, l = -1, r = -1; } v[N]; char a[N][N]; ll first[N][N]; kal second[N]; void remove(ll x) { if (second[x].u != -1) second[second[x].u].d = second[x].d; if (second[x].d != -1) second[second[x].d].u = second[x].u; if (second[x].r != -1) second[second[x].r].l = second[x].l; if (second[x].l != -1) second[second[x].l].r = second[x].r; } int32_t main() { ll n, m; cin >> n >> m; ll z = 0; for (ll i = 0; i < (n); ++i) for (ll j = 0; j < (m); ++j) { cin >> a[i][j]; if (a[i][j] != '.') { v[z].go = a[i][j]; first[i][j] = z++; } } for (ll i = 0; i < (n); ++i) for (ll j = 0; j < (m); ++j) { if (a[i][j] == '.') continue; for (ll k = i + 1; v[first[i][j]].d == -1 && k < n; ++k) if (a[k][j] != '.') v[first[i][j]].d = first[k][j]; for (ll k = i - 1; v[first[i][j]].u == -1 && k >= 0; --k) if (a[k][j] != '.') v[first[i][j]].u = first[k][j]; for (ll k = j + 1; v[first[i][j]].r == -1 && k < m; ++k) if (a[i][k] != '.') v[first[i][j]].r = first[i][k]; for (ll k = j - 1; v[first[i][j]].l == -1 && k >= 0; --k) if (a[i][k] != '.') v[first[i][j]].l = first[i][k]; } ll mx = 0, cmx = 0; for (ll i = 0; i < (z); ++i) { for (ll j = 0; j < (z); ++j) second[j] = v[j]; ll u = i, cur = 0; while (u >= 0) { cur++; ll go; if (second[u].go == 'U') go = second[u].u; if (second[u].go == 'D') go = second[u].d; if (second[u].go == 'L') go = second[u].l; if (second[u].go == 'R') go = second[u].r; remove(u); u = go; } if (cur > mx) mx = cur, cmx = 0; if (mx == cur) cmx++; } cout << mx << ' ' << cmx << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; string s[8002]; int l[8002][6]; int n, m, t; int a[8002]; char *pat = "LRUD."; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; bool vis[8002]; int cnt[8002]; int go(int t) { int x = t / m, y = t % m, res = 0; while (1) { ++res; int nxt = l[t][a[t]]; if (nxt == -1) break; for (int i = 0; i < 4; i++) { if (l[t][i ^ 1] == -1) continue; l[l[t][i ^ 1]][i] = l[t][i]; } t = nxt; } return res; } int main() { int i, j, k; scanf("%d%d", &n, &m); t = n * m; for (i = 0; i < n; i++) { cin >> s[i]; for (j = 0; j < m; j++) { a[i * m + j] = strchr(pat, s[i][j]) - pat; } } int mx = 0, cnt = 0; for (k = 0; k < t; k++) { if (a[k] == 4) continue; memset(l, -1, sizeof(l)); for (i = 0; i < t; i++) { if (a[i] == 4) continue; for (j = 0; j < 4; j++) { int x = i / m, y = i % m; while (1) { x += dx[j]; y += dy[j]; if (x < 0 || x >= n || y < 0 || y >= m) break; if (a[x * m + y] == 4) continue; l[i][j] = x * m + y; break; } } } int tt = go(k); if (tt > mx) { mx = tt; cnt = 1; } else if (tt == mx) { cnt++; } } printf("%d %d\n", mx, cnt); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int inf = 0x7f7f7f7f; const int N = 20000 + 5; const int mod = 1e9 + 7; char M[5007][5007]; vector<int> ans; int L[N], R[N], U[N], D[N], _L[N], _R[N], _U[N], _D[N]; int n, m, flag; void init() { for (int i = (1); i < (n + 1); i++) for (int j = (1); j < (m + 2); j++) _L[i * (m + 2) + j] = j - 1; for (int i = (1); i < (n + 1); i++) for (int j = (0); j < (m + 1); j++) _R[i * (m + 2) + j] = j + 1; for (int i = (1); i < (n + 2); i++) for (int j = (1); j < (m + 1); j++) _U[i * (m + 2) + j] = i - 1; for (int i = (0); i < (n + 1); i++) for (int j = (1); j < (m + 1); j++) _D[i * (m + 2) + j] = i + 1; for (int i = (1); i < (n + 1); i++) for (int j = (1); j < (m + 1); j++) { if (M[i][j] == '.') { _L[i * (m + 2) + _R[i * (m + 2) + j]] = _L[i * (m + 2) + j]; _R[i * (m + 2) + _L[i * (m + 2) + j]] = _R[i * (m + 2) + j]; _U[_D[i * (m + 2) + j] * (m + 2) + j] = _U[i * (m + 2) + j]; _D[_U[i * (m + 2) + j] * (m + 2) + j] = _D[i * (m + 2) + j]; } } for (int i = (1); i < (n + 1); i++) for (int j = (1); j < (m + 2); j++) L[i * (m + 2) + j] = _L[i * (m + 2) + j]; for (int i = (1); i < (n + 1); i++) for (int j = (0); j < (m + 1); j++) R[i * (m + 2) + j] = _R[i * (m + 2) + j]; for (int i = (1); i < (n + 2); i++) for (int j = (1); j < (m + 1); j++) U[i * (m + 2) + j] = _U[i * (m + 2) + j]; for (int i = (0); i < (n + 1); i++) for (int j = (1); j < (m + 1); j++) D[i * (m + 2) + j] = _D[i * (m + 2) + j]; } vector<int> xl, xr, xd, xu; void gao(int x, int y) { int cnt = 0; xl.clear(); xr.clear(); xd.clear(); xu.clear(); while (x && y && x <= n && y <= m) { cnt++; xl.push_back(x * (m + 2) + R[x * (m + 2) + y]); xr.push_back(x * (m + 2) + L[x * (m + 2) + y]); xu.push_back(D[x * (m + 2) + y] * (m + 2) + y); xd.push_back(U[x * (m + 2) + y] * (m + 2) + y); L[x * (m + 2) + R[x * (m + 2) + y]] = L[x * (m + 2) + y]; R[x * (m + 2) + L[x * (m + 2) + y]] = R[x * (m + 2) + y]; U[D[x * (m + 2) + y] * (m + 2) + y] = U[x * (m + 2) + y]; D[U[x * (m + 2) + y] * (m + 2) + y] = D[x * (m + 2) + y]; if (M[x][y] == 'L') { y = L[x * (m + 2) + y]; } else if (M[x][y] == 'R') { y = R[x * (m + 2) + y]; } else if (M[x][y] == 'U') { x = U[x * (m + 2) + y]; } else if (M[x][y] == 'D') { x = D[x * (m + 2) + y]; } } if (cnt) ans.push_back(cnt); if (1) { for (int i = (0); i < (xl.size()); i++) L[xl[i]] = _L[xl[i]]; for (int i = (0); i < (xr.size()); i++) R[xr[i]] = _R[xr[i]]; for (int i = (0); i < (xu.size()); i++) U[xu[i]] = _U[xu[i]]; for (int i = (0); i < (xd.size()); i++) D[xd[i]] = _D[xd[i]]; } } int main() { scanf("%d%d", &n, &m); memset(M, 0, sizeof(M)); for (int i = (1); i < (n + 1); i++) scanf("%s", M[i] + 1); init(); long long all = 0; for (int i = (1); i < (n + 1); i++) for (int j = (1); j < (m + 1); j++) { if (M[i][j] != '.') gao(i, j); all += flag; } sort(ans.begin(), ans.end()); int mx = ans[(int)ans.size() - 1]; printf("%d %d\n", mx, (int)ans.size() - (lower_bound(ans.begin(), ans.end(), mx) - ans.begin())); }
#include <bits/stdc++.h> using namespace std; int n, m, cur, id[5010][5010], l[5010], r[5010], u[5010], d[5010], L[5010], R[5010], U[5010], D[5010], a1, a2; char c[5010][5010], dir[5010]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) scanf("%s", c[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (c[i][j] != '.') { id[i][j] = ++cur; dir[cur] = c[i][j]; } } for (int i = 0; i < n; i++) { int pre = 0; for (int j = 0; j < m; j++) if (c[i][j] != '.') L[id[i][j]] = pre, R[pre] = id[i][j], pre = id[i][j]; } for (int j = 0; j < m; j++) { int pre = 0; for (int i = 0; i < n; i++) if (c[i][j] != '.') U[id[i][j]] = pre, D[pre] = id[i][j], pre = id[i][j]; } for (int i = 1; i < cur + 1; i++) { for (int j = 1; j < cur + 1; j++) l[j] = L[j], r[j] = R[j], u[j] = U[j], d[j] = D[j]; int p = 0, now = i; while (now) { p++; l[r[now]] = l[now]; r[l[now]] = r[now]; u[d[now]] = u[now]; d[u[now]] = d[now]; if (dir[now] == 'L') now = l[now]; else if (dir[now] == 'R') now = r[now]; else if (dir[now] == 'U') now = u[now]; else now = d[now]; } if (p > a1) a1 = p, a2 = 1; else if (p == a1) a2++; } cout << a1 << " " << a2; }
#include <bits/stdc++.h> using namespace std; int d[4]; int nx[5555][4], pr[5555][4]; int cn[5555][4], cp[5555][4]; int a[5555]; int n, m; inline int go(int k, int l) { if (nx[k][l] != -2) return nx[k][l]; if (k + d[l] >= n * m || k + d[l] < 0 || (l & 1 && (k + d[l]) / m != k / m)) return nx[k][l] = -1; else if (a[k + d[l]] != -1) return nx[k][l] = k + d[l]; else return nx[k][l] = go(k + d[l], l); } inline int gop(int k, int l) { if (pr[k][l] != -2) return pr[k][l]; if (k - d[l] >= n * m || k - d[l] < 0 || (l & 1 && (k - d[l]) / m != k / m)) return pr[k][l] = -1; else if (a[k - d[l]] != -1) return pr[k][l] = k - d[l]; else return pr[k][l] = gop(k - d[l], l); } int main() { cin >> n >> m; d[0] = -m; d[1] = 1; d[2] = m; d[3] = -1; for (int(i) = 0; (i) < (n * m); ++(i)) { char ch; cin >> ch; if (ch == '.') a[i] = -1; else if (ch == 'U') a[i] = 0; else if (ch == 'R') a[i] = 1; else if (ch == 'D') a[i] = 2; else a[i] = 3; for (int(l) = 0; (l) < (4); ++(l)) nx[i][l] = pr[i][l] = -2; } for (int(i) = 0; (i) < (n * m); ++(i)) for (int(l) = 0; (l) < (4); ++(l)) if (nx[i][l] == -2) go(i, l); for (int(i) = 0; (i) < (n * m); ++(i)) for (int(l) = 0; (l) < (4); ++(l)) if (pr[i][l] == -2) gop(i, l); int ans = 0; int cnt = 0; for (int(i) = 0; (i) < (n * m); ++(i)) if (a[i] != -1) { memcpy(cn, nx, sizeof(cn)); memcpy(cp, pr, sizeof(cp)); int j = i; int cur = 0; while (j != -1) { cur++; for (int(dir) = 0; (dir) < (4); ++(dir)) { int pp = cp[j][dir]; int nn = cn[j][dir]; if (~pp) cn[pp][dir] = nn; if (~nn) cp[nn][dir] = pp; } j = cn[j][a[j]]; } if (cur > ans) { ans = cur; cnt = 1; } else if (cur == ans) cnt++; } cout << ans << " " << cnt << endl; }
#include <bits/stdc++.h> using namespace std; struct p { int L, R, U, D; } point[5005], pos[5005]; string g[5005]; void DEL(int x) { pos[pos[x].D].U = pos[x].U; pos[pos[x].U].D = pos[x].D; pos[pos[x].R].L = pos[x].L; pos[pos[x].L].R = pos[x].R; } int main() { int m, n, i, j, l, sx, sy; scanf("%d%d", &m, &n); for (i = 0; i < m; i++) cin >> g[i]; n = g[0].size(); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (g[i][j] != '.') { for (l = i - 1; l >= 0; l--) if (g[l][j] != '.') break; if (l < 0) point[i * n + j].U = -1; else point[i * n + j].U = l * n + j; for (l = i + 1; l < m; l++) if (g[l][j] != '.') break; if (l >= m) point[i * n + j].D = -1; else point[i * n + j].D = l * n + j; for (l = j - 1; l >= 0; l--) if (g[i][l] != '.') break; if (l < 0) point[i * n + j].L = -1; else point[i * n + j].L = i * n + l; for (l = j + 1; l < n; l++) if (g[i][l] != '.') break; if (l >= n) point[i * n + j].R = -1; else point[i * n + j].R = i * n + l; } } } int r1 = 0, r2 = 0, cnt, t, x, y; for (sx = 0; sx < m; sx++) { for (sy = 0; sy < n; sy++) if (g[sx][sy] != '.') { for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { pos[i * n + j] = point[i * n + j]; } } if (g[sx][sy] == 'D') t = pos[sx * n + sy].D; else if (g[sx][sy] == 'U') t = pos[sx * n + sy].U; else if (g[sx][sy] == 'R') t = pos[sx * n + sy].R; else if (g[sx][sy] == 'L') t = pos[sx * n + sy].L; DEL(sx * n + sy); cnt = 1; while (t != -1) { cnt++; x = t / n; y = t % n; DEL(t); if (g[x][y] == 'D') t = pos[t].D; else if (g[x][y] == 'U') t = pos[t].U; else if (g[x][y] == 'R') t = pos[t].R; else if (g[x][y] == 'L') t = pos[t].L; } if (cnt > r1) { r1 = cnt; r2 = 1; } else if (cnt == r1) { r2++; } } } printf("%d %d\n", r1, r2); }
#include <bits/stdc++.h> using namespace std; char **g; int n, m; struct nodes { nodes *d[4]; char dir; }; nodes **dl; template <class T> void getarr(T **&a) { a = (T **)malloc((n + 10) * sizeof(T *)); for (int i = 0; i < n + 10; i++) { a[i] = (T *)malloc((m + 10) * sizeof(T)); } } void del(nodes *p) { for (int i = 0; i < 4; i++) { p->d[i]->d[(i + 2) % 4] = p->d[(i + 2) % 4]; } } int to[300]; int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; void init() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i != 0 && j != 0) dl[i][j].dir = to[g[i][j]]; else dl[i][j].dir = -1; for (int d = 0; d < 4; d++) { dl[i][j].d[d] = &dl[(i + dir[d][0] + n + 1) % (n + 1)] [(j + dir[d][1] + m + 1) % (m + 1)]; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] == '.') del(&dl[i][j]); } } } int solve(int x, int y) { nodes *ptr = &dl[x][y]; int cc = 0; while (ptr->dir != -1) { del(ptr); cc++; ptr = ptr->d[ptr->dir]; } return cc; } int main() { to['U'] = 0; to['R'] = 1; to['D'] = 2; to['L'] = 3; scanf("%d%d", &n, &m); getarr(g); getarr(dl); for (int i = 1; i <= n; i++) { scanf("%s", g[i] + 1); } int ans1 = -1, ans2 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] != '.') { init(); int ret = solve(i, j); if (ans1 < ret) { ans1 = ret; ans2 = 1; } else if (ans1 == ret) { ans2++; } } } } printf("%d %d\n", ans1, ans2); return 0; }
#include <bits/stdc++.h> const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, -1, 0, 1}; using namespace std; char str[5001]; class Point { public: int Left; int Right; int Up; int Down; }; Point f1[5001], f2[5001]; int ans1, ans2, n, m; int dfs(int p, int tot) { int tmp; if (str[p] == 'D') tmp = f2[p].Down; else if (str[p] == 'U') tmp = f2[p].Up; else if (str[p] == 'L') tmp = f2[p].Left; else tmp = f2[p].Right; if (f2[p].Left != -1) f2[f2[p].Left].Right = f2[p].Right; if (f2[p].Right != -1) f2[f2[p].Right].Left = f2[p].Left; if (f2[p].Up != -1) f2[f2[p].Up].Down = f2[p].Down; if (f2[p].Down != -1) f2[f2[p].Down].Up = f2[p].Up; if (ans1 == tot) ans2++; if (ans1 < tot) { ans1 = tot; ans2 = 1; } if (tmp != -1) dfs(tmp, tot + 1); return 0; } int work() { scanf("%d%d", &n, &m); getchar(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) str[i * m + j] = getchar(); getchar(); } memset(f1, -1, sizeof(f1)); memset(f2, -1, sizeof(f2)); int k; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { int p = i * m + j; if (str[p] == '.') continue; { k = i + 1; while (str[k * m + j] == '.' && k < n) k++; if (k < n) { f1[p].Down = k * m + j; f1[k * m + j].Up = p; } } { k = i - 1; while (str[k * m + j] == '.' && k >= 0) k--; if (k >= 0) { f1[p].Up = k * m + j; f1[k * m + j].Down = p; } } { k = j - 1; while (str[i * m + k] == '.' && k >= 0) k--; if (k >= 0) { f1[p].Left = i * m + k; f1[i * m + k].Right = p; } } { k = j + 1; while (str[i * m + k] == '.' && k < m) k++; if (k < m) { f1[p].Right = i * m + k; f1[i * m + k].Left = p; } } } ans1 = ans2 = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (str[i * m + j] != '.') { for (int k = 0; k < 5001; ++k) f2[k] = f1[k]; dfs(i * m + j, 1); } printf("%d %d\n", ans1, ans2); return 0; } int main() { work(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; int n, m, cnt; int u[MAXN], d[MAXN], l[MAXN], r[MAXN]; char c[MAXN]; string mat[MAXN]; bool check(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); } int id(int x, int y) { return check(x, y) ? x * m + y : -1; } void add(int k) { u[d[k]] = k; d[u[k]] = k; l[r[k]] = k; r[l[k]] = k; } void del(int k) { u[d[k]] = u[k]; d[u[k]] = d[k]; l[r[k]] = l[k]; r[l[k]] = r[k]; } void build() { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int k = id(i, j); u[k] = id(i - 1, j); d[k] = id(i + 1, j); l[k] = id(i, j - 1); r[k] = id(i, j + 1); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int k = id(i, j); if (c[k] == '.') { del(k); } } } } int gao(int k) { if (k == -1) return 0; int next = -1; if (c[k] == 'U') next = u[k]; if (c[k] == 'D') next = d[k]; if (c[k] == 'L') next = l[k]; if (c[k] == 'R') next = r[k]; del(k); int mov = gao(next) + 1; add(k); return mov; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> mat[i]; for (int j = 0; j < m; ++j) { c[id(i, j)] = mat[i][j]; } } build(); int ans = 0, cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (mat[i][j] != '.') { int tmp = gao(id(i, j)); if (ans < tmp) ans = tmp, cnt = 1; else if (ans == tmp) ++cnt; } } } cout << ans << " " << cnt << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 20000; struct node { int flow; int ff[5]; int ru; } mat[maxn]; int n, m; int kk[500]; int now; int getup(int id) { int ret = -1; int k = id - m; while (k >= 1) { if (mat[k].flow != -1) { ret = k; break; } k -= m; } return ret; } int getright(int id) { int ret = -1; int k = id; while (k % m != 0) { k = k + 1; if (mat[k].flow != -1) { ret = k; break; } } return ret; } int getdown(int id) { int ret = -1; int k = id + m; while (k <= n * m) { if (mat[k].flow != -1) { ret = k; break; } k += m; } return ret; } int getleft(int id) { int ret = -1; int k = id; while (k % m != 1) { k--; if (mat[k].flow != -1) { ret = k; break; } } return ret; } int fl[maxn][5]; int ans; int cnt; int solve(int id) { int ret = 0; int k = id; int ne; int u, d, r, l; while (k != -1) { ret++; ne = mat[k].ff[mat[k].flow]; u = mat[k].ff[1]; d = mat[k].ff[3]; r = mat[k].ff[2]; l = mat[k].ff[4]; if (u != -1) { mat[u].ff[3] = d; } if (d != -1) { mat[d].ff[1] = u; } if (r != -1) { mat[r].ff[4] = l; } if (l != -1) { mat[l].ff[2] = r; } k = ne; } return ret; } int main() { kk['U'] = 1; kk['R'] = 2; kk['D'] = 3; kk['L'] = 4; kk['.'] = -1; now = 0; cin >> n >> m; string s; for (int i = 1; i <= n; i++) { cin >> s; for (int j = 0; j < m; j++) { mat[++now].flow = kk[s[j]]; } } for (int i = 1; i <= now; i++) { if (mat[i].flow == -1) continue; if (n != 1) fl[i][1] = getup(i); if (m != 1) fl[i][2] = getright(i); if (n != 1) fl[i][3] = getdown(i); if (m != 1) fl[i][4] = getleft(i); if (m == 1) { fl[i][2] = fl[i][4] = -1; } if (n == 1) { fl[i][1] = fl[i][3] = -1; } } for (int i = 1; i <= now; i++) { int ne = fl[i][mat[i].flow]; if (ne != -1) { int nne = fl[ne][mat[ne].flow]; if (nne != i) ; mat[ne].ru = 1; } } ans = 0; for (int i = 1; i <= now; i++) { if (mat[i].flow == -1) continue; for (int i1 = 1; i1 <= now; i1++) { for (int j = 1; j <= 4; j++) { mat[i1].ff[j] = fl[i1][j]; } } int z = solve(i); if (z > ans) { ans = z; cnt = 1; } else if (z == ans) { cnt++; } } cout << ans << " " << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct point { point *l, *r, *u, *d; } p[5010], q[5010], *ii, *jj; int i, j, n, m; int num = 0; char s[5010]; inline void rm(point* pp) { if (pp->l) { pp->l->r = pp->r; } if (pp->r) { pp->r->l = pp->l; } if (pp->d) { pp->d->u = pp->u; } if (pp->u) { pp->u->d = pp->d; } } int main() { scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &s[num]); if (i) { assert(num >= m); p[num].u = &p[num - m]; p[num - m].d = &p[num]; } if (j) { p[num].l = &p[num - 1]; p[num - 1].r = &p[num]; } num++; } } for (i = 0; i < num; i++) { if (s[i] == '.') { rm(&p[i]); } } int ans = 0, ansnum = 0, now; memcpy(q, p, sizeof(p)); for (i = 0; i < num; i++) { if (s[i] == '.') { continue; } if (i) memcpy(p, q, sizeof(q)); ii = &p[i]; now = 0; while (ii) { now++; rm(ii); switch (s[ii - p]) { case 'D': ii = ii->d; break; case 'U': ii = ii->u; break; case 'L': ii = ii->l; break; case 'R': ii = ii->r; break; default: assert(0); } } if (now > ans) { ans = now; ansnum = 1; } else if (now == ans) { ansnum++; } } printf("%d %d\n", ans, ansnum); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; unordered_map<char, int> c_to_idx{{'L', 0}, {'U', 1}, {'R', 2}, {'D', 3}}; int is_ok(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } struct Node { public: pair<int, int> cur; pair<int, int> nxt_dir[4]; Node(int x, int y) : cur{x, y} { for (int ctr1 = 0; ctr1 < 4; ++ctr1) nxt_dir[ctr1] = {x + dir[ctr1][0], y + dir[ctr1][1]}; } void expand(vector<vector<Node>>& mapa) { for (int ctr1 = 0; ctr1 < 4; ++ctr1) if (is_ok(nxt_dir[ctr1].first, nxt_dir[ctr1].second)) mapa[nxt_dir[ctr1].first][nxt_dir[ctr1].second] .nxt_dir[(ctr1 + 2) % 4] = nxt_dir[(ctr1 + 2) % 4]; } void compress(vector<vector<Node>>& mapa) { for (int ctr1 = 0; ctr1 < 4; ++ctr1) if (is_ok(nxt_dir[ctr1].first, nxt_dir[ctr1].second)) mapa[nxt_dir[ctr1].first][nxt_dir[ctr1].second] .nxt_dir[(ctr1 + 2) % 4] = cur; } }; vector<vector<Node>> mat; vector<string> mapa; int count(pair<int, int> a) { pair<int, int> nxt = mat[a.first][a.second].nxt_dir[c_to_idx[mapa[a.first][a.second]]]; if (!is_ok(nxt.first, nxt.second)) return 1; mat[a.first][a.second].expand(mat); int rez = 1 + count(nxt); mat[a.first][a.second].compress(mat); return rez; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; string s; for (int ctr1 = 0; ctr1 < n; ++ctr1) cin >> s, mapa.push_back(s); for (int ctr1 = 0; ctr1 < n; ++ctr1) { vector<Node> tmp; for (int ctr2 = 0; ctr2 < m; ++ctr2) tmp.emplace_back(ctr1, ctr2); mat.push_back(tmp); } for (int ctr1 = 0; ctr1 < n; ++ctr1) { for (int ctr2 = 0; ctr2 < m; ++ctr2) { if (mapa[ctr1][ctr2] == '.') mat[ctr1][ctr2].expand(mat); } } pair<int, int> rez = {0, 0}; for (int ctr1 = 0; ctr1 < n; ++ctr1) { for (int ctr2 = 0; ctr2 < m; ++ctr2) { if (mapa[ctr1][ctr2] != '.') { int c_rez = count({ctr1, ctr2}); if (c_rez == rez.first) ++rez.second; else if (c_rez > rez.first) rez = {c_rez, 1}; } } } cout << rez.first << ' ' << rez.second << '\n'; return 0; }
#include <bits/stdc++.h> int n, m; char buf[5001]; char input[5001]; int left[5001]; int right[5001]; int up[5001]; int down[5001]; int getIndex(int r, int c) { return r * m + c; } void setLinklist() { memset(left, -1, sizeof(left)); memset(right, -1, sizeof(right)); memset(up, -1, sizeof(up)); memset(down, -1, sizeof(down)); for (int r = 0; r < n; ++r) { int prevIdx = -1; for (int c = 0; c < m; ++c) { int currIdx = getIndex(r, c); if (buf[currIdx] != '.') { if (prevIdx != -1) { right[prevIdx] = currIdx; left[currIdx] = prevIdx; } prevIdx = currIdx; } } } for (int c = 0; c < m; ++c) { int prevIdx = -1; for (int r = 0; r < n; ++r) { int currIdx = getIndex(r, c); if (buf[currIdx] != '.') { if (prevIdx != -1) { down[prevIdx] = currIdx; up[currIdx] = prevIdx; } prevIdx = currIdx; } } } } void remove(int idx) { if (left[idx] != -1) { right[left[idx]] = right[idx]; } if (right[idx] != -1) { left[right[idx]] = left[idx]; } if (up[idx] != -1) { down[up[idx]] = down[idx]; } if (down[idx] != -1) { up[down[idx]] = up[idx]; } } void add(int idx) { if (left[idx] != -1) { right[left[idx]] = idx; } if (right[idx] != -1) { left[right[idx]] = idx; } if (up[idx] != -1) { down[up[idx]] = idx; } if (down[idx] != -1) { up[down[idx]] = idx; } } int dfs(int idx) { if (idx == -1) { return 0; } int res = 1; remove(idx); if (buf[idx] == 'L') { res += dfs(left[idx]); } else if (buf[idx] == 'R') { res += dfs(right[idx]); } else if (buf[idx] == 'U') { res += dfs(up[idx]); } else if (buf[idx] == 'D') { res += dfs(down[idx]); } add(idx); return res; } int main() { while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < n; ++i) { scanf("%s", input); for (int j = 0; j < m; ++j) { buf[getIndex(i, j)] = input[j]; } } setLinklist(); int res = 0; int cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int idx = getIndex(i, j); if (buf[idx] == '.') { continue; } int tmp = dfs(idx); if (tmp > res) { res = tmp; cnt = 1; } else if (tmp == res) { cnt++; } } } printf("%d %d\n", res, cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5010; int n, m; int pnt[N * 4], last; char tab[N][N], a[N][N]; int find(int x) { if (x != pnt[x]) pnt[x] = find(pnt[x]); return pnt[x]; } int id(int x, int y, int d) { return (x * m + y) * 4 + d; } void add(int x, int y) { pnt[id(x, y, 0)] = x - 1 >= 0 ? id(x - 1, y, 0) : last; pnt[id(x, y, 1)] = x + 1 < n ? id(x + 1, y, 1) : last; pnt[id(x, y, 2)] = y - 1 >= 0 ? id(x, y - 1, 2) : last; pnt[id(x, y, 3)] = y + 1 < m ? id(x, y + 1, 3) : last; } void solve() { int ans = 0, cnt; last = n * m * 4; for (int i = 0; i < n; ++i) scanf("%s", tab[i]); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (tab[i][j] == '.') continue; for (int u = 0; u <= last; ++u) pnt[u] = u; for (int u = 0; u < n; ++u) for (int v = 0; v < m; ++v) { a[u][v] = tab[u][v]; if (a[u][v] == '.') add(u, v); } int x = i, y = j, t = 0; while (1) { t++; int dir; if (a[x][y] == 'U') dir = 0; if (a[x][y] == 'D') dir = 1; if (a[x][y] == 'L') dir = 2; if (a[x][y] == 'R') dir = 3; add(x, y); int p = find(id(x, y, dir)); if (p == last) break; x = p / 4 / m; y = p / 4 % m; } if (ans < t) ans = t, cnt = 1; else if (ans == t) cnt++; } printf("%d %d\n", ans, cnt); } int main() { while (scanf("%d%d", &n, &m) != EOF) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const unsigned long long MOD = 257; int n, m; string s[5005]; int L[5005], R[5005], U[5005], D[5005]; void init() { int b, pos; for (int i = 0; i < n; i++) { b = -1; for (int j = 0; j < m; j++) { if (s[i][j] != '.') { pos = i * m + j; L[pos] = b; if (b != -1) R[b] = pos; b = pos; } } R[b] = -1; } for (int i = 0; i < m; i++) { b = -1; for (int j = 0; j < n; j++) { if (s[j][i] != '.') { pos = j * m + i; U[pos] = b; if (b != -1) { D[b] = pos; } b = pos; } } D[b] = -1; } } int dfs(int now) { int x = now / m, y = now % m; if (L[now] != -1) R[L[now]] = R[now]; if (R[now] != -1) L[R[now]] = L[now]; if (U[now] != -1) D[U[now]] = D[now]; if (D[now] != -1) U[D[now]] = U[now]; int go; if (s[x][y] == 'U') go = U[now]; else if (s[x][y] == 'D') go = D[now]; else if (s[x][y] == 'L') go = L[now]; else if (s[x][y] == 'R') go = R[now]; if (go == -1) return 1; return dfs(go) + 1; } int main() { std::ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } int ans = 0, num = 0, tmp; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] != '.') { init(); tmp = dfs(i * m + j); if (tmp == ans) num++; else if (tmp > ans) { ans = tmp; num = 1; } } } } cout << ans << " " << num << endl; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; int n, m; char tbl[5010][5010]; int dir[5010][5010]; int lnk[5010][4]; int h(int x, int y) { return x * m + y + 1; } int cal_x(int p) { return (p - 1) / m; } int cal_y(int p) { return (p - 1) % m; } bool valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } void del(int x, int y) { for (int i = 0; i < 4; i++) { lnk[lnk[h(x, y)][i]][i ^ 1] = lnk[h(x, y)][i ^ 1]; } } void add(int x, int y) { for (int i = 0; i < 4; i++) { lnk[lnk[h(x, y)][i]][i ^ 1] = h(x, y); } } int go(int p) { if (p == 0) return 0; int x = cal_x(p); int y = cal_y(p); int d = dir[x][y]; if (d == -1) return 0; del(x, y); int ret = go(lnk[h(x, y)][d]) + 1; add(x, y); return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", tbl[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { dir[i][j] = -1; if (tbl[i][j] == 'U') dir[i][j] = 0; if (tbl[i][j] == 'D') dir[i][j] = 1; if (tbl[i][j] == 'L') dir[i][j] = 2; if (tbl[i][j] == 'R') dir[i][j] = 3; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { for (int k = 0; k < 4; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (!valid(nx, ny)) continue; lnk[h(i, j)][k] = h(nx, ny); } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (dir[i][j] == -1) del(i, j); int ans = -1, cnt = -1; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (dir[i][j] == -1) continue; int now = go(h(i, j)); if (now > ans) { ans = now; cnt = 1; } else if (now == ans) cnt++; } printf("%d %d\n", ans, cnt); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5036; bool vis[N][N]; char s[N][N]; short pr[2][N][N]; short ne[2][N][N]; int n, m; void ER(int first, int second, char c) { if (vis[first][second]) return; vis[first][second] = 1; { int v1 = pr[0][first][second]; int v2 = ne[0][first][second]; if (v2 != -1) pr[0][first][v2] = v1; if (v1 != -1) ne[0][first][v1] = v2; } { int u1 = pr[1][first][second]; int u2 = ne[1][first][second]; if (u2 != -1) pr[1][u2][second] = u1; if (u1 != -1) ne[1][u1][second] = u2; } } void LK(int first, int second, int a, int b) { if (first == a) { ne[0][first][second] = b; pr[0][first][b] = second; } else { ne[1][first][second] = a; pr[1][a][second] = first; } } void init() { for (int k = (0); k < (int)(2); k++) for (int i = (0); i < (int)(n); i++) for (int j = (0); j < (int)(m); j++) { pr[k][i][j] = -1; ne[k][i][j] = -1; vis[i][j] = 0; } for (int first = (0); first < (int)(n); first++) { int second = 0; while (second < m && s[first][second] == '.') ++second; for (; second < m;) { int v = second + 1; while (v < m && s[first][v] == '.') ++v; if (v >= m) break; LK(first, second, first, v); second = v; } } for (int second = (0); second < (int)(m); second++) { int first = 0; while (first < n && s[first][second] == '.') ++first; for (; first < n;) { int u = first + 1; while (u < n && s[u][second] == '.') ++u; if (u >= n) break; LK(first, second, u, second); first = u; } } } int main() { scanf("%d%d", &n, &m); for (int i = (0); i < (int)(n); i++) scanf("%s", s[i]); int best = 0; int hv = n * m; for (int i = (0); i < (int)(n); i++) for (int j = (0); j < (int)(m); j++) { if (s[i][j] != '.') { init(); int cur = 0; int first = i, second = j; while (first != -1 && second != -1) { int a, b; if (s[first][second] == 'R') { a = first; b = ne[0][first][second]; } else if (s[first][second] == 'L') { a = first; b = pr[0][first][second]; } else if (s[first][second] == 'D') { a = ne[1][first][second]; b = second; } else if (s[first][second] == 'U') { a = pr[1][first][second]; b = second; } ER(first, second, s[first][second]); first = a; second = b; ++cur; } if (cur == best) { hv++; } else if (cur > best) { best = cur; hv = 1; } } } printf("%d %d\n", best, hv); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; int sn, sx[maxn], sy[maxn]; int h, w; vector<vector<int> > a, b, nextX[4], nextY[4]; vector<vector<char> > s; int dx[] = {1, 0, -1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; const char *str = "RDLU."; int Max, Num; int main() { int F = 0; scanf("%d%d", &h, &w); if (h <= w) { s = vector<vector<char> >(h, vector<char>(w + 1, 0)); for (int i = 0; i < (int)(h); i++) for (int j = 0; j < (int)(w); j++) scanf(" %c", &s[i][j]); } else { swap(w, h), F = 1; s = vector<vector<char> >(h, vector<char>(w + 1, 0)); for (int i = 0; i < (int)(w); i++) for (int j = 0; j < (int)(h); j++) scanf(" %c", &s[j][i]); } a = vector<vector<int> >(h + 2, vector<int>(w + 2, -1)); for (int j = 0; j < (int)(4); j++) { nextX[j] = vector<vector<int> >(h + 2, vector<int>(w + 2, -1)); nextY[j] = vector<vector<int> >(h + 2, vector<int>(w + 2, -1)); } for (int i = 0; i < (int)(h); i++) for (int j = 0; j < (int)(w); j++) { int &x = a[i + 1][j + 1]; x = strchr(str, s[i][j]) - str; if (F && x < 4) x ^= 1; } for (int i = (int)(1); i <= (int)(h); i++) for (int j = (int)(1); j <= (int)(w); j++) if (a[i][j] != 4) { b = a; for (int k = 0; k < (int)(4); k++) for (int i = (int)(1); i <= (int)(h); i++) for (int j = (int)(1); j <= (int)(w); j++) nextX[k][i][j] = -1, nextY[k][i][j] = -1; int dir = -1, x = j, y = i, score = 0; while (b[y][x] != -1) { score++, dir = b[y][x], b[y][x] = 4; sn = 0; while (b[y][x] == 4) { sx[sn] = x, sy[sn++] = y; if (nextX[dir][y][x] != -1) { int x1 = nextX[dir][y][x]; int y1 = nextY[dir][y][x]; x = x1, y = y1; } else x += dx[dir], y += dy[dir]; } for (int i = 0; i < (int)(sn); i++) { nextX[dir][sy[i]][sx[i]] = x; nextY[dir][sy[i]][sx[i]] = y; } } if (score > Max) Max = score, Num = 1; else if (score == Max) Num++; } printf("%d %d\n", Max, Num); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5050; int n, m, x[N], y[N], dirmap[300], dir[N], _; char s[N]; struct DirLink { int go[4]; } D[N], G[N]; void Link(int u, int v, int a, int b, DirLink* d) { d[u].go[a] = v, d[v].go[b] = u; } void Del(int x, DirLink* d) { for (int i = 0; i < (4); ++i) if (d[x].go[i]) d[d[x].go[i]].go[i ^ 2] = d[x].go[i ^ 2]; } int main() { dirmap['R'] = 0; dirmap['L'] = 2; dirmap['U'] = 1; dirmap['D'] = 3; scanf("%d%d", &n, &m); _ = 0; for (int i = 1; i < (n + 1); ++i) { scanf("%s", s + 1); for (int j = 1; j < (m + 1); ++j) if (s[j] != '.') { dir[++_] = dirmap[(int)s[j]]; if (x[i]) Link(x[i], _, 0, 2, D); x[i] = _; if (y[j]) Link(y[j], _, 3, 1, D); y[j] = _; } } for (int i = 1; i < (_ + 1); ++i) G[i] = D[i]; pair<int, int> ans = make_pair(0, 1); for (int i = 1; i < (_ + 1); ++i) { for (int j = 1; j < (_ + 1); ++j) D[j] = G[j]; int cnt = 0; for (int c = i, t; c; c = t) { t = D[c].go[dir[c]]; Del(c, D); cnt++; } if (cnt > ans.first) ans = make_pair(cnt, 1); else ans.second += (cnt == ans.first); } printf("%d %d\n", ans.first, ans.second); return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long n, k, t, m, q, flag = 0; long long power(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long min(long long a, long long b) { if (a > b) return b; else return a; } long long max(long long a, long long b) { if (a > b) return a; else return b; } long long gcd(long long a, long long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); t = 1; while (t--) { cin >> n >> m; char a[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; long long rown[n][m], rowp[n][m], coln[n][m], colp[n][m]; int ans = 0, maxcount = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (a[i][j] == '.') continue; for (int i1 = 0; i1 < n; i1++) for (int j1 = 0; j1 < m; j1++) rown[i1][j1] = i1 + 1, rowp[i1][j1] = i1 - 1, coln[i1][j1] = j1 + 1, colp[i1][j1] = j1 - 1; for (int i1 = 0; i1 < n; i1++) for (int j1 = 0; j1 < m; j1++) if (a[i1][j1] == '.') { if (rowp[i1][j1] != -1) rown[rowp[i1][j1]][j1] = rown[i1][j1]; if (rown[i1][j1] != n) rowp[rown[i1][j1]][j1] = rowp[i1][j1]; if (colp[i1][j1] != -1) coln[i1][colp[i1][j1]] = coln[i1][j1]; if (coln[i1][j1] != m) colp[i1][coln[i1][j1]] = colp[i1][j1]; } long long count = 0; int i1 = i, j1 = j; count++; while (1) { if (rowp[i1][j1] != -1) rown[rowp[i1][j1]][j1] = rown[i1][j1]; if (rown[i1][j1] != n) rowp[rown[i1][j1]][j1] = rowp[i1][j1]; if (colp[i1][j1] != -1) coln[i1][colp[i1][j1]] = coln[i1][j1]; if (coln[i1][j1] != m) colp[i1][coln[i1][j1]] = colp[i1][j1]; if (a[i1][j1] == 'D') { if (rown[i1][j1] == n) break; else { count++; i1 = rown[i1][j1]; } } else if (a[i1][j1] == 'U') { if (rowp[i1][j1] == -1) break; else { count++; i1 = rowp[i1][j1]; } } else if (a[i1][j1] == 'R') { if (coln[i1][j1] == m) break; else { count++; j1 = coln[i1][j1]; } } else if (a[i1][j1] == 'L') { if (colp[i1][j1] == -1) break; else { count++; j1 = colp[i1][j1]; } } } if (count == ans) maxcount++; else if (count > ans) ans = count, maxcount = 1; } cout << ans << " " << maxcount; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 * (int)1e3 + 10; int l[MAXN], r[MAXN], u[MAXN], d[MAXN]; int tl[MAXN], tr[MAXN], tu[MAXN], td[MAXN]; int n, m; vector<char> s[MAXN]; int node(int x, int y) { return x * m + y + 1; } int getfa(int x, int *fa) { if (fa[x] == x) return fa[x]; return fa[x] = getfa(fa[x], fa); } char st[MAXN]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", st); for (int j = 0; j < m; j++) s[i].push_back(st[j]); } for (int i = 0; i < n; i++) { int las = 0; for (int j = 0; j < m; j++) { int x = node(i, j); l[x] = las; if (s[i][j] != '.') las = x; } las = 0; for (int j = m - 1; j >= 0; j--) { int x = node(i, j); r[x] = las; if (s[i][j] != '.') las = x; } } for (int i = 0; i < m; i++) { int las = 0; for (int j = 0; j < n; j++) { int x = node(j, i); u[x] = las; if (s[j][i] != '.') las = x; } las = 0; for (int j = n - 1; j >= 0; j--) { int x = node(j, i); d[x] = las; if (s[j][i] != '.') las = x; } } int total = n * m; int mx = 0, sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (s[i][j] == '.') continue; int step = 0; int now = node(i, j); for (int k = 0; k <= total; k++) tl[k] = l[k], tr[k] = r[k], tu[k] = u[k], td[k] = d[k]; int ss = 0; while (now != 0) { int x = (now - 1) / m, y = (now - 1) % m; step++; tr[tl[now]] = tr[now]; tl[tr[now]] = tl[now]; td[tu[now]] = td[now]; tu[td[now]] = tu[now]; if (s[x][y] == 'R') now = tr[now]; else if (s[x][y] == 'L') now = tl[now]; else if (s[x][y] == 'D') now = td[now]; else if (s[x][y] == 'U') now = tu[now]; } if (step > mx) { mx = step; sum = 1; } else if (step == mx) sum++; } cout << mx << " " << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int MOD = 1e9 + 7; const int N = 5e3 + 10; const string dir = "LDUR"; const int dx[4] = {0, 1, -1, 0}, dy[4] = {-1, 0, 0, 1}; int a[N][N] = {0}, n, m, Slink[N][4], link[N][4]; char s[N]; int cv(char x) { int ans = 3; while (ans >= 0 && dir[ans] != x) ans--; return ans; } bool inside(pair<int, int> x) { return (x.first > 0 && x.first <= n && x.second > 0 && x.second <= m); } int check(pair<int, int> cur) { for (auto i = 0; i < n * m; i++) for (auto k = 0; k <= 3; k++) link[i][k] = Slink[i][k]; int ans = 0, idx = (cur.first - 1) * m + cur.second - 1; while (1) { ans++; for (auto i = 0; i <= 3; i++) { int nidx = link[idx][i]; if (nidx == -1) continue; link[nidx][3 - i] = link[idx][3 - i]; } int cx = idx / m + 1, cy = idx % m + 1; idx = link[idx][a[cx][cy]]; if (idx == -1) break; } return ans; } int main() { scanf("%d%d", &n, &m); for (auto i = 1; i <= n; i++) { scanf("\n%s", s); for (auto j = 1; j <= m; j++) a[i][j] = cv(s[j - 1]); } for (auto i = 1; i <= n; i++) for (auto j = 1; j <= m; j++) if (a[i][j] != -1) { for (auto k = 0; k <= 3; k++) { int cx = i + dx[k], cy = j + dy[k]; while (a[cx][cy] == -1) cx += dx[k], cy += dy[k]; Slink[(i - 1) * m + j - 1][k] = inside(pair<int, int>(cx, cy)) ? ((cx - 1) * m + cy - 1) : -1; } } int ans = 0, way = 0; for (auto i = 1; i <= n; i++) for (auto j = 1; j <= m; j++) if (a[i][j] != -1) { int v = check(pair<int, int>(i, j)); if (v > ans) ans = v, way = 1; else if (v == ans) way++; } cout << ans << " " << way; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 5003; int n, m; char** a; int** l; int** r; int** u; int** d; void bil(char**& a) { a = new char*[n + 5]; for (int i = 0; i < n + 5; ++i) a[i] = new char[m + 5]; for (int i = 0; i < n + 5; ++i) for (int j = 0; j < m + 5; ++j) a[i][j] = 0; } void bil(int**& a) { a = new int*[n + 5]; for (int i = 0; i < n + 5; ++i) a[i] = new int[m + 5]; for (int i = 0; i < n + 5; ++i) for (int j = 0; j < m + 5; ++j) a[i][j] = 0; } void han(int x, int y) { r[x][l[x][y]] = r[x][y]; l[x][r[x][y]] = l[x][y]; d[u[x][y]][y] = d[x][y]; u[d[x][y]][y] = u[x][y]; } int ans; void go(int x, int y) { if (!(1 <= x && x <= n && 1 <= y && y <= m)) return; ++ans; han(x, y); if (a[x][y] == 'L') { go(x, l[x][y]); } else if (a[x][y] == 'R') { go(x, r[x][y]); } else if (a[x][y] == 'U') { go(u[x][y], y); } else if (a[x][y] == 'D') { go(d[x][y], y); } } void solv() { scanf("%d%d", &n, &m); bil(a); for (int i = 1; i <= n; ++i) { scanf(" %s", (a[i] + 1)); } for (int i = 0; i <= n + 1; ++i) { a[i][0] = a[i][m + 1] = '.'; } for (int j = 0; j <= m + 1; ++j) { a[0][j] = a[n + 1][j] = '.'; } bil(l); bil(r); bil(u); bil(d); int yans = 0; int qans = 0; for (int sx = 1; sx <= n; ++sx) { for (int sy = 1; sy <= m; ++sy) { if (a[sx][sy] == '.') continue; for (int i = 1; i <= n; ++i) { int k = 0; for (int j = 1; j <= m; ++j) { l[i][j] = k; if (a[i][j] != '.') k = j; } k = m + 1; for (int j = m; j >= 1; --j) { r[i][j] = k; if (a[i][j] != '.') k = j; } } for (int j = 1; j <= m; ++j) { int k = 0; for (int i = 1; i <= n; ++i) { u[i][j] = k; if (a[i][j] != '.') k = i; } k = n + 1; for (int i = n; i >= 1; --i) { d[i][j] = k; if (a[i][j] != '.') k = i; } } ans = 0; go(sx, sy); if (ans > yans) { yans = ans; qans = 1; } else if (ans == yans) { ++qans; } } } printf("%d %d\n", yans, qans); } int main() { solv(); return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<char> > a, savea; vector<vector<int> > up, down, __left, __right; int m, n, cur[5011]; char tmp[100111]; void calc() { memset(cur, 0, sizeof cur); for (int i = (1), _b = (m); i <= _b; i++) { for (int j = (1), _b = (n); j <= _b; j++) if (a[i][j] != '.') { if (cur[j]) up[i][j] = cur[j]; cur[j] = i; } } memset(cur, 0, sizeof cur); for (int i = (m), _b = (1); i >= _b; i--) { for (int j = (1), _b = (n); j <= _b; j++) if (a[i][j] != '.') { if (cur[j]) down[i][j] = cur[j]; cur[j] = i; } } memset(cur, 0, sizeof cur); for (int j = (1), _b = (n); j <= _b; j++) { for (int i = (1), _b = (m); i <= _b; i++) if (a[i][j] != '.') { if (cur[i]) __left[i][j] = cur[i]; cur[i] = j; } } memset(cur, 0, sizeof cur); for (int j = (n), _b = (1); j >= _b; j--) { for (int i = (1), _b = (m); i <= _b; i++) if (a[i][j] != '.') { if (cur[i]) __right[i][j] = cur[i]; cur[i] = j; } } } void clear(int u, int v) { int x = up[u][v], y = down[u][v]; if (x) down[x][v] = y; if (y) up[y][v] = x; x = __left[u][v], y = __right[u][v]; if (x) __right[u][x] = y; if (y) __left[u][y] = x; a[u][v] = '.'; } int main() { while (scanf("%d%d\n", &m, &n) == 2) { { a.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) a[i].resize(n + 5); }; { savea.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) savea[i].resize(n + 5); }; { up.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) up[i].resize(n + 5); }; { down.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) down[i].resize(n + 5); }; { __left.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) __left[i].resize(n + 5); }; { __right.resize(m + 5); for (int i = 0, _a = (m + 5); i < _a; i++) __right[i].resize(n + 5); }; for (int i = (1), _b = (m); i <= _b; i++) { gets(tmp); for (int j = (1), _b = (n); j <= _b; j++) a[i][j] = savea[i][j] = tmp[j - 1]; } calc(); int res = 0, cnt = 0; for (int i = (1), _b = (m); i <= _b; i++) for (int j = (1), _b = (n); j <= _b; j++) if (a[i][j] != '.') { int u = i, v = j; int cur = 0; while (true) { ++cur; if (a[u][v] == 'U') { if (!up[u][v]) break; else { clear(u, v); u = up[u][v]; } } else if (a[u][v] == 'D') { if (!down[u][v]) break; else { clear(u, v); u = down[u][v]; } } else if (a[u][v] == 'L') { if (!__left[u][v]) break; else { clear(u, v); v = __left[u][v]; } } else { if (!__right[u][v]) break; else { clear(u, v); v = __right[u][v]; } } } if (cur > res) { res = cur; cnt = 1; } else if (cur == res) ++cnt; for (int i = (1), _b = (m); i <= _b; i++) for (int j = (1), _b = (n); j <= _b; j++) a[i][j] = savea[i][j]; calc(); } cout << res << ' ' << cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; char b[5005]; int n, m; int l[5005], r[5005], u[5005], d[5005]; void init() { fill(l, l + n * m, n * m); fill(r, r + n * m, n * m); fill(u, u + n * m, n * m); fill(d, d + n * m, n * m); for (int i = 0, ThxDem = n; i < ThxDem; ++i) { int z = n * m; for (int j = 0, ThxDem = m; j < ThxDem; ++j) if (b[i * m + j] != '.') { l[i * m + j] = z; r[z] = i * m + j; z = i * m + j; } } for (int j = 0, ThxDem = m; j < ThxDem; ++j) { int z = n * m; for (int i = 0, ThxDem = n; i < ThxDem; ++i) if (b[i * m + j] != '.') { u[i * m + j] = z; d[z] = i * m + j; z = i * m + j; } } } int main() { scanf("%d%d", &n, &m); for (int i = 0, ThxDem = n; i < ThxDem; ++i) scanf("%s", b + i * m); int r0 = 0, r1 = 0; for (int i = 0, ThxDem = n; i < ThxDem; ++i) for (int j = 0, ThxDem = m; j < ThxDem; ++j) if (b[i * m + j] != '.') { init(); int x = i * m + j, c = 0; while (x < n * m) { c++; l[r[x]] = l[x]; r[l[x]] = r[x]; u[d[x]] = u[x]; d[u[x]] = d[x]; if (b[x] == 'L') x = l[x]; else if (b[x] == 'R') x = r[x]; else if (b[x] == 'U') x = u[x]; else x = d[x]; } if (c > r0) r0 = c, r1 = 1; else if (c == r0) r1++; } printf("%d %d\n", r0, r1); return 0; }
#include <bits/stdc++.h> using namespace std; class chip_info { public: int lef, rig; }; vector<vector<char> > game; vector<vector<chip_info> > strng, stolb, cpstrng, cpstolb; int N, M; void copy() { for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { strng[i][j] = cpstrng[i][j]; stolb[j][i] = cpstolb[j][i]; } } int modeling(int x, int y) { int moves = 0; while (x >= 0 && y >= 0) { moves++; if (strng[x][y].lef != -1) strng[x][strng[x][y].lef].rig = strng[x][y].rig; if (strng[x][y].rig != -1) strng[x][strng[x][y].rig].lef = strng[x][y].lef; if (stolb[y][x].lef != -1) stolb[y][stolb[y][x].lef].rig = stolb[y][x].rig; if (stolb[y][x].rig != -1) stolb[y][stolb[y][x].rig].lef = stolb[y][x].lef; if (game[x][y] == 'L') y = strng[x][y].lef; else if (game[x][y] == 'R') y = strng[x][y].rig; else if (game[x][y] == 'U') x = stolb[y][x].lef; else x = stolb[y][x].rig; } copy(); return moves; } int main() { cin >> N >> M; game.resize(N + 1); strng.resize(N + 1); stolb.resize(M + 1); cpstrng.resize(N + 1); cpstolb.resize(M + 1); for (int i = 0; i < N; i++) game[i].resize(M + 1); for (int i = 0; i < N; i++) { strng[i].resize(M + 1); cpstrng[i].resize(M + 1); } for (int j = 0; j < M; j++) { stolb[j].resize(N + 1); cpstolb[j].resize(N + 1); } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> game[i][j]; int numchip = -1; for (int i = 0; i < N; i++) { numchip = -1; for (int j = 0; j < M; j++) if (game[i][j] != '.') { strng[i][j].lef = numchip; if (numchip != -1) strng[i][numchip].rig = j; numchip = j; } if (numchip != -1) strng[i][numchip].rig = -1; } for (int j = 0; j < M; j++) { numchip = -1; for (int i = 0; i < N; i++) if (game[i][j] != '.') { stolb[j][i].lef = numchip; if (numchip != -1) stolb[j][numchip].rig = i; numchip = i; } if (numchip != -1) stolb[j][numchip].rig = -1; } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { cpstrng[i][j] = strng[i][j]; cpstolb[j][i] = stolb[j][i]; } int t = 0, maxi = 0, kol = 0; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { t = 0; if (game[i][j] != '.') t = modeling(i, j); if (t > maxi) { maxi = t; kol = 1; } else if (t == maxi) kol++; } cout << maxi << " " << kol; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, cnt; int u[20005], d[20005], l[20005], r[20005]; char c[20005]; string mat[5005]; int id(int x, int y) { return x * (m + 2) + y; } void add(int k) { u[d[k]] = k; d[u[k]] = k; l[r[k]] = k; r[l[k]] = k; } void del(int k) { u[d[k]] = u[k]; d[u[k]] = d[k]; l[r[k]] = l[k]; r[l[k]] = r[k]; } void build() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int k = id(i, j); u[k] = id(i - 1, j); d[k] = id(i + 1, j); l[k] = id(i, j - 1); r[k] = id(i, j + 1); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (mat[i][j - 1] == '.') { del(id(i, j)); } } } } int gao(int k) { if (!k) return 0; int next = 0; if (c[k] == 'U') next = u[k]; if (c[k] == 'D') next = d[k]; if (c[k] == 'L') next = l[k]; if (c[k] == 'R') next = r[k]; del(k); int mov = gao(next) + 1; add(k); return mov; } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> mat[i]; for (int j = 0; j < m; ++j) { c[id(i, j + 1)] = mat[i][j]; } } build(); int ans = 0, cnt = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (mat[i][j - 1] != '.') { int tmp = gao(id(i, j)); if (ans < tmp) ans = tmp, cnt = 1; else if (ans == tmp) ++cnt; } } } cout << ans - 1 << " " << cnt << endl; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void debug(const T& c) { cout << "{"; for (__typeof((c.begin())) it = (c).begin(); it != (c).end(); ++it) cout << *it << ","; cout << "}" << endl; } template <class T> inline void setmin(T& a, const T& b) { if (b < a) a = b; } template <class T> inline void setmax(T& a, const T& b) { if (b > a) a = b; } template <class T> inline int size(const T& c) { return (int)c.size(); } template <class T> inline string to_str(const T& a) { ostringstream os(""); os << a; return os.str(); } template <class T> inline T sqr(const T& a) { return a * a; } template <class T> T next() { T x; cin >> x; return x; } int II() { int x; scanf("%d", &x); return x; } char tab[5009][5009]; vector<pair<int, int> > dp[4][5009]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int n, m; int trans[250]; void odnow() { for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (m); ++j) for (int k = (2); k <= (3); ++k) { int i2 = i + dx[k]; int j2 = j + dy[k]; if (tab[i2][j2] != '.' && tab[i2][j2] != 0) dp[k][i][j] = make_pair(i2, j2); else dp[k][i][j] = dp[k][i2][j2]; } for (int i = (n); i >= (1); --i) for (int j = (m); j >= (1); --j) for (int k = (0); k <= (1); ++k) { int i2 = i + dx[k]; int j2 = j + dy[k]; if (tab[i2][j2] != '.' && tab[i2][j2] != 0) dp[k][i][j] = make_pair(i2, j2); else dp[k][i][j] = dp[k][i2][j2]; } } void usun(int a, int b) { for (int k = 0; k < (4); ++k) { int a0 = dp[k][a][b].first; int b0 = dp[k][a][b].second; dp[(k + 2) & 3][a0][b0] = dp[(k + 2) & 3][a][b]; } } int go(int a, int b) { int ile = 0; while (a != 0 && b != 0) { int k = trans[tab[a][b]]; int nx = dp[k][a][b].first; int ny = dp[k][a][b].second; usun(a, b); a = nx; b = ny; ++ile; } return ile; } int main() { scanf("%d%d\n", &n, &m); for (int i = (1); i <= (n); ++i) { gets(&tab[i][1]); } for (int i = (0); i <= (n + 1); ++i) for (int j = 0; j < (4); ++j) dp[j][i] = vector<pair<int, int> >(m + 9, make_pair(0, 0)); trans['U'] = 3; trans['D'] = 1; trans['L'] = 2; trans['R'] = 0; int best = 0; int ile = 0; for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (m); ++j) { if (tab[i][j] == '.') continue; odnow(); int wynik = go(i, j); if (best == wynik) ++ile; else if (best < wynik) { best = wynik; ile = 1; } } printf("%d %d\n", best, ile); return 0; }
#include <bits/stdc++.h> using namespace std; int a[5010][4], aa[5010][4], d[5010]; int main() { int r, c, n = 0; scanf("%d%d\n", &r, &c); char s[r][c]; for (int i = 0; i < r; i++) gets(s[i]); int b[r][c]; memset(b, 0, sizeof(b)); for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) if (s[i][j] != '.') { b[i][j] = ++n; if (s[i][j] == 'U') d[n] = 0; if (s[i][j] == 'D') d[n] = 1; if (s[i][j] == 'L') d[n] = 2; if (s[i][j] == 'R') d[n] = 3; } for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) if (b[i][j]) { for (int k = i - 1; k >= 0; k--) if (b[k][j]) { a[b[i][j]][0] = b[k][j]; break; } for (int k = i + 1; k < r; k++) if (b[k][j]) { a[b[i][j]][1] = b[k][j]; break; } for (int k = j - 1; k >= 0; k--) if (b[i][k]) { a[b[i][j]][2] = b[i][k]; break; } for (int k = j + 1; k < c; k++) if (b[i][k]) { a[b[i][j]][3] = b[i][k]; break; } } int ans1 = 0, ans2; for (int i = 1; i <= n; i++) { int j = i, cnt = 0; memcpy(aa, a, sizeof(a)); while (j) { aa[aa[j][0]][1] = aa[j][1]; aa[aa[j][1]][0] = aa[j][0]; aa[aa[j][2]][3] = aa[j][3]; aa[aa[j][3]][2] = aa[j][2]; cnt++; j = aa[j][d[j]]; } if (cnt > ans1) { ans1 = cnt; ans2 = 0; } if (cnt == ans1) ans2++; } cout << ans1 << ' ' << ans2 << endl; }
#include <bits/stdc++.h> using namespace std; const unsigned long long MOD = 257; int n, m; string s[5005]; int L[5005], R[5005], U[5005], D[5005]; void init() { int b, pos; for (int i = 0; i < n; i++) { b = -1; for (int j = 0; j < m; j++) { if (s[i][j] != '.') { pos = i * m + j; L[pos] = b; if (b != -1) R[b] = pos; b = pos; } } R[b] = -1; } for (int i = 0; i < m; i++) { b = -1; for (int j = 0; j < n; j++) { if (s[j][i] != '.') { pos = j * m + i; U[pos] = b; if (b != -1) { D[b] = pos; } b = pos; } } D[b] = -1; } } int dfs(int now) { int x = now / m, y = now % m; if (L[now] != -1) R[L[now]] = R[now]; if (R[now] != -1) L[R[now]] = L[now]; if (U[now] != -1) D[U[now]] = D[now]; if (D[now] != -1) U[D[now]] = U[now]; int go; if (s[x][y] == 'U') go = U[now]; else if (s[x][y] == 'D') go = D[now]; else if (s[x][y] == 'L') go = L[now]; else if (s[x][y] == 'R') go = R[now]; if (go == -1) return 1; return dfs(go) + 1; } int main() { std::ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } int ans = 0, num = 0, tmp; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] != '.') { init(); tmp = dfs(i * m + j); if (tmp == ans) num++; else if (tmp > ans) { ans = tmp; num = 1; } } } } cout << ans << " " << num << endl; }
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int r = 1; x = x % p; while (y) { if (y & 1) r = r * x % p; y = y >> 1; x = x * x % p; } return r; } struct node { node *l, *r, *u, *d; int dir; int i, j; node() { l = r = u = d = NULL; i = j = dir = 0; } }; vector<vector<node> > g; int n, m; void build() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { g[i][j].i = i, g[i][j].j = j; if (i) { if (g[i - 1][j].dir) g[i][j].u = &g[i - 1][j]; else g[i][j].u = g[i - 1][j].u; } if (j) { if (g[i][j - 1].dir) g[i][j].l = &g[i][j - 1]; else g[i][j].l = g[i][j - 1].l; } } for (int i = n - 1; i >= 0; i--) for (int j = m - 1; j >= 0; j--) { if (i != n - 1) { if (g[i + 1][j].dir) g[i][j].d = &g[i + 1][j]; else g[i][j].d = g[i + 1][j].d; } if (j != m - 1) { if (g[i][j + 1].dir) g[i][j].r = &g[i][j + 1]; else g[i][j].r = g[i][j + 1].r; } } } void del(node *cur) { if (cur->l) cur->l->r = cur->r; if (cur->r) cur->r->l = cur->l; if (cur->u) cur->u->d = cur->d; if (cur->d) cur->d->u = cur->u; } long long int solve(int i, int j) { long long int ans = 0; node *temp = &g[i][j], *cc; while (1) { ans++; if (temp->dir == 1) { if (temp->l == NULL) return ans; cc = temp->l; del(temp); temp = cc; } else if (temp->dir == 2) { if (temp->r == NULL) return ans; cc = temp->r; del(temp); temp = cc; } else if (temp->dir == 3) { if (temp->u == NULL) return ans; cc = temp->u; del(temp); temp = cc; } else if (temp->dir == 4) { if (temp->d == NULL) return ans; cc = temp->d; del(temp); temp = cc; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; g.resize(n + 1); for (int i = 0; i < n + 1; i++) g[i].resize(m + 1); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char ch; cin >> ch; if (ch == 'L') g[i][j].dir = 1; if (ch == 'R') g[i][j].dir = 2; if (ch == 'U') g[i][j].dir = 3; if (ch == 'D') g[i][j].dir = 4; } long long int ans = -1e9, ct = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (!g[i][j].dir) continue; build(); long long int ta = solve(i, j); if (ta > ans) ans = ta, ct = 1; else if (ta == ans) ct++; } cout << ans << " " << ct << '\n'; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool gmin(T &a, T b) { if (a > b) return a = b, true; return false; } template <class T> inline bool gmax(T &a, T b) { if (a < b) return a = b, true; return false; } template <class T> T exgcd(T a, T b, T &first, T &second) { if (!b) return first = (T)1, second = (T)0, a; else { T d = exgcd(b, a % b, second, first); return second -= a / b * first, d; } } const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const double DINF = 1e10; const double EPS = 1e-9; const double PI = 3.14159265358979323846264338327950288; const double TAU = 2.0 * PI; inline int sgn(const double &a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); } inline int lowbit(int i) { return i & (-i); } struct Initializer { Initializer() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } } initializer; const int MAXN = 5005; int n, m; int tol[MAXN], tor[MAXN], tou[MAXN], tod[MAXN]; int l[MAXN], r[MAXN], d[MAXN], u[MAXN]; char t[MAXN][MAXN]; inline int idx(int i, int j) { return (i - 1) * n + j; } void update(int i, int j) { if (r[idx(i, j)]) l[idx(i, r[idx(i, j)])] = l[idx(i, j)]; if (l[idx(i, j)]) r[idx(i, l[idx(i, j)])] = r[idx(i, j)]; if (u[idx(i, j)]) d[idx(u[idx(i, j)], j)] = d[idx(i, j)]; if (d[idx(i, j)]) u[idx(d[idx(i, j)], j)] = u[idx(i, j)]; } int main() { scanf("%d %d", &m, &n); for (int i = 1; i <= m; i++) scanf("%s", t[i] + 1); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { for (int k = j - 1; k >= 1; k--) if (t[i][k] != '.') { tol[idx(i, j)] = k; break; } for (int k = i - 1; k >= 1; k--) if (t[k][j] != '.') { tou[idx(i, j)] = k; break; } for (int k = j + 1; k <= n; k++) if (t[i][k] != '.') { tor[idx(i, j)] = k; break; } for (int k = i + 1; k <= m; k++) if (t[k][j] != '.') { tod[idx(i, j)] = k; break; } } int ans = 0, cnt = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if (t[i][j] != '.') { for (int p = 1; p <= m; p++) for (int q = 1; q <= n; q++) { l[idx(p, q)] = tol[idx(p, q)]; r[idx(p, q)] = tor[idx(p, q)]; u[idx(p, q)] = tou[idx(p, q)]; d[idx(p, q)] = tod[idx(p, q)]; } int rec = 0; for (int first = i, second = j; first && second;) { rec++; update(first, second); switch (t[first][second]) { case 'L': second = l[idx(first, second)]; break; case 'R': second = r[idx(first, second)]; break; case 'U': first = u[idx(first, second)]; break; case 'D': first = d[idx(first, second)]; break; } } if (rec > ans) ans = rec, cnt = 1; else { if (rec == ans) cnt++; } } printf("%d %d\n", ans, cnt); return 0; }
#include <bits/stdc++.h> using namespace std; const int step[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; const char *s = "LRUD"; int n, m, tot; char mp[5010][5010]; struct point { int x, y; }; bool valid(point a) { if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m) return 1; return 0; } int to(int x, int y) { return x * m + y; } struct str { point next[4]; int dir; }; str st[5010]; point temp, now, last; point rec[5010]; int ct; int go() { int i, j, k; int k1, k2; int p, q; ct = 1; rec[0] = now; while (valid(rec[ct - 1])) { k = to(rec[ct - 1].x, rec[ct - 1].y); temp = st[k].next[st[k].dir]; for (i = 0; i < 4; i += 2) { if (i == 0) j = 1; else j = 3; now = st[k].next[i]; last = st[k].next[j]; if (valid(now)) { k1 = to(now.x, now.y); st[k1].next[j] = last; } if (valid(last)) { k2 = to(last.x, last.y); st[k2].next[i] = now; } } rec[ct] = temp; ct++; } int ret = ct - 1; ct--; while (ct) { k = to(rec[ct - 1].x, rec[ct - 1].y); temp = st[k].next[st[k].dir]; for (i = 0; i < 4; i += 2) { if (i == 0) j = 1; else j = 3; now = st[k].next[i]; last = st[k].next[j]; if (valid(now)) { k1 = to(now.x, now.y); st[k1].next[j] = rec[ct - 1]; } if (valid(last)) { k2 = to(last.x, last.y); st[k2].next[i] = rec[ct - 1]; } } ct--; } return ret; } int main() { int i, j, k, o; int p, q; while (scanf("%d%d", &n, &m) != EOF) { for (i = 0; i < n; i++) scanf("%s", &mp[i]); tot = n * m; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { o = to(i, j); for (k = 0; k < 4; k++) { p = i + step[k][0]; q = j + step[k][1]; temp.x = p; temp.y = q; while (valid(temp)) { if (mp[temp.x][temp.y] != '.') break; temp.x += step[k][0]; temp.y += step[k][1]; } st[o].next[k] = temp; } for (k = 0; k < 4; k++) { if (mp[i][j] == s[k]) { st[o].dir = k; break; } } } } int ans = 0; int num = 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (mp[i][j] == '.') continue; now.x = i; now.y = j; k = go(); if (k > ans) { ans = k; num = 1; } else if (k == ans) { num++; } } } printf("%d %d\n", ans, num); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char c[5010][5001] = {}; short p[5010][550][4] = {}; short q[5010][550][4] = {}; short pi[550][5010][4] = {}; short qi[550][5010][4] = {}; vector<int> c1; vector<int> c2; vector<int> c3; vector<int> c4; void init() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { p[i][j][0] = p[i - 1][j][0]; p[i][j][2] = p[i][j - 1][2]; if (c[i - 1][j] != '.') p[i][j][0] = i - 1; if (c[i][j - 1] != '.') p[i][j][2] = j - 1; } } for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { p[i][j][1] = p[i + 1][j][1]; p[i][j][3] = p[i][j + 1][3]; if (c[i + 1][j] != '.') p[i][j][1] = i + 1; if (c[i][j + 1] != '.') p[i][j][3] = j + 1; } } } void pp() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k <= 3; k++) { q[i][j][k] = p[i][j][k]; } } } } int ff(int x, int y) { int ans = 0; c1.clear(); c2.clear(); c3.clear(); c4.clear(); while (1) { ans++; int w; if (c[x][y] == 'U') w = q[x][y][0]; if (c[x][y] == 'D') w = q[x][y][1]; if (c[x][y] == 'L') w = q[x][y][2]; if (c[x][y] == 'R') w = q[x][y][3]; if (w == 0) break; int e[4]; for (int i = 0; i <= 3; i++) e[i] = q[x][y][i]; if (e[0] != 0) { c1.push_back(e[0]); c2.push_back(y); c3.push_back(1); c4.push_back(q[e[0]][y][1]); q[e[0]][y][1] = e[1]; } if (e[1] != 0) { c1.push_back(e[1]); c2.push_back(y); c3.push_back(0); c4.push_back(q[e[1]][y][0]); q[e[1]][y][0] = e[0]; } if (e[2] != 0) { c1.push_back(x); c2.push_back(e[2]); c3.push_back(3); c4.push_back(q[x][e[2]][3]); q[x][e[2]][3] = e[3]; } if (e[3] != 0) { c1.push_back(x); c2.push_back(e[3]); c3.push_back(2); c4.push_back(q[x][e[3]][2]); q[x][e[3]][2] = e[2]; } if (c[x][y] == 'U') x = e[0]; else if (c[x][y] == 'D') x = e[1]; else if (c[x][y] == 'L') y = e[2]; else if (c[x][y] == 'R') y = e[3]; } for (int i = c1.size() - 1; i >= 0; i--) { q[c1[i]][c2[i]][c3[i]] = c4[i]; } return ans; } void init1() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { pi[i][j][0] = pi[i - 1][j][0]; pi[i][j][2] = pi[i][j - 1][2]; if (c[i - 1][j] != '.') pi[i][j][0] = i - 1; if (c[i][j - 1] != '.') pi[i][j][2] = j - 1; } } for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { pi[i][j][1] = pi[i + 1][j][1]; pi[i][j][3] = pi[i][j + 1][3]; if (c[i + 1][j] != '.') pi[i][j][1] = i + 1; if (c[i][j + 1] != '.') pi[i][j][3] = j + 1; } } } void pp1() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k <= 3; k++) { qi[i][j][k] = pi[i][j][k]; } } } } int ff1(int x, int y) { int ans = 0; c1.clear(); c2.clear(); c3.clear(); c4.clear(); while (1) { ans++; int w; if (c[x][y] == 'U') w = qi[x][y][0]; if (c[x][y] == 'D') w = qi[x][y][1]; if (c[x][y] == 'L') w = qi[x][y][2]; if (c[x][y] == 'R') w = qi[x][y][3]; if (w == 0) break; int e[4]; for (int i = 0; i <= 3; i++) e[i] = qi[x][y][i]; if (e[0] != 0) { c1.push_back(e[0]); c2.push_back(y); c3.push_back(1); c4.push_back(qi[e[0]][y][1]); qi[e[0]][y][1] = e[1]; } if (e[1] != 0) { c1.push_back(e[1]); c2.push_back(y); c3.push_back(0); c4.push_back(qi[e[1]][y][0]); qi[e[1]][y][0] = e[0]; } if (e[2] != 0) { c1.push_back(x); c2.push_back(e[2]); c3.push_back(3); c4.push_back(qi[x][e[2]][3]); qi[x][e[2]][3] = e[3]; } if (e[3] != 0) { c1.push_back(x); c2.push_back(e[3]); c3.push_back(2); c4.push_back(qi[x][e[3]][2]); qi[x][e[3]][2] = e[2]; } if (c[x][y] == 'U') x = e[0]; else if (c[x][y] == 'D') x = e[1]; else if (c[x][y] == 'L') y = e[2]; else if (c[x][y] == 'R') y = e[3]; } for (int i = c1.size() - 1; i >= 0; i--) { qi[c1[i]][c2[i]][c3[i]] = c4[i]; } return ans; } char cc; int main() { scanf("%d%d", &n, &m); for (int i = 0; i <= n + 1; i++) { for (int j = 0; j <= m + 1; j++) c[i][j] = '.'; } cc = getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cc = getchar(); c[i][j] = cc; } cc = getchar(); } int ansx = 0; int ansy = 0; if (n >= m) { init(); pp(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] == '.') continue; int tt = ff(i, j); if (tt > ansx) { ansx = tt; ansy = 1; } else if (tt == ansx) { ansy++; } } } } else { init1(); pp1(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] == '.') continue; int tt = ff1(i, j); if (tt > ansx) { ansx = tt; ansy = 1; } else if (tt == ansx) { ansy++; } } } } printf("%d %d\n", ansx, ansy); return 0; }
#include <bits/stdc++.h> using namespace std; struct node { node *next, *oth, *prev; int x, y; bool type; node(int x, int y, bool type) : x(x), y(y), type(type) { next = oth = prev = 0; } node() { x = y = -1; next = oth = prev = 0; } }; node* xs[5100]; node* ys[5100]; vector<char> s[5100]; vector<node*> xnode[5100]; vector<node*> ynode[5100]; int n, m; void del(node* x) { x->prev->next = x->next; x->next->prev = x->prev; } void init() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] != '.') { node* tmp = new node(i, j, 0); node* tmp2 = new node(i, j, 1); (tmp->oth = tmp2)->oth = tmp; xnode[i].push_back(tmp); ynode[j].push_back(tmp2); } } node mem[30000]; int memptr; node* getnode() { mem[memptr] = node(); return &mem[memptr++]; } void build() { memptr = 0; for (int i = 0; i < n; i++) { xs[i] = getnode(); node* t = xs[i]; for (int j = 0; j < xnode[i].size(); j++) { t->next = xnode[i][j]; xnode[i][j]->prev = t; t = xnode[i][j]; } t->next = getnode(); t->next->prev = t; } for (int i = 0; i < m; i++) { ys[i] = getnode(); node* t = ys[i]; for (int j = 0; j < ynode[i].size(); j++) { t->next = ynode[i][j]; ynode[i][j]->prev = t; t = ynode[i][j]; } t->next = getnode(); t->next->prev = t; } } void read() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { s[i].resize(m); for (int j = 0; j < m; j++) scanf(" %c ", &s[i][j]); } } int calc(int x, int y) { build(); node* cur = xs[x]; for (; cur->y != y; cur = cur->next) ; int cnt = 0; for (; cur->x != -1;) { cnt++; int x = cur->x; int y = cur->y; node* next; if (s[x][y] == 'L' || s[x][y] == 'R') if (cur->type) cur = cur->oth; if (s[x][y] == 'U' || s[x][y] == 'D') if (!cur->type) cur = cur->oth; if (s[x][y] == 'L' || s[x][y] == 'U') next = cur->prev; else next = cur->next; del(cur->oth); del(cur); cur = next; } return cnt; } int main() { read(); init(); int best = 0, bcnt = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] != '.') { int tmp = calc(i, j); if (tmp > best) best = tmp, bcnt = 1; else if (tmp == best) bcnt++; } cout << best << " " << bcnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct _ { _() { ios_base::sync_with_stdio(0); } } _; template <class A, class B> ostream &operator<<(ostream &o, pair<A, B> t) { o << "(" << t.first << ", " << t.second << ")"; return o; } template <class T> string tostring(T first, int len = 0, char c = '0') { stringstream ss; ss << first; string r = ss.str(); if (r.length() < len) r = string(len - r.length(), c) + r; return r; } template <class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") { int c = 0; while (a != b) { cout << tostring(*a++, w, ' '); if (a != b && (n == 0 || ++c % n)) cout << s; else cout << endl; } } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; } const long long linf = 0x3f3f3f3f3f3f3f3fLL; const int inf = 0x3f3f3f3f; const int mod = int(1e9) + 7; const int N = 5005; int n, m; vector<string> s; vector<vector<int>> L, R, U, D; vector<vector<int>> L1, R1, U1, D1; int main() { cin >> n >> m; s.resize(n); L.resize(n, vector<int>(m)); R.resize(n, vector<int>(m)); U.resize(n, vector<int>(m)); D.resize(n, vector<int>(m)); for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) { { int pre = -1; for (int j = 0; j < m; j++) { L[i][j] = pre; if (s[i][j] != '.') pre = j; } } { int pre = -1; for (int j = m - 1; j >= 0; j--) { R[i][j] = pre; if (s[i][j] != '.') pre = j; } } } for (int j = 0; j < m; j++) { { int pre = -1; for (int i = 0; i < n; i++) { U[i][j] = pre; if (s[i][j] != '.') pre = i; } } { int pre = -1; for (int i = n - 1; i >= 0; i--) { D[i][j] = pre; if (s[i][j] != '.') pre = i; } } } L1 = L; R1 = R; U1 = U; D1 = D; int res = 0; int num = 0; vector<string> ss = s; long long tot = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (ss[i][j] == '.') continue; long long t1 = clock(); for (int first = 0; first < n; first++) for (int second = 0; second < m; second++) { L[first][second] = L1[first][second]; R[first][second] = R1[first][second]; U[first][second] = U1[first][second]; D[first][second] = D1[first][second]; s[first][second] = ss[first][second]; } tot += clock() - t1; int ci = i, cj = j, ni = -1, nj = -1; int cnt = 0; while (1) { if (ci < 0 || cj < 0) break; char &ch = s[ci][cj]; if (ch == '.') break; cnt++; if (ch == 'L') ni = ci, nj = L[ci][cj]; else if (ch == 'R') ni = ci, nj = R[ci][cj]; else if (ch == 'U') ni = U[ci][cj], nj = cj; else if (ch == 'D') ni = D[ci][cj], nj = cj; if (R[ci][cj] >= 0) L[ci][R[ci][cj]] = L[ci][cj]; if (L[ci][cj] >= 0) R[ci][L[ci][cj]] = R[ci][cj]; if (D[ci][cj] >= 0) U[D[ci][cj]][cj] = U[ci][cj]; if (U[ci][cj] >= 0) D[U[ci][cj]][cj] = D[ci][cj]; ch = '.'; ci = ni, cj = nj; } if (res < cnt) { res = cnt; num = 1; } else if (res == cnt) { num++; } } cerr << "[" "0" " -> " "tot" ": " << 1e3 * (tot - 0) / CLOCKS_PER_SEC << "ms]\n"; ; cout << res << " " << num << endl; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:134217728") using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int n; int m; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; string s[5001]; vector<int> t[5001]; string ss[5001]; vector<pair<int, int> > root[4][5001]; vector<pair<int, int> > next239[4][5001]; int good(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; } int ans[10000]; pair<int, int> get_root(int t, pair<int, int> o) { if (root[t][o.first][o.second] == o) return o; return root[t][o.first][o.second] = get_root(t, root[t][o.first][o.second]); } void merge(int t, pair<int, int> a, pair<int, int> b) { a = get_root(t, a); root[t][a.first][a.second] = get_root(t, b); } int main() { cin >> n >> m; for (int i = 0; i < (n); i++) cin >> s[i]; for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++) if (s[i][j] != '.') { int c = s[i][j]; if (c == 'U') t[i].push_back(3); if (c == 'D') t[i].push_back(1); if (c == 'L') t[i].push_back(2); if (c == 'R') t[i].push_back(0); } else t[i].push_back(-1); for (int o = 0; o < (4); o++) { for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++) { int cx = i + dx[o]; int cy = j + dy[o]; while (good(cx, cy) && s[cx][cy] == '.') { cx += dx[o]; cy += dy[o]; } if (good(cx, cy)) next239[o][i].push_back(make_pair(cx, cy)); else next239[o][i].push_back(make_pair(n, m)); } } for (int o = 0; o < (4); o++) for (int i = 0; i < (n + 1); i++) root[o][i].resize(m + 1); for (int x = 0; x < (n); x++) for (int y = 0; y < (m); y++) if (s[x][y] != '.') { for (int i = 0; i < (n + 1); i++) for (int j = 0; j < (m + 1); j++) for (int o = 0; o < (4); o++) root[o][i][j] = make_pair(i, j); int cx = x, cy = y; int col = 1; while (good(cx, cy)) { pair<int, int> tmp = get_root(t[cx][cy], next239[t[cx][cy]][cx][cy]); if (!good(tmp.first, tmp.second)) break; for (int o = 0; o < (4); o++) { merge(o, make_pair(cx, cy), next239[o][cx][cy]); } cx = tmp.first; cy = tmp.second; col++; } ans[col]++; } int o = n * m; while (ans[o] == 0) o--; cout << o << ' ' << ans[o] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mxn = 5006; int ans = -1, tot; int n, m; class tien { public: int U, D, L, R; inline void ini() { U = D = L = R = 1; } } * a[mxn]; char *c[mxn]; inline void erase(int x, int y) { a[x][y - a[x][y].L].R += a[x][y].R; a[x][y + a[x][y].R].L += a[x][y].L; a[x - a[x][y].U][y].D += a[x][y].D; a[x + a[x][y].D][y].U += a[x][y].U; } inline int cal(int x, int y) { int re = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) a[i][j].ini(); char faon; while (c[x][y]) { erase(x, y); if (c[x][y] != '.') { faon = c[x][y]; ++re; } if (faon == 'U') { x -= a[x][y].U; } else if (faon == 'D') { x += a[x][y].D; } else if (faon == 'L') { y -= a[x][y].L; } else if (faon == 'R') { y += a[x][y].R; } } return re; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i <= n + 1; ++i) { c[i] = new char[m + 2]; a[i] = new tien[m + 2]; } for (int i = 0; i <= m + 1; ++i) c[0][i] = c[n + 1][i] = 0; for (int i = 1; i <= n; ++i) { scanf("%s", c[i] + 1); c[i][0] = c[i][m + 1] = 0; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (c[i][j] != '.') { int t = cal(i, j); if (t > ans) { ans = t; tot = 1; } else if (t == ans) { ++tot; } } printf("%d %d\n", ans, tot); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } template <class T> T read_value(string s) { T result; istringstream sin(s); sin >> result; return result; } string read_line() { string ret_val; getline(std::cin, ret_val); return ret_val; } template <class T> void init_array(T *arr, int size, T value) { for (int i = 0; i < size; i++) arr[i] = value; } template <class T> inline T gcd(T a, T b) { return (!a) ? b : gcd(b % a, a); } template <class T> inline T mod(T a, T p) { a %= p; return (a < 0) ? a + p : a; } template <class T> inline int num_bits(T n) { return (!n) ? 0 : 1 + numbits(n & (n - 1)); } template <class T> inline T inverse(T a, T m) { a = mod<T>(a, m); return (a == 1) ? 1 : mod((1 - m * inverse(m % a, a)) / a, m); } template <class T> inline bool is_prime(T a) { T lim = (T)sqrt((double)a); for (T i = 2; i <= T(lim + 1E-9); i++) if (a % i == 0) return false; return true; } template <class T> inline T power(T a, T p, T mod) { if (!p) return 1; T temp = power(a, p >> 1, mod); temp = (temp * temp) % mod; if (p & 1) temp = (temp * a) % mod; return temp; } void get_primes(int start, int end, vector<int> &vi) { int *p = new int[end + 1]; init_array<int>(p, end + 1, 0); p[1] = 1; for (int i = 2; i <= end; i++) { if (!p[i]) { if (i >= start) vi.push_back(i); for (int j = 2 * i; j <= end; j += i) p[j] = 1; } } delete[] p; } bool dfs(int current, int final, int total, int *visited, int **edges, bool flow) { if (current == final) return true; if (visited[current]) return false; visited[current] = true; for (int i = 0; i < total; i++) if (edges[current][i] && dfs(i, final, total, visited, edges, flow)) { if (flow) { edges[current][i]--; edges[i][current]++; } return true; } return false; } int flow(int in, int out, int total, int **edges) { int result = 0; int *visited = new int[total]; while (init_array<int>(visited, total, 0), dfs(in, out, total, visited, edges, true)) result++; delete[] visited; return result; } void create_set(int x, int *P, int *rank) { P[x] = x; rank[x] = 0; } int find_set(int x, int *P) { if (x != P[x]) P[x] = find_set(P[x], P); return P[x]; } bool merge_sets(int x, int y, int *P, int *rank) { int Px = find_set(x, P); int Py = find_set(y, P); if (Px == Py) return false; if (rank[Px] > rank[Py]) P[Py] = Px; else P[Px] = Py; if (rank[Px] == rank[Py]) rank[Py]++; return true; } template <typename T> int read_cum_freq(int index, T *tree) { int sum = 0; while (index) { sum += tree[index]; index -= (index & -index); } return sum; } template <typename T> void upd_freq(int index, int mxIndex, int value, T *tree) { while (index <= mxIndex) { tree[index] += value; index += (index & -index); } } template <typename T> int read_freq(int index, T *tree) { return read_cum_freq(index, tree) - read_cum_freq(index - 1, tree); } void build_failure_function(const string &str, int *temp_arr) { temp_arr[0] = temp_arr[1] = 0; for (int i = 2; i <= ((int)(str).size()); i++) { temp_arr[i] = -1; for (int ind = temp_arr[i - 1]; temp_arr[i] == -1; ind = temp_arr[ind]) { if (str[ind] == str[i - 1]) { temp_arr[i] = ind + 1; } else if (ind == 0) { temp_arr[i] = 0; } } } } void KMP(const string &text, const string &pattern, int *res) { int *temp_arr = new int[((int)(pattern).size()) + 1]; build_failure_function(pattern, temp_arr); int i = 0; int ind = 0; while (i < ((int)(text).size())) { if (text[i] == pattern[ind]) { res[i] = ++ind; if (res[i] == ((int)(pattern).size())) { ind = temp_arr[ind]; } i++; } else if (ind == 0) { res[i++] = 0; } else { ind = temp_arr[ind]; } } delete[] temp_arr; } void setup(int value, string name) { string name_in = name + ".in"; string name_out = name + ".out"; freopen(name_in.c_str(), "r", stdin); if (value) { freopen(name_out.c_str(), "w", stdout); } } vector<vector<int> > vvp[4]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int go(int r, int c, vector<string> vs) { vector<vector<int> > temp[4]; for (int i = 0; i < 4; i++) temp[i] = vvp[i]; int res = 0; while (true) { if (vs[r][c] == '.') break; res++; char ch = vs[r][c]; vs[r][c] = '.'; int dir = 0; if (ch == 'L') dir = 0; if (ch == 'R') dir = 2; if (ch == 'D') dir = 1; if (ch == 'U') dir = 3; int value = temp[dir][r][c]; if (value == -1) break; { int col1 = temp[0][r][c]; int col2 = temp[2][r][c]; if (col1 != -1) { temp[2][r][col1] = col2; } if (col2 != -1) { temp[0][r][col2] = col1; } } { int row1 = temp[3][r][c]; int row2 = temp[1][r][c]; if (row1 != -1) { temp[1][row1][c] = row2; } if (row2 != -1) { temp[3][row2][c] = row1; } } if (ch == 'L' || ch == 'R') { c = value; } else { r = value; } } return res; } char change_dir(char ch) { if (ch == '.') return '.'; if (ch == 'L') return 'U'; if (ch == 'U') return 'L'; if (ch == 'D') return 'R'; if (ch == 'R') return 'D'; } int main() { int n, m; std::cin >> n >> m; vector<string> vs(n); for (int i = 0; i < n; i++) std::cin >> vs[i]; if (n > m) { vector<string> tmp(m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { tmp[i].push_back(change_dir(vs[j][i])); } } vs = tmp; swap(n, m); } for (int dir = 0; dir < 4; dir++) { for (int i = 0; i < n; i++) { vector<int> vi(m, -1); vvp[dir].push_back(vi); } int dc = dx[dir]; int dr = dy[dir]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int row = i; int col = j; if (vs[row][col] == '.') continue; while (true) { row += dr; col += dc; if (row < 0 || col < 0 || row >= n || col >= m) break; if (vs[row][col] != '.') { if (dir == 0 || dir == 2) { vvp[dir][i][j] = col; } else { vvp[dir][i][j] = row; } break; } } } } int res = 0; int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int r = i; int c = j; int nres = go(r, c, vs); if (nres > res) { res = nres; cnt = 1; } else if (nres == res) { cnt++; } } } std::cout << res << " " << cnt << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; long long M = 550; long long n, k, t0, m; vector<string> a; int maxV = 0; int maxVCount = 0; vector<vector<vector<int>>> ma; int getMa(int t, int r, int c) { if (r == -1 || c == -1) return -1; return ma[t][r][c]; } void setMa(int t, int r, int c, int v) { if (r == -1 || c == -1) return; ma[t][r][c] = v; } bool search(int r, int c, int v) { v++; if (v > maxV) { maxV = v; maxVCount = 1; } else if (v == maxV) { maxVCount++; } int temp[5]; temp[0] = getMa(0, ma[2][r][c], c); temp[1] = getMa(1, r, ma[3][r][c]); temp[2] = getMa(2, ma[0][r][c], c); temp[3] = getMa(3, r, ma[1][r][c]); setMa(0, ma[2][r][c], c, ma[0][r][c]); setMa(1, r, ma[3][r][c], ma[1][r][c]); setMa(2, ma[0][r][c], c, ma[2][r][c]); setMa(3, r, ma[1][r][c], ma[3][r][c]); if (a[r][c] == 'U') { if (ma[0][r][c] != -1) { search(ma[0][r][c], c, v); } } else if (a[r][c] == 'R') { if (ma[1][r][c] != -1) { search(r, ma[1][r][c], v); } } else if (a[r][c] == 'D') { if (ma[2][r][c] != -1) { search(ma[2][r][c], c, v); } } else if (a[r][c] == 'L') { if (ma[3][r][c] != -1) { search(r, ma[3][r][c], v); } } setMa(0, ma[2][r][c], c, temp[0]); setMa(1, r, ma[3][r][c], temp[1]); setMa(2, ma[0][r][c], c, temp[2]); setMa(3, r, ma[1][r][c], temp[3]); return true; } int main() { scanf("%d%d", &n, &m); ma = vector<vector<vector<int>>>( 4, vector<vector<int>>(n + 10, vector<int>(m + 10, -1))); a = vector<string>(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> last = vector<int>(m + 10, -1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] != '.') { ma[0][i][j] = last[j]; last[j] = i; } } } last = vector<int>(n + 10, -1); for (int j = m - 1; j >= 0; j--) { for (int i = 0; i < n; i++) { if (a[i][j] != '.') { ma[1][i][j] = last[i]; last[i] = j; } } } last = vector<int>(m + 10, -1); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < m; j++) { if (a[i][j] != '.') { ma[2][i][j] = last[j]; last[j] = i; } } } last = vector<int>(n + 10, -1); for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (a[i][j] != '.') { ma[3][i][j] = last[i]; last[i] = j; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] != '.') { search(i, j, 0); } } } printf("%d %d", maxV, maxVCount); }
#include <bits/stdc++.h> using namespace std; inline int max(int a, int b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } char s[5005], ch[] = {'U', 'L', 'R', 'D'}; int typ[5005], mk[5005][5005], to[5005][4], w[5005][4]; int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; struct node { int r, c; node() {} node(int r_, int c_) : r(r_), c(c_) {} node operator+(int k) { return node(r + dir[k][0], c + dir[k][1]); } void operator+=(int k) { r += dir[k][0]; c += dir[k][1]; } } nod[5005]; int n, m, tot; inline bool ok(node u) { return u.r >= 0 && u.r < n && u.c >= 0 && u.c < m; } int main() { while (scanf("%d %d", &n, &m) == 2) { tot = 0; getchar(); for (int i(0); i < (n); ++i) { gets(s); for (int j(0); j < (m); ++j) { if (s[j] != '.') { for (int k(0); k < (4); ++k) if (s[j] == ch[k]) { typ[tot] = k; mk[i][j] = tot; nod[tot] = node(i, j); ++tot; break; } } else mk[i][j] = -1; } } for (int i(0); i < (tot); ++i) for (int j(0); j < (4); ++j) { node v(nod[i] + j); w[i][j] = -1; while (ok(v)) { if (mk[v.r][v.c] != -1) { w[i][j] = mk[v.r][v.c]; break; } v += j; } } int len(-1), num(0); for (int i(0); i < (tot); ++i) { int t(i), tmp(0); memcpy(to, w, sizeof(w)); while (t != -1) { ++tmp; for (int j(0); j < (4); ++j) if (to[t][j] != -1) to[to[t][j]][3 - j] = to[t][3 - j]; t = to[t][typ[t]]; } if (tmp > len) len = tmp, num = 1; else if (tmp == len) ++num; } printf("%d %d\n", len, num); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5000 + 5; short dir[4][maxn][maxn]; int h, w; char mat[maxn][maxn]; void init() { for (int i = 0; i < h; ++i) { dir[1][i][0] = -1; for (int j = 1; j < w; ++j) dir[1][i][j] = j - 1; dir[3][i][w - 1] = -1; for (int j = w - 2; j >= 0; --j) dir[3][i][j] = j + 1; } for (int i = 0; i < w; ++i) { dir[2][0][i] = -1; for (int j = 1; j < h; ++j) dir[2][j][i] = j - 1; dir[0][h - 1][i] = -1; for (int j = h - 2; j >= 0; --j) dir[0][j][i] = j + 1; } } void del(int r, int c) { int left = dir[1][r][c]; int right = dir[3][r][c]; if (left != -1) dir[3][r][left] = right; if (right != -1) dir[1][r][right] = left; int down = dir[0][r][c]; int up = dir[2][r][c]; if (up != -1) dir[0][up][c] = down; if (down != -1) dir[2][down][c] = up; } int d[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; struct dat { int r, c, d; dat(){}; dat(int p1, int p2, int p3) : r(p1), c(p2), d(p3){}; }; int main() { scanf("%d%d", &h, &w); for (int i = 0; i < h; ++i) { scanf("%s", mat[i]); for (int j = 0; j < w; ++j) { if (mat[i][j] == '.') mat[i][j] = -1; else if (mat[i][j] == 'D') mat[i][j] = 0; else if (mat[i][j] == 'L') mat[i][j] = 1; else if (mat[i][j] == 'U') mat[i][j] = 2; else mat[i][j] = 3; } } int mx = 0, cn = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (mat[i][j] == -1) continue; init(); int cnt = 0; int r = i, c = j, d = mat[r][c]; while (r != -1 && c != -1) { del(r, c); if (mat[r][c] != -1) d = mat[r][c], cnt++; if (d == 0 || d == 2) r = dir[d][r][c]; else c = dir[d][r][c]; } if (cnt > mx) mx = cnt, cn = 1; else if (cnt == mx) cn++; } } printf("%d %d\n", mx, cn); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; const char *dc = "LRUD"; int get(vector<int> &t, int x) { assert(x >= 0 && x < ((int)(t).size())); if (t[x] == -1) return x; return t[x] = get(t, t[x]); } const int MAXW = 5000; char buf[MAXW + 1]; int main() { int h, w; while (scanf("%d%d", &h, &w) >= 2) { vector<string> f(h); for (int y = 0; y < h; y++) scanf("%s", buf), f[y] = buf; vector<vector<int> > trl(h, vector<int>(w + 2, -1)), trr(h, vector<int>(w + 2, -1)); vector<vector<int> > tru(w, vector<int>(h + 2, -1)), trd(w, vector<int>(h + 2, -1)); int mans = -1, mcnt = 0; for (int sy = 0; sy < h; sy++) { for (int sx = 0; sx < w; sx++) if (f[sy][sx] != '.') { for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) if (f[y][x] == '.') { trl[y][x + 1] = x; trr[y][x + 1] = x + 2; tru[x][y + 1] = y; trd[x][y + 1] = y + 2; ; } else { trl[y][x + 1] = trr[y][x + 1] = tru[x][y + 1] = trd[x][y + 1] = -1; ; } int ans = 0; int y = sy, x = sx; while (1) { ans++; int d = 0; for (; assert(d < 4), dc[d] != f[y][x]; d++) ; trl[y][x + 1] = x; trr[y][x + 1] = x + 2; tru[x][y + 1] = y; trd[x][y + 1] = y + 2; ; if (x < 0 || y < 0 || x >= w || y >= h) break; switch (d) { case 0: x = get(trl[y], x + 1) - 1; break; case 1: x = get(trr[y], x + 1) - 1; break; case 2: y = get(tru[x], y + 1) - 1; break; case 3: y = get(trd[x], y + 1) - 1; break; } if (x < 0 || y < 0 || x >= w || y >= h) break; } if (mans < ans) { mans = ans; mcnt = 0; } if (mans == ans) mcnt++; } } printf("%d %d\n", mans, mcnt); break; } return 0; }
#include <bits/stdc++.h> using namespace std; int const N = 5555; int n, m; string s, _s; vector<int> _L, _R, _U, _D; vector<int> L, R, U, D; void rm(int x) { if (D[x] != -1) U[D[x]] = U[x]; if (U[x] != -1) D[U[x]] = D[x]; if (R[x] != -1) L[R[x]] = L[x]; if (L[x] != -1) R[L[x]] = R[x]; } int dfs(int x) { if (x < 0) return 0; rm(x); if (s[x] == 'U') return dfs(U[x]) + 1; if (s[x] == 'D') return dfs(D[x]) + 1; if (s[x] == 'L') return dfs(L[x]) + 1; if (s[x] == 'R') return dfs(R[x]) + 1; throw ""; return 0x12345678; } int main() { ios_base ::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> _s; s += _s; } _L.resize(n * m), _R.resize(n * m), _U.resize(n * m), _D.resize(n * m); L.resize(n * m), R.resize(n * m), U.resize(n * m), D.resize(n * m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { int x = i * m + j, l = i * m + j - 1, u = (i - 1) * m + j; if (j == 0) _L[x] = -1; else _L[x] = s[l] == '.' ? _L[l] : l; if (i == 0) _U[x] = -1; else _U[x] = s[u] == '.' ? _U[u] : u; } for (int i = n - 1; i >= 0; --i) for (int j = m - 1; j >= 0; --j) { int x = i * m + j, r = i * m + j + 1, d = (i + 1) * m + j; if (j == m - 1) _R[x] = -1; else _R[x] = s[r] == '.' ? _R[r] : r; if (i == n - 1) _D[x] = -1; else _D[x] = s[d] == '.' ? _D[d] : d; } int mx = -1, cnt = 0; for (int i = 0; i < n * m; ++i) if (s[i] != '.') { L = _L, R = _R, U = _U, D = _D; int cur = dfs(i); if (cur > mx) mx = cur, cnt = 1; else if (cur == mx) ++cnt; } cout << mx << ' ' << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int l, r, u, d, x, y; void init() { l = r = u = d = x = y = 0; } }; node tmp[5010], p[5010]; vector<char> G[5010], a[5010]; vector<int> pos[5010]; int n, m, size; int ans, sum, cnt; int left(int x, int y) { for (int i = y - 1; i >= 0; i--) { if (G[x][i] != '.') return pos[x][i]; } return -1; } int right(int x, int y) { for (int i = y + 1; i < m; i++) { if (G[x][i] != '.') return pos[x][i]; } return -1; } int up(int x, int y) { for (int i = x - 1; i >= 0; i--) { if (G[i][y] != '.') return pos[i][y]; } return -1; } int down(int x, int y) { for (int i = x + 1; i < n; i++) { if (G[i][y] != '.') return pos[i][y]; } return -1; } void Delete(int id) { tmp[tmp[id].l].r = tmp[id].r; tmp[tmp[id].r].l = tmp[id].l; tmp[tmp[id].u].d = tmp[id].d; tmp[tmp[id].d].u = tmp[id].u; } void gao(int x, int y) { int i, j, id; for (i = 1; i <= size; i++) tmp[i] = p[i]; id = pos[x][y]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) G[i][j] = a[i][j]; } while (id != -1) { sum++; x = tmp[id].x; y = tmp[id].y; Delete(id); if (G[x][y] == 'L') id = tmp[id].l; else if (G[x][y] == 'R') id = tmp[id].r; else if (G[x][y] == 'U') id = tmp[id].u; else if (G[x][y] == 'D') id = tmp[id].d; else break; G[x][y] = '.'; } } int main() { int i, j; while (~scanf("%d%d", &n, &m)) { for (i = 0; i < n; i++) { G[i].clear(); G[i].resize(m); pos[i].clear(); pos[i].resize(m); a[i].clear(); a[i].resize(m); } size = 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &G[i][j]); a[i][j] = G[i][j]; if (a[i][j] != '.') { pos[i][j] = ++size; p[size].x = i; p[size].y = j; } else pos[i][j] = 0; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (a[i][j] == '.') continue; p[pos[i][j]].l = left(i, j); p[pos[i][j]].r = right(i, j); p[pos[i][j]].u = up(i, j); p[pos[i][j]].d = down(i, j); } } ans = cnt = 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (a[i][j] == '.') continue; sum = 0; gao(i, j); if (sum > ans) { ans = sum; cnt = 1; } else if (sum == ans) cnt++; } } printf("%d %d\n", ans, cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int d, dir[4]; } e[5050]; int dx[5] = {0, 0, -1, 1}; int dy[5] = {-1, 1, 0, 0}; int n, m, ans = -1, sum = 0; int dir(int s, int i) { int x = s / m, y = s % m; while (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m) { x += dx[i]; y += dy[i]; if (e[x * m + y].d == -1) continue; else return x * m + y; } return -1; } void init() { scanf("%d%d", &n, &m); getchar(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int s = i * m + j; char c; scanf("%c", &c); if (c == 'L') e[s].d = 0; if (c == 'R') e[s].d = 1; if (c == 'U') e[s].d = 2; if (c == 'D') e[s].d = 3; if (c == '.') e[s].d = -1; } getchar(); } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int s = i * m + j; e[s].dir[0] = dir(s, 0); e[s].dir[1] = dir(s, 1); e[s].dir[2] = dir(s, 2); e[s].dir[3] = dir(s, 3); } } void dfs(int s, int t) { int i = e[s].d, o = 0; if (e[s].dir[i] != -1) { o = 1; e[e[s].dir[0]].dir[1] = e[s].dir[1]; e[e[s].dir[1]].dir[0] = e[s].dir[0]; e[e[s].dir[2]].dir[3] = e[s].dir[3]; e[e[s].dir[3]].dir[2] = e[s].dir[2]; dfs(e[s].dir[i], t + 1); e[e[s].dir[0]].dir[1] = s; e[e[s].dir[1]].dir[0] = s; e[e[s].dir[2]].dir[3] = s; e[e[s].dir[3]].dir[2] = s; } if (!o) { if (t > ans) { ans = t; sum = 1; } else if (t == ans) sum++; } } int main() { init(); for (int i = 0; i < m * n; i++) if (e[i].d != -1) dfs(i, 1); printf("%d %d\n", ans, sum); return 0; }
#include <bits/stdc++.h> using namespace std; const int L = 0, R = 1, U = 2, D = 3; const int maxn = 5005; class Node { public: int dir, l, r, u, d; } a[maxn], b[maxn]; int n, m; char s[maxn]; inline int id(int x, int y) { return x * m + y; } inline void build(int x, int y) { Node &u = a[id(x, y)]; int nx, ny; for (nx = x, ny = y - 1; ny >= 0 && s[id(nx, ny)] == '.'; --ny) ; if (ny < 0) u.l = -1; else u.l = id(nx, ny); for (nx = x, ny = y + 1; ny < m && s[id(nx, ny)] == '.'; ++ny) ; if (ny >= m) u.r = -1; else u.r = id(nx, ny); for (nx = x - 1, ny = y; nx >= 0 && s[id(nx, ny)] == '.'; --nx) ; if (nx < 0) u.u = -1; else u.u = id(nx, ny); for (nx = x + 1, ny = y; nx < n && s[id(nx, ny)] == '.'; ++nx) ; if (nx >= n) u.d = -1; else u.d = id(nx, ny); return; } inline void connectX(int u, int v) { if (~u) b[u].r = v; if (~v) b[v].l = u; } inline void connectY(int u, int v) { if (~u) b[u].d = v; if (~v) b[v].u = u; return; } inline void erase(int u) { connectX(b[u].l, b[u].r); connectY(b[u].u, b[u].d); return; } inline void dfs(int x, int &res) { Node &u = b[x]; ++res; if (u.dir == L) { if (!~u.l) return; erase(x); dfs(u.l, res); } else if (u.dir == R) { if (!~u.r) return; erase(x); dfs(u.r, res); } else if (u.dir == U) { if (!~u.u) return; erase(x); dfs(u.u, res); } else { if (!~u.d) return; erase(x); dfs(u.d, res); } return; } int main() { cin >> n >> m; for (int i = (0); i < (n); ++i) { string buf; cin >> buf; for (int j = (0); j < (m); ++j) { s[id(i, j)] = buf[j]; if (buf[j] == 'L') a[id(i, j)].dir = L; else if (buf[j] == 'R') a[id(i, j)].dir = R; else if (buf[j] == 'U') a[id(i, j)].dir = U; else if (buf[j] == 'D') a[id(i, j)].dir = D; } } for (int x = (0); x < (n); ++x) for (int y = (0); y < (m); ++y) a[id(x, y)].l = a[id(x, y)].r = a[id(x, y)].u = a[id(x, y)].d = -1; for (int x = (0); x < (n); ++x) for (int y = (0); y < (m); ++y) build(x, y); int ans = 0, cnt = 0; for (int i = (0); i < (n); ++i) for (int j = (0); j < (m); ++j) if (s[id(i, j)] != '.') { for (int x = (0); x < (n); ++x) for (int y = (0); y < (m); ++y) b[id(x, y)] = a[id(x, y)]; int res = 0; dfs(id(i, j), res); if (res > ans) ans = res, cnt = 1; else if (res == ans) ++cnt; } printf("%d %d\n", ans, cnt); return 0; }
#include <bits/stdc++.h> using namespace std; struct point { point *l, *r, *u, *d; int id; } p[5010], q[5010], *ii, *jj; int i, j, n, m; int num = 0; char s[5010]; inline void rm(point* pp) { if (pp->l) { pp->l->r = pp->r; } if (pp->r) { pp->r->l = pp->l; } if (pp->d) { pp->d->u = pp->u; } if (pp->u) { pp->u->d = pp->d; } } int main() { scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &s[num]); p[num].id = num; if (i) { assert(num >= m); p[num].u = &p[num - m]; p[num - m].d = &p[num]; } if (j) { p[num].l = &p[num - 1]; p[num - 1].r = &p[num]; } num++; } } for (i = 0; i < num; i++) { if (s[i] == '.') { rm(&p[i]); } } int ans = 0, ansnum = 0, now; memcpy(q, p, sizeof(p)); for (i = 0; i < num; i++) { if (s[i] == '.') { continue; } if (i) memcpy(p, q, sizeof(q)); ii = &p[i]; now = 0; while (ii) { now++; rm(ii); switch (s[ii->id]) { case 'D': ii = ii->d; break; case 'U': ii = ii->u; break; case 'L': ii = ii->l; break; case 'R': ii = ii->r; break; default: assert(0); } } if (now > ans) { ans = now; ansnum = 1; } else if (now == ans) { ansnum++; } } printf("%d %d\n", ans, ansnum); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<string> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int A[n][m][4]; memset(A, -1, sizeof A); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (v[i][j] != '.') { for (int k = i - 1; k >= 0; k--) if (v[k][j] != '.') { A[i][j][0] = k; break; } for (int k = j + 1; k < m; k++) if (v[i][k] != '.') { A[i][j][1] = k; break; } for (int k = i + 1; k < n; k++) if (v[k][j] != '.') { A[i][j][2] = k; break; } for (int k = j - 1; k >= 0; k--) if (v[i][k] != '.') { A[i][j][3] = k; break; } } } int Aux[n][m][4]; int mx = 0; int C[5001] = {}; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (v[i][j] != '.') { for (int k = 0; k < n; k++) for (int l = 0; l < m; l++) for (int f = 0; f < 4; f++) Aux[k][l][f] = A[k][l][f]; int di = i; int dj = j; int r = 0; while (di != -1 && dj != -1) { r++; if (Aux[di][dj][0] != -1) Aux[Aux[di][dj][0]][dj][2] = Aux[di][dj][2]; if (Aux[di][dj][1] != -1) Aux[di][Aux[di][dj][1]][3] = Aux[di][dj][3]; if (Aux[di][dj][2] != -1) Aux[Aux[di][dj][2]][dj][0] = Aux[di][dj][0]; if (Aux[di][dj][3] != -1) Aux[di][Aux[di][dj][3]][1] = Aux[di][dj][1]; int ni = di, nj = dj; if (v[di][dj] == 'R') nj = Aux[di][dj][1]; else if (v[di][dj] == 'L') nj = Aux[di][dj][3]; else if (v[di][dj] == 'D') ni = Aux[di][dj][2]; else if (v[di][dj] == 'U') ni = Aux[di][dj][0]; di = ni; dj = nj; } mx = max(mx, r); C[r]++; } } cout << mx << " " << C[mx]; }
#include <bits/stdc++.h> using namespace std; const int Maxn = 2e5 + 7; const int Max = 5e3 + 7; const int Mod = 1e9 + 7; const int Inf = 2e9 + 1e8; int a[Max][Max], g[Max][Max], p[5][Max], t; pair<int, int> G[Max]; void dfs(int u) { if (u == 0) return; t++; int U = p[1][u], D = p[2][u]; int R = p[3][u], L = p[4][u]; p[2][U] = D, p[1][D] = U; p[4][R] = L, p[3][L] = R; pair<int, int> v = G[u]; int jahat = a[v.first][v.second]; dfs(p[jahat][u]); p[2][U] = u, p[1][D] = u; p[4][R] = u, p[3][L] = u; } int change(char t, int R) { if (t == '.') return 0; if (t == 'U') return 1; if (t == 'D') return 2; if (t == 'R') return 3; else return 4; } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char C; cin >> C; a[i][j] = change(C, 0); } } int k = 0; for (int i = 1; i <= n; i++) { int tmp = 0; for (int j = 1; j <= m; j++) { k++; g[i][j] = k; G[k] = make_pair(i, j); if (a[i][j] == 0) continue; p[4][k] = tmp; if (tmp) p[3][tmp] = k; tmp = k; } } for (int i = 1; i <= m; i++) { int tmp = 0; for (int j = n; j >= 1; j--) { if (a[j][i] == 0) continue; k = g[j][i]; p[2][k] = tmp; if (tmp) p[1][tmp] = k; tmp = k; } } int ans = 0, p = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { t = 0; if (a[i][j] == 0) continue; dfs(g[i][j]); if (ans < t) p = 1; if (ans == t) p++; ans = max(ans, t); } } printf("%d %d", ans, p); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; const int inf = 5100; typedef struct a { int pos, num; } per; short n, m, si[maxn][maxn], ans, ctt; short tl, tr, tu, td, l[maxn][maxn], r[maxn][maxn], up[maxn][maxn], down[maxn][maxn]; void change(int x, int y) { tl = l[x][y], tr = r[x][y], tu = up[x][y], td = down[x][y]; if (tl != inf) r[x][tl] = tr; if (tr != inf) l[x][tr] = tl; if (td != inf) up[td][y] = tu; if (tu != inf) down[tu][y] = td; } void go_back(int x, int y, int ttl, int ttr, int ttu, int ttd) { if (ttl != inf) r[x][ttl] = y; if (ttr != inf) l[x][ttr] = y; if (ttd != inf) up[ttd][y] = x; if (ttu != inf) down[ttu][y] = x; } void print() { cout << "TEST\n"; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cout << l[i][j] << ' '; } cout << "\n"; } } void dfs(int x, int y, int cnt) { if (x == inf || y == inf) { if (cnt == ans) ctt++; else if (cnt > ans) { ans = cnt; ctt = 1; } return; } change(x, y); int ttl = tl, ttr = tr, ttu = tu, ttd = td; if (si[x][y] == 0) dfs(up[x][y], y, cnt + 1); else if (si[x][y] == 1) dfs(down[x][y], y, cnt + 1); else if (si[x][y] == 2) dfs(x, l[x][y], cnt + 1); else dfs(x, r[x][y], cnt + 1); go_back(x, y, ttl, ttr, ttu, ttd); return; } int main() { cin >> n >> m; char tc; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> tc; if (tc == '.') si[i][j] = -1; else if (tc == 'U') si[i][j] = 0; else if (tc == 'D') si[i][j] = 1; else if (tc == 'L') si[i][j] = 2; else si[i][j] = 3; } } for (int i = 1; i <= n; ++i) { int last = inf; for (int j = 1; j <= m; ++j) { if (si[i][j] != -1) { l[i][j] = last; last = j; } } last = inf; for (int j = m; j >= 1; --j) { if (si[i][j] != -1) { r[i][j] = last; last = j; } } } for (int j = 1; j <= m; ++j) { int last = inf; for (int i = 1; i <= n; ++i) { if (si[i][j] != -1) { up[i][j] = last; last = i; } } last = inf; for (int i = n; i >= 1; --i) { if (si[i][j] != -1) { down[i][j] = last; last = i; } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (si[i][j] != -1) dfs(i, j, 0); } } cout << ans << ' ' << ctt; return 0; }
#include <bits/stdc++.h> int a[10001], up[10001], down[10001], left[10001], right[10001], n, m, now, ans, total; bool b[10001]; void readin() { int i, j, last[10001], pre, num; char tmp, ch; scanf("%d %d", &n, &m); scanf("%c", &tmp); num = n * m; for (i = 1; i <= m; ++i) last[10001] = -1; for (i = 1; i <= num; ++i) up[i] = down[i] = left[i] = right[i] = -1; for (i = 1; i <= n; ++i) { pre = -1; for (j = 1; j <= m; ++j) { b[(i - 1) * m + j] = false; scanf("%c", &ch); switch (ch) { case '.': a[(i - 1) * m + j] = 0; break; case 'U': a[(i - 1) * m + j] = 1; break; case 'D': a[(i - 1) * m + j] = 2; break; case 'L': a[(i - 1) * m + j] = 3; break; case 'R': a[(i - 1) * m + j] = 4; break; } if (ch == '.') continue; if (last[j] > 0) { up[(i - 1) * m + j] = last[j]; down[last[j]] = (i - 1) * m + j; } last[j] = (i - 1) * m + j; if (pre > 0) { left[(i - 1) * m + j] = pre; right[pre] = (i - 1) * m + j; } pre = (i - 1) * m + j; } scanf("%c", &tmp); } } void del(int t) { if (up[t] > 0) down[up[t]] = down[t]; if (down[t] > 0) up[down[t]] = up[t]; if (left[t] > 0) right[left[t]] = right[t]; if (right[t] > 0) left[right[t]] = left[t]; } void back(int t) { if (up[t] > 0) down[up[t]] = t; if (down[t] > 0) up[down[t]] = t; if (left[t] > 0) right[left[t]] = t; if (right[t] > 0) left[right[t]] = t; } void search(int t) { int i, x, y; if (t < 0 || t > n * m) return; y = t % m; if (y == 0) { x = t / m; y = m; } else x = t / m + 1; b[t] = true; ++now; if (now == ans) ++total; if (now > ans) { ans = now; total = 1; } switch (a[t]) { case 1: del(t); search(up[t]); back(t); break; case 2: del(t); search(down[t]); back(t); break; case 3: del(t); search(left[t]); back(t); break; case 4: del(t); search(right[t]); back(t); break; } --now; b[t] = false; } void solve() { int i, j; for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) if (a[(i - 1) * m + j] > 0) { now = 0; search((i - 1) * m + j); if (now > ans) { ans = now; total = 1; continue; } if (now == ans) ++total; } } void print() { printf("%d %d\n", ans, total); } int main() { readin(); solve(); print(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; char mat[MAXN], tmp[MAXN]; int setx[MAXN][4]; int n, m, total; int cnt[MAXN]; static int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; inline int GetK(const char c) { switch (c) { case 'D': return 0; case 'R': return 1; case 'U': return 2; default: return 3; } } inline bool Inside(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } inline int InitSet(int No, int k) { int x = No / m, y = No % m; x += dir[k][0]; y += dir[k][1]; if (Inside(x, y) == false) return -1; return x * m + y; } inline int FindNext(int no, int k) { if (setx[no][k] == -1) return -1; if (mat[setx[no][k]] != '.') return setx[no][k]; return setx[no][k] = FindNext(setx[no][k], k); } int Solve(int cur) { int ret = 0; while (cur != -1) { int nextno = FindNext(cur, GetK(mat[cur])); mat[cur] = '.'; ret++; cur = nextno; } return ret; puts(""); } int main() { while (cin >> n >> m) { memset(cnt, 0, sizeof(cnt)); total = n * m; for (int i = 0; i < total; i++) { cin >> tmp[i]; } for (int i = 0; i < total; i++) { if (tmp[i] == '.') continue; memcpy(mat, tmp, sizeof(char) * total); for (int j = 0; j < total; j++) { for (int k = 0; k < 4; k++) { setx[j][k] = InitSet(j, k); } } cnt[Solve(i)]++; } for (int i = total; i >= 0; i--) { if (cnt[i] > 0) { cout << i << ' ' << cnt[i] << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int gr[5000][5], n, m; string s[5000]; bool isOK(int x, int y) { if (x >= 0 && x < n && y >= 0 && y < m) return true; return false; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; map<char, int> mp; mp['U'] = 0; mp['R'] = 1; mp['D'] = 2; mp['L'] = 3; mp['.'] = -1; int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x = i * m + j; gr[x][4] = mp[s[i][j]]; if (gr[x][4] != -1) for (int k = 0; k < 4; k++) { int x1 = i + d[k][0], y1 = j + d[k][1]; while (isOK(x1, y1) && s[x1][y1] == '.') { x1 += d[k][0]; y1 += d[k][1]; } if (isOK(x1, y1)) { int y = x1 * m + y1; gr[x][k] = y; } else gr[x][k] = -1; } } } int gr2[5000][5]; int ans = 0, num = 0; for (int i = 0; i < n * m; i++) { if (gr[i][4] != -1) { memcpy(gr2, gr, sizeof gr2); int x = i; int k = 1; while (true) { int y = gr2[x][4]; if (gr2[x][y] == -1) break; for (int i = 0; i < 4; i++) { if (gr2[x][i] != -1) gr2[gr2[x][i]][(i + 2) % 4] = gr2[x][(i + 2) % 4]; } x = gr2[x][y]; k++; } if (k > ans) { ans = k; num = 1; } else if (k == ans) num++; } } cout << ans << " " << num << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s[6000]; int n, m; vector<pair<int, int> > up[6000], dn[6000], lt[6000], rt[6000]; vector<int> d[6000]; int calc(int x, int y) { for (int i = 0; i < n; i++) lt[i][0] = make_pair(-1, -1); for (int i = 0; i < n; i++) for (int j = 1; j < m; j++) if (s[i][j - 1] != '.') lt[i][j] = make_pair(i, j - 1); else lt[i][j] = lt[i][j - 1]; for (int i = 0; i < n; i++) rt[i][m - 1] = make_pair(-1, -1); for (int i = 0; i < n; i++) for (int j = m - 2; j >= 0; j--) if (s[i][j + 1] != '.') rt[i][j] = make_pair(i, j + 1); else rt[i][j] = rt[i][j + 1]; for (int i = 0; i < m; i++) up[0][i] = make_pair(-1, -1); for (int i = 0; i < m; i++) for (int j = 1; j < n; j++) if (s[j - 1][i] != '.') up[j][i] = make_pair(j - 1, i); else up[j][i] = up[j - 1][i]; for (int i = 0; i < m; i++) dn[n - 1][i] = make_pair(-1, -1); for (int i = 0; i < m; i++) for (int j = n - 2; j >= 0; j--) if (s[j + 1][i] != '.') dn[j][i] = make_pair(j + 1, i); else dn[j][i] = dn[j + 1][i]; int res = 0; while (true) { res++; if (s[x][y] == 'L') { pair<int, int> p = lt[x][y]; if (p == make_pair(-1, -1)) return res; if (rt[x][y] != make_pair(-1, -1)) lt[rt[x][y].first][rt[x][y].second] = lt[x][y]; if (lt[x][y] != make_pair(-1, -1)) rt[lt[x][y].first][lt[x][y].second] = rt[x][y]; if (dn[x][y] != make_pair(-1, -1)) up[dn[x][y].first][dn[x][y].second] = up[x][y]; if (up[x][y] != make_pair(-1, -1)) dn[up[x][y].first][up[x][y].second] = dn[x][y]; x = p.first, y = p.second; } else if (s[x][y] == 'R') { pair<int, int> p = rt[x][y]; if (p == make_pair(-1, -1)) return res; if (rt[x][y] != make_pair(-1, -1)) lt[rt[x][y].first][rt[x][y].second] = lt[x][y]; if (lt[x][y] != make_pair(-1, -1)) rt[lt[x][y].first][lt[x][y].second] = rt[x][y]; if (dn[x][y] != make_pair(-1, -1)) up[dn[x][y].first][dn[x][y].second] = up[x][y]; if (up[x][y] != make_pair(-1, -1)) dn[up[x][y].first][up[x][y].second] = dn[x][y]; x = p.first, y = p.second; } else if (s[x][y] == 'U') { pair<int, int> p = up[x][y]; if (p == make_pair(-1, -1)) return res; if (rt[x][y] != make_pair(-1, -1)) lt[rt[x][y].first][rt[x][y].second] = lt[x][y]; if (lt[x][y] != make_pair(-1, -1)) rt[lt[x][y].first][lt[x][y].second] = rt[x][y]; if (dn[x][y] != make_pair(-1, -1)) up[dn[x][y].first][dn[x][y].second] = up[x][y]; if (up[x][y] != make_pair(-1, -1)) dn[up[x][y].first][up[x][y].second] = dn[x][y]; x = p.first, y = p.second; } else if (s[x][y] == 'D') { pair<int, int> p = dn[x][y]; if (p == make_pair(-1, -1)) return res; if (rt[x][y] != make_pair(-1, -1)) lt[rt[x][y].first][rt[x][y].second] = lt[x][y]; if (lt[x][y] != make_pair(-1, -1)) rt[lt[x][y].first][lt[x][y].second] = rt[x][y]; if (dn[x][y] != make_pair(-1, -1)) up[dn[x][y].first][dn[x][y].second] = up[x][y]; if (up[x][y] != make_pair(-1, -1)) dn[up[x][y].first][up[x][y].second] = dn[x][y]; x = p.first, y = p.second; } } return res; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) up[i].resize(m), dn[i].resize(m), lt[i].resize(m), rt[i].resize(m), d[i].resize(m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] != '.') d[i][j] = calc(i, j); int best = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] != '.' && d[i][j] > best) best = d[i][j]; int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] != '.' && d[i][j] == best) ans++; printf("%d %d\n", best, ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5000; int n, m; char s[N + 10][N + 10]; int L[N + 10], R[N + 10], U[N + 10], D[N + 10], l[N + 10], r[N + 10], u[N + 10], d[N + 10]; int rec[N + 10][N + 10], id[N + 10][2]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%s", s[i]); memset(L, 0, sizeof(L)); memset(R, 0, sizeof(R)); memset(U, 0, sizeof(U)); memset(D, 0, sizeof(D)); memset(id, 0, sizeof(id)); memset(rec, 0, sizeof(rec)); int tot = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (s[i][j] != '.') { rec[i][j] = ++tot; id[tot][0] = i; id[tot][1] = j; } for (int i = 0; i < n; ++i) { int e; for (int j = 0, e = 0; j < m; ++j) { if (s[i][j] != '.') { if (e > 0) L[rec[i][j]] = e, e = rec[i][j]; else e = rec[i][j]; } } for (int j = m - 1, e = 0; j >= 0; --j) { if (s[i][j] != '.') { if (e > 0) R[rec[i][j]] = e, e = rec[i][j]; else e = rec[i][j]; } } } for (int j = 0; j < m; ++j) { int e; for (int i = 0, e = 0; i < n; ++i) { if (s[i][j] != '.') { if (e > 0) U[rec[i][j]] = e, e = rec[i][j]; else e = rec[i][j]; } } for (int i = n - 1, e = 0; i >= 0; --i) { if (s[i][j] != '.') { if (e > 0) D[rec[i][j]] = e, e = rec[i][j]; else e = rec[i][j]; } } } int best = 0; int cnt = 0; for (int i = 1; i <= tot; ++i) { for (int j = 1; j <= tot; ++j) l[j] = L[j], r[j] = R[j], u[j] = U[j], d[j] = D[j]; int count = 0; int cur = i; int next; for (;;) { count++; if (s[id[cur][0]][id[cur][1]] == 'L') next = l[cur]; if (s[id[cur][0]][id[cur][1]] == 'R') next = r[cur]; if (s[id[cur][0]][id[cur][1]] == 'U') next = u[cur]; if (s[id[cur][0]][id[cur][1]] == 'D') next = d[cur]; if (l[cur] != 0) r[l[cur]] = r[cur]; if (r[cur] != 0) l[r[cur]] = l[cur]; if (u[cur] != 0) d[u[cur]] = d[cur]; if (d[cur] != 0) u[d[cur]] = u[cur]; if (next == 0) break; cur = next; } if (count > best) { best = count; cnt = 1; } else if (count == best) { cnt++; } } printf("%d %d\n", best, cnt); return 0; }
#include <bits/stdc++.h> short L[5005][5005], R[5005][5005], U[5005][5005], D[5005][5005]; char q[5005][5005]; int n, m, av, aw, sp; void gao(int x, int y) { if (x < 1 || y < 1 || x > m || y > n) return; ++sp; L[x][R[x][y]] = L[x][y]; R[x][L[x][y]] = R[x][y]; U[D[x][y]][y] = U[x][y]; D[U[x][y]][y] = D[x][y]; if (q[x][y] == 'L') gao(x, L[x][y]); if (q[x][y] == 'R') gao(x, R[x][y]); if (q[x][y] == 'U') gao(U[x][y], y); if (q[x][y] == 'D') gao(D[x][y], y); L[x][R[x][y]] = y; R[x][L[x][y]] = y; U[D[x][y]][y] = x; D[U[x][y]][y] = x; } int main() { scanf("%d%d", &m, &n); for (int i = 1; i <= m; i++) scanf("%s", q[i] + 1); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) L[i][j] = q[i][j - 1] != '.' ? j - 1 : L[i][j - 1]; for (int j = n; j > 0; j--) R[i][j] = q[i][j + 1] != '.' ? j + 1 : R[i][j + 1]; } for (int j = 1; j <= n; j++) { for (int i = 1; i <= m; i++) U[i][j] = q[i - 1][j] != '.' ? i - 1 : U[i - 1][j]; for (int i = m; i > 0; i--) D[i][j] = q[i + 1][j] != '.' ? i + 1 : D[i + 1][j]; } for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if (q[i][j] != '.') { sp = 0; gao(i, j); if (sp == av) ++aw; else if (sp > av) av = sp, aw = 1; } printf("%d %d\n", av, aw); }
#include <bits/stdc++.h> using namespace std; struct pkt { int x, y; }; int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<string> p(n); for (int i = 0; i < n; ++i) cin >> p[i]; vector<vector<int> > a(n, vector<int>(m, -1)); for (int k = 0; k < n; ++k) { for (int l = 1; l < m; ++l) { if (p[k][l - 1] != '.') a[k][l] = l - 1; else a[k][l] = a[k][l - 1]; } } vector<vector<int> > b(n, vector<int>(m, -1)); for (int k = 0; k < n; ++k) { for (int l = m - 2; l >= 0; --l) { if (p[k][l + 1] != '.') b[k][l] = l + 1; else b[k][l] = b[k][l + 1]; } } vector<vector<int> > c(n, vector<int>(m, -1)); for (int l = 0; l < m; ++l) { for (int k = 1; k < n; ++k) { if (p[k - 1][l] != '.') c[k][l] = k - 1; else c[k][l] = c[k - 1][l]; } } vector<vector<int> > d(n, vector<int>(m, -1)); for (int l = 0; l < m; ++l) { for (int k = n - 2; k >= 0; --k) { if (p[k + 1][l] != '.') d[k][l] = k + 1; else d[k][l] = d[k + 1][l]; } } vector<vector<int> > L, P, G, D; L = a; P = b; G = c; D = d; int odp = 0, il = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (p[i][j] == '.') continue; int dl = 1; pkt akt = {i, j}; vector<pkt> z; while (true) { pkt nast; if (p[akt.x][akt.y] == 'L') nast = {akt.x, L[akt.x][akt.y]}; else { if (p[akt.x][akt.y] == 'R') nast = {akt.x, P[akt.x][akt.y]}; else { if (p[akt.x][akt.y] == 'U') nast = {G[akt.x][akt.y], akt.y}; else { if (p[akt.x][akt.y] == 'D') nast = {D[akt.x][akt.y], akt.y}; } } } if (nast.x == -1 || nast.y == -1) break; ++dl; pkt le = {akt.x, L[akt.x][akt.y]}; pkt pr = {akt.x, P[akt.x][akt.y]}; if (le.x != -1 && le.y != -1) P[le.x][le.y] = pr.y; if (pr.x != -1 && pr.y != -1) L[pr.x][pr.y] = le.y; pkt go = {G[akt.x][akt.y], akt.y}; pkt dol = {D[akt.x][akt.y], akt.y}; if (dol.x != -1 && dol.y != -1) G[dol.x][dol.y] = go.x; if (go.x != -1 && go.y != -1) D[go.x][go.y] = dol.x; akt = nast; z.push_back(le); z.push_back(pr); z.push_back(go); z.push_back(dol); } if (dl > odp) { odp = dl; il = 1; } else { if (dl == odp) ++il; } for (int k = 0; k < z.size(); ++k) { if (z[k].x == -1 || z[k].y == -1) continue; L[z[k].x][z[k].y] = a[z[k].x][z[k].y]; P[z[k].x][z[k].y] = b[z[k].x][z[k].y]; G[z[k].x][z[k].y] = c[z[k].x][z[k].y]; D[z[k].x][z[k].y] = d[z[k].x][z[k].y]; } } } cout << odp << " " << il; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline T abs(T a) { return a > 0 ? a : -a; } int d[5001], n, m; pair<int, int> p[5001][5]; int d1[5001]; pair<int, int> p1[5001][5]; void sP(int x, int y, int k, pair<int, int> a) { p[x * m + y][k] = a; } pair<int, int> gP(int x, int y, int k) { return p[x * m + y][k]; } void sD(int x, int y, int a) { d[x * m + y] = a; } int gD(int x, int y) { return d[x * m + y]; } void sP1(int x, int y, int k, pair<int, int> a) { p1[x * m + y][k] = a; } pair<int, int> gP1(int x, int y, int k) { return p1[x * m + y][k]; } void sD1(int x, int y, int a) { d1[x * m + y] = a; } int gD1(int x, int y) { return d1[x * m + y]; } const int dx[4] = {0, -1, 0, 1}; const int dy[4] = {1, 0, -1, 0}; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (s[j] == 'R') sD(i, j, 0); else if (s[j] == 'U') sD(i, j, 1); else if (s[j] == 'L') sD(i, j, 2); else if (s[j] == 'D') sD(i, j, 3); else sD(i, j, -1); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int d = 0; d < 4; d++) { int x = i + dx[d], y = j + dy[d]; while (0 <= x && x < n && 0 <= y && y < m) { if (gD(x, y) >= 0) break; x += dx[d]; y += dy[d]; } if (0 <= x && x < n && 0 <= y && y < m) { sP(i, j, d, make_pair(x, y)); } else sP(i, j, d, make_pair(-1, -1)); } } } vector<int> ans; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int d = gD(i, j); if (d < 0) continue; for (int x = 0; x < n * m; x++) { for (int y = 0; y < 4; y++) p1[x][y] = p[x][y]; } pair<int, int> cur = make_pair(i, j); int res = 0; while (cur.first >= 0) { res++; int d = gD(cur.first, cur.second); pair<int, int> next = gP1(cur.first, cur.second, d); for (int k = 0; k < 4; k++) { pair<int, int> q = gP1(cur.first, cur.second, k); if (q.first >= 0) sP1(q.first, q.second, (k + 2) % 4, gP1(cur.first, cur.second, (k + 2) % 4)); } cur = next; } ans.push_back(res); } } sort(ans.begin(), ans.end()); int K = 0; for (int i = 0; i < (int)ans.size(); i++) if (ans[i] == ans.back()) K++; cout << ans.back() << ' ' << K << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int U, D, L, R; } a[5010]; char s[5010]; int n, m; int ans, ansp; void cover(int p) { if (a[p].L > -1) { int k = a[p].L; a[k].R = a[p].R; } if (a[p].R > -1) { int k = a[p].R; a[k].L = a[p].L; } if (a[p].U > -1) { int k = a[p].U; a[k].D = a[p].D; } if (a[p].D > -1) { int k = a[p].D; a[k].U = a[p].U; } } void recover(int p) { if (a[p].D > -1) a[a[p].D].U = p; if (a[p].U > -1) a[a[p].U].D = p; if (a[p].R > -1) a[a[p].R].L = p; if (a[p].L > -1) a[a[p].L].R = p; } int go(int p) { int ret = 1; if (s[p] == 'L') { cover(p); if (a[p].L > -1) ret += go(a[p].L); recover(p); } else if (s[p] == 'U') { cover(p); if (a[p].U > -1) ret += go(a[p].U); recover(p); } else if (s[p] == 'R') { cover(p); if (a[p].R > -1) ret += go(a[p].R); recover(p); } else { cover(p); if (a[p].D > -1) ret += go(a[p].D); recover(p); } return ret; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", s + m * i); int last[5010]; memset(last, -1, sizeof(last)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int p = i * m + j; if (s[p] != '.') { a[p].U = last[j]; last[j] = p; } } memset(last, -1, sizeof(last)); for (int i = n - 1; i >= 0; i--) for (int j = 0; j < m; j++) { int p = i * m + j; if (s[p] != '.') { a[p].D = last[j]; last[j] = p; } } memset(last, -1, sizeof(last)); for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) { int p = i * m + j; if (s[p] != '.') { a[p].L = last[i]; last[i] = p; } } memset(last, -1, sizeof(last)); for (int j = m - 1; j >= 0; j--) for (int i = 0; i < n; i++) { int p = i * m + j; if (s[p] != '.') { a[p].R = last[i]; last[i] = p; } } ans = 0; for (int i = 0; i < n * m; i++) { if (s[i] == '.') continue; int k = go(i); if (k > ans) { ans = k; ansp = 1; } else if (k == ans) ansp++; } printf("%d %d\n", ans, ansp); return 0; }
#include <bits/stdc++.h> using namespace std; inline void wait(double seconds) { double endtime = clock() + (seconds * CLOCKS_PER_SEC); while (clock() < endtime) { ; } } inline int fastIn() { register char c = 0; register int a = 0; while (c < 33) c = getchar(); while (c > 33) { a = (a * 10) + (c - '0'); c = getchar(); } return a; } const int MX = 5005; int R, C; char buff[MX]; string grid[MX]; inline void read() { scanf("%d %d\n", &R, &C); for (int i = 0, j1 = R; i < j1; ++i) scanf(" %s", buff), grid[i] = (string)buff; } inline int encodeRC(int r, int c) { return (r * C + c); } inline int decodeR(int val) { return val / C; } inline int decodeC(int val) { return val % C; } struct info { int U[MX]; int D[MX]; int L[MX]; int R[MX]; inline void clear() { memset(U, 0, sizeof(U)), memset(D, 0, sizeof(D)), memset(L, 0, sizeof(L)), memset(R, 0, sizeof(R)); } }; info A, B; inline void mycopy() { memcpy(B.U, A.U, sizeof(A.U)); memcpy(B.D, A.D, sizeof(A.D)); memcpy(B.L, A.L, sizeof(A.L)); memcpy(B.R, A.R, sizeof(A.R)); } const int inf = 1023456789; inline void preprocess() { A.clear(); for (int i = 0; i < R; ++i) { int cnt = -1, ccnt = -1; for (int j = 0; j < C; ++j) { if (grid[i][j] != '.') A.L[encodeRC(i, j)] = cnt, cnt = encodeRC(i, j); if (grid[i][(C - 1) - j] != '.') A.R[encodeRC(i, (C - 1) - j)] = ccnt, ccnt = encodeRC(i, (C - 1) - j); } } for (int j = 0; j < C; ++j) { int cnt = -1, ccnt = -1; for (int i = 0; i < R; ++i) { if (grid[i][j] != '.') A.U[encodeRC(i, j)] = cnt, cnt = encodeRC(i, j); if (grid[(R - 1) - i][j] != '.') A.D[encodeRC((R - 1) - i, j)] = ccnt, ccnt = encodeRC((R - 1) - i, j); } } } inline bool move(char ch, int &tmp) { int li = B.L[tmp], ri = B.R[tmp], ui = B.U[tmp], di = B.D[tmp]; if (li != -1) B.R[li] = ri; if (ri != -1) B.L[ri] = li; if (ui != -1) B.D[ui] = di; if (di != -1) B.U[di] = ui; switch (ch) { case 'L': if (li == -1) return false; tmp = li; break; case 'R': if (ri == -1) return false; tmp = ri; break; case 'U': if (ui == -1) return false; tmp = ui; break; case 'D': if (di == -1) return false; tmp = di; break; case '.': return false; } return true; } inline int solve(int i, int j) { int tmp = encodeRC(i, j); mycopy(); int res = 0; while (true) { char ch = grid[decodeR(tmp)][decodeC(tmp)]; bool flag = move(ch, tmp); if (flag == false) break; ++res; } return res; } inline void proc() { preprocess(); int bst = 0; int cnt = 0; for (int i = 0, j1 = R; i < j1; ++i) { for (int j = 0, j1 = C; j < j1; ++j) { if (grid[i][j] != '.') { int local_bst = solve(i, j); if (local_bst >= bst) { if (local_bst == bst) { ++cnt; } else { cnt = 1; bst = local_bst; } } } } } cout << bst + 1 << ' ' << cnt << '\n'; } int main() { int kase = 1; for (int i = 0, j1 = kase; i < j1; ++i) { read(); proc(); } return 0; }
#include <bits/stdc++.h> using namespace std; struct coin { int dir; coin *nx[4]; coin() { memset(nx, 0, sizeof nx); } }; int n, m; vector<vector<coin> > cs; int di[] = {-1, 0, 1, 0}; int dj[] = {0, 1, 0, -1}; string dirs = "URDL"; void remove_coin(coin *c) { for (int k = 0; k < int(4); ++k) if (c->nx[(k + 2) % 4]) c->nx[(k + 2) % 4]->nx[k] = c->nx[k]; } void make_net() { for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) for (int k = 0; k < int(4); ++k) { int ni = i + di[k], nj = j + dj[k]; if (0 <= ni && ni < n && 0 <= nj && nj < m) cs[i][j].nx[k] = &cs[ni][nj]; else cs[i][j].nx[k] = 0; } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) if (cs[i][j].dir == int(string::npos)) remove_coin(&cs[i][j]); } int solve(int i, int j) { int count = 1; make_net(); for (coin *c = &cs[i][j]; c->nx[c->dir]; c = c->nx[c->dir]) { remove_coin(c); ++count; } return count; } int main() { cin >> n >> m; cs.resize(n, vector<coin>(m)); for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) { char tmp; cin >> tmp; cs[i][j].dir = dirs.find(tmp); } int opt = -1, optc = 0; for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) if (cs[i][j].dir != int(string::npos)) { int pts = solve(i, j); if (opt < pts) opt = pts, optc = 1; else if (opt == pts) ++optc; } cout << opt << ' ' << optc << endl; }
#include <bits/stdc++.h> using namespace std; string st = "UDLR."; string a[5006]; char s[5006]; int p1[5006][4], p2[5006][4], b[5006]; int num[5006][5006]; int n, m, k, ans, ans1, nn; int main() { nn = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%s", s); a[i] = s; } memset(p1, 0, sizeof(p1)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (a[i][j - 1] != '.') num[i][j] = ++nn; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (a[i][j - 1] != '.') { int mm = num[i][j]; b[mm] = st.find(a[i][j - 1]); for (int k = i - 1; k > 0; --k) if (a[k][j - 1] != '.') { p1[mm][0] = num[k][j]; break; } for (int k = i + 1; k <= n; ++k) if (a[k][j - 1] != '.') { p1[mm][1] = num[k][j]; break; } for (int k = j - 1; k > 0; --k) if (a[i][k - 1] != '.') { p1[mm][2] = num[i][k]; break; } for (int k = j + 1; k <= m; ++k) if (a[i][k - 1] != '.') { p1[mm][3] = num[i][k]; break; } } ans = ans1 = 0; for (int i = 1; i <= nn; ++i) { memcpy(p2, p1, sizeof(p1)); int j = i, tmp = 0; while (j) { p2[p2[j][0]][1] = p2[j][1]; p2[p2[j][1]][0] = p2[j][0]; p2[p2[j][2]][3] = p2[j][3]; p2[p2[j][3]][2] = p2[j][2]; ++tmp; j = p2[j][b[j]]; } if (tmp > ans) { ans = tmp; ans1 = 1; } else if (tmp == ans) ans1++; } printf("%d %d\n", ans, ans1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; map<int, int> ndx; int back[maxn]; int u[maxn], d[maxn], l[maxn], r[maxn]; char str[maxn][maxn]; int n, m; int bu[maxn], bd[maxn], bl[maxn], br[maxn]; inline void build() { int i, j, x; for (i = 0; i < n; i++) { x = -1; for (j = 0; j < m; j++) if (str[i][j] != '.') { if (x == -1) l[ndx[i * m + j]] = -1; else l[ndx[i * m + j]] = ndx[i * m + x]; x = j; } x = -1; for (j = m - 1; j >= 0; j--) if (str[i][j] != '.') { if (x == -1) r[ndx[i * m + j]] = -1; else r[ndx[i * m + j]] = ndx[i * m + x]; x = j; } } for (j = 0; j < m; j++) { x = -1; for (i = 0; i < n; i++) if (str[i][j] != '.') { if (x == -1) u[ndx[i * m + j]] = -1; else u[ndx[i * m + j]] = ndx[x * m + j]; x = i; } x = -1; for (i = n - 1; i >= 0; i--) if (str[i][j] != '.') { if (x == -1) d[ndx[i * m + j]] = -1; else d[ndx[i * m + j]] = ndx[x * m + j]; x = i; } } } inline void remove(int x) { if (u[x] != -1) d[u[x]] = d[x]; if (d[x] != -1) u[d[x]] = u[x]; if (l[x] != -1) r[l[x]] = r[x]; if (r[x] != -1) l[r[x]] = l[x]; } inline int travel(int x) { int step = 0; int i, j; while (x != -1) { step++; i = back[x] / m; j = back[x] % m; remove(x); switch (str[i][j]) { case 'U': x = u[x]; break; case 'D': x = d[x]; break; case 'L': x = l[x]; break; case 'R': x = r[x]; break; } } return step; } int main() { int i, j, num, ans, tot, t = 0; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%s", str[i]); for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (str[i][j] != '.') { ndx[i * m + j] = ++t; back[t] = i * m + j; } ans = tot = 0; build(); memcpy(bu, u, 4 * (t + 1)); memcpy(bd, d, 4 * (t + 1)); memcpy(bl, l, 4 * (t + 1)); memcpy(br, r, 4 * (t + 1)); for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (str[i][j] != '.') { memcpy(u, bu, 4 * (t + 1)); memcpy(d, bd, 4 * (t + 1)); memcpy(l, bl, 4 * (t + 1)); memcpy(r, br, 4 * (t + 1)); num = travel(ndx[i * m + j]); if (num > ans) { ans = num; tot = 1; } else if (num == ans) tot++; } printf("%d %d\n", ans, tot); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<string> S(N); for (string& s : S) cin >> s; int ans = 0; int cnt = 0; vector<vector<int>> Left(N, vector<int>(M, -1)); vector<vector<int>> Right(N, vector<int>(M, M)); vector<vector<int>> Down(N, vector<int>(M, N)); vector<vector<int>> Up(N, vector<int>(M, -1)); for (int r = 0; r < N; ++r) { for (int c = 0; c < M; ++c) { if (S[r][c] == '.') { continue; } for (int i = 0; i < N; ++i) { int prev = -1; for (int j = 0; j < M; ++j) { if (S[i][j] != '.') { Left[i][j] = prev; prev = j; } } } for (int i = 0; i < N; ++i) { int prev = M; for (int j = M - 1; j >= 0; --j) { if (S[i][j] != '.') { Right[i][j] = prev; prev = j; } } } for (int j = 0; j < M; ++j) { int prev = -1; for (int i = 0; i < N; ++i) { if (S[i][j] != '.') { Up[i][j] = prev; prev = i; } } } for (int j = 0; j < M; ++j) { int prev = N; for (int i = N - 1; i >= 0; --i) { if (S[i][j] != '.') { Down[i][j] = prev; prev = i; } } } int len = 0; int a = r, b = c; while (0 <= a && a < N && 0 <= b && b < M) { len++; int l = Left[a][b]; int r = Right[a][b]; int u = Up[a][b]; int d = Down[a][b]; if (l != -1) Right[a][l] = r; if (r != M) Left[a][r] = l; if (u != -1) Down[u][b] = d; if (d != N) Up[d][b] = u; if (S[a][b] == 'L') b = l; else if (S[a][b] == 'R') b = r; else if (S[a][b] == 'U') a = u; else if (S[a][b] == 'D') a = d; } if (len > ans) { ans = len; cnt = 1; } else if (len == ans) cnt++; } } cout << ans << ' ' << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int r, c, ans, cnt; int **L, **R, **U, **D; char mat[5005][5005]; void go(int x, int y) { int last; for (int i = 1; i <= r; i++) { last = 0; for (int j = 1; j <= c + 1; j++) if (mat[i][j] != '.') { R[i][last] = j; L[i][j] = last; last = j; } } for (int j = 1; j <= c; j++) { last = 0; for (int i = 1; i <= r + 1; i++) if (mat[i][j] != '.') { D[last][j] = i; U[i][j] = last; last = i; } } int i, j, s = 0, aux, a, b; i = x; j = y; while (1) { s++; if (mat[i][j] == 'U') { if (!U[i][j]) break; a = U[i][j]; b = D[i][j]; D[a][j] = b; U[b][j] = a; a = L[i][j]; b = R[i][j]; R[i][a] = b; L[i][b] = a; i = U[i][j]; } else if (mat[i][j] == 'R') { if (R[i][j] == c + 1) break; a = U[i][j]; b = D[i][j]; D[a][j] = b; U[b][j] = a; a = L[i][j]; b = R[i][j]; R[i][a] = b; L[i][b] = a; j = R[i][j]; } else if (mat[i][j] == 'D') { if (D[i][j] == r + 1) break; a = U[i][j]; b = D[i][j]; D[a][j] = b; U[b][j] = a; a = L[i][j]; b = R[i][j]; R[i][a] = b; L[i][b] = a; i = D[i][j]; } else { if (!L[i][j]) break; a = U[i][j]; b = D[i][j]; D[a][j] = b; U[b][j] = a; a = L[i][j]; b = R[i][j]; R[i][a] = b; L[i][b] = a; j = L[i][j]; } } if (s > ans) { ans = s; cnt = 1; } else if (s == ans) cnt++; return; } int main() { cin >> r >> c; L = (int **)(malloc((r + 2) * sizeof(int *))); U = (int **)(malloc((r + 2) * sizeof(int *))); R = (int **)(malloc((r + 2) * sizeof(int *))); D = (int **)(malloc((r + 2) * sizeof(int *))); for (int i = 0; i < r + 2; i++) { L[i] = (int *)(malloc((c + 2) * sizeof(int))); U[i] = (int *)(malloc((c + 2) * sizeof(int))); R[i] = (int *)(malloc((c + 2) * sizeof(int))); D[i] = (int *)(malloc((c + 2) * sizeof(int))); } for (int i = 1; i <= r; i++) cin >> mat[i] + 1; for (int i = 1; i <= r; i++) mat[i][c + 1] = 'U'; for (int j = 1; j <= c; j++) mat[r + 1][j] = 'U'; ans = cnt = 0; for (int i = 1; i <= r; i++) for (int j = 1; j <= c; j++) if (mat[i][j] != '.') go(i, j); cout << ans << " " << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 20010; int n, m; vector<char> grid(MAX, '.'); vector<int> U(MAX), D(MAX), R(MAX), L(MAX), tU(MAX), tD(MAX), tR(MAX), tL(MAX); int erase(int x) { tD[tU[x]] = tD[x]; tU[tD[x]] = tU[x]; tR[tL[x]] = tR[x]; tL[tR[x]] = tL[x]; return grid[x] == 'U' ? tU[x] : grid[x] == 'D' ? tD[x] : grid[x] == 'L' ? tL[x] : tR[x]; } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cin >> grid[i * (m + 2) + j]; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int x = i * (m + 2) + j - 1; L[x + 1] = (grid[x] != '.') ? x : L[x]; } for (int j = m; j; --j) { int x = i * (m + 2) + j + 1; R[x - 1] = (grid[x] != '.') ? x : R[x]; } } for (int j = 1; j <= m; ++j) { for (int i = 1; i <= n; ++i) { int x = (i - 1) * (m + 2) + j; U[x + m + 2] = (grid[x] != '.') ? x : U[x]; } for (int i = n; i; --i) { int x = (i + 1) * (m + 2) + j; D[x - m - 2] = (grid[x] != '.') ? x : D[x]; } } int ans = 0, cnt = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int x = i * (m + 2) + j, tans = 0; if (grid[x] == '.') continue; tU = U, tD = D, tR = R, tL = L; while (x) ++tans, x = erase(x); if (tans == ans) ++cnt; else if (tans > ans) ans = tans, cnt = 1; } } cout << ans << ' ' << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char **g; int **lx, **rx; int **ly, **ry; int n, m; template <class T> void getarr(T ***a) { *a = (T **)malloc((n + 10) * sizeof(T *)); for (int i = 0; i < n + 10; i++) { (*a)[i] = (T *)malloc((m + 10) * sizeof(T)); } } void del(int x, int y) { rx[x][lx[x][y]] = rx[x][y]; lx[x][rx[x][y]] = lx[x][y]; ry[ly[x][y]][y] = ry[x][y]; ly[ry[x][y]][y] = ly[x][y]; } void init() { for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { lx[i][j] = j - 1; rx[i][j] = j + 1; } lx[i][0] = m; rx[i][m] = 0; } for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { ly[j][i] = j - 1; ry[j][i] = j + 1; } ly[0][i] = n; ry[n][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] == '.') { del(i, j); } } } } int to[300]; int solve(int x, int y) { int cc = 0; while (x != 0 && y != 0) { int d = to[g[x][y]]; cc++; del(x, y); if (d == 0) { x = ly[x][y]; } else if (d == 1) { y = rx[x][y]; } else if (d == 2) { x = ry[x][y]; } else { y = lx[x][y]; } } return cc; } int main() { to['U'] = 0; to['R'] = 1; to['D'] = 2; to['L'] = 3; scanf("%d%d", &n, &m); getarr(&g); getarr(&lx); getarr(&rx); getarr(&ly); getarr(&ry); for (int i = 1; i <= n; i++) { scanf("%s", g[i] + 1); } int ans1 = -1, ans2 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (g[i][j] != '.') { init(); int ret = solve(i, j); if (ans1 < ret) { ans1 = ret; ans2 = 1; } else if (ans1 == ret) { ans2++; } } } } printf("%d %d\n", ans1, ans2); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_HW = 5000; const char dcs[] = "RULD"; const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1}; int dcmap[128]; char s[MAX_HW + 4], fs0[MAX_HW], fs1[MAX_HW]; short nbs0[MAX_HW][4], nbs1[MAX_HW][4]; inline bool inside(int x, int y, int w, int h) { return x >= 0 && x < w && y >= 0 && y < h; } inline void remcell(short (*nbs)[4], int p) { for (int di0 = 0; di0 < 4; di0++) { int di1 = (di0 ^ 2), p0 = nbs[p][di0]; if (p0 >= 0) nbs[p0][di1] = nbs[p][di1]; } } int main() { for (int i = 0; i < 4; i++) dcmap[dcs[i]] = i; dcmap['.'] = -1; int h, w; scanf("%d%d", &h, &w); int hw = h * w; int dps[4] = {1, -w, -1, w}; for (int y = 0, p = 0; y < h; y++) { scanf("%s", s); for (int x = 0; x < w; x++, p++) { fs0[p] = dcmap[s[x]]; for (int di = 0; di < 4; di++) nbs0[p][di] = inside(x + dxs[di], y + dys[di], w, h) ? p + dps[di] : -1; } } for (int p = 0; p < hw; p++) if (fs0[p] < 0) remcell(nbs0, p); int maxr = 0, maxc = 0; for (int st = 0; st < hw; st++) if (fs0[st] >= 0) { int p = st, r = 0; memcpy(fs1, fs0, sizeof(fs0)); memcpy(nbs1, nbs0, sizeof(nbs0)); while (p >= 0) { int ndi = fs1[p]; fs1[p] = -1; remcell(nbs1, p); r++; p = nbs1[p][ndi]; } if (maxr < r) maxr = r, maxc = 1; else if (maxr == r) maxc++; } printf("%d %d\n", maxr, maxc); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char s[5001][5001]; int p[5001][5001]; int a[5001][4], A[5001][4]; int d[5001]; int dis, num; int main() { int nn = 0; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", s[i]); memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] != '.') { p[i][j] = ++nn; if (s[i][j] == 'U') d[nn] = 0; if (s[i][j] == 'D') d[nn] = 1; if (s[i][j] == 'L') d[nn] = 2; if (s[i][j] == 'R') d[nn] = 3; } else p[i][j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (p[i][j]) { for (int k = i - 1; k >= 0; k--) { if (p[k][j]) { a[p[i][j]][0] = p[k][j]; break; } } for (int k = i + 1; k < n; k++) { if (p[k][j]) { a[p[i][j]][1] = p[k][j]; break; } } for (int k = j - 1; k >= 0; k--) { if (p[i][k]) { a[p[i][j]][2] = p[i][k]; break; } } for (int k = j + 1; k < m; k++) { if (p[i][k]) { a[p[i][j]][3] = p[i][k]; break; } } } } } dis = 0; num = 0; for (int i = 1; i <= nn; i++) { memcpy(A, a, sizeof(a)); int j = i; int length = 0; while (j) { length++; A[A[j][0]][1] = A[j][1]; A[A[j][1]][0] = A[j][0]; A[A[j][2]][3] = A[j][3]; A[A[j][3]][2] = A[j][2]; j = A[j][d[j]]; } if (dis < length) { dis = length; num = 1; } else if (dis == length) num++; } printf("%d %d\n", dis, num); return 0; }
#include <bits/stdc++.h> using namespace std; int const DX[] = {0, -1, 0, 1}; int const DY[] = {-1, 0, 1, 0}; string const DIRS = "ULDR"; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); int n, m; cin >> n >> m; vector<string> table(n); for (int i = (0); i < ((n)); ++i) cin >> table[i]; vector<vector<pair<int, int> > > levels[4]; for (int dir = (0); dir < ((4)); ++dir) levels[dir].resize(n, vector<pair<int, int> >(m, make_pair(-1, -1))); for (int j = (0); j < ((m)); ++j) { for (int i = (0); i < ((n - 1)); ++i) { if (table[i][j] == '.') levels[0][i + 1][j] = levels[0][i][j]; else levels[0][i + 1][j] = make_pair(i, j); } for (int i = n - 1; i > 0; --i) { if (table[i][j] == '.') levels[2][i - 1][j] = levels[2][i][j]; else levels[2][i - 1][j] = make_pair(i, j); } } for (int i = (0); i < ((n)); ++i) { for (int j = (0); j < ((m - 1)); ++j) { if (table[i][j] == '.') levels[1][i][j + 1] = levels[1][i][j]; else levels[1][i][j + 1] = make_pair(i, j); } for (int j = m - 1; j > 0; --j) { if (table[i][j] == '.') levels[3][i][j - 1] = levels[3][i][j]; else levels[3][i][j - 1] = make_pair(i, j); } } vector<vector<pair<int, int> > > tempLevels[4]; int best = 0; int count = 0; for (int i = (0); i < ((n)); ++i) for (int j = (0); j < ((m)); ++j) if (table[i][j] != '.') { for (int i = (0); i < ((4)); ++i) tempLevels[i] = levels[i]; int current = 0; pair<int, int> pos = make_pair(i, j); while (pos != make_pair(-1, -1)) { int currentDir = DIRS.find(table[pos.first][pos.second]); const pair<int, int>& left = levels[1][pos.first][pos.second]; const pair<int, int>& right = levels[3][pos.first][pos.second]; const pair<int, int>& up = levels[0][pos.first][pos.second]; const pair<int, int>& down = levels[2][pos.first][pos.second]; if (left.first > -1) levels[3][left.first][left.second] = right; if (right.first > -1) levels[1][right.first][right.second] = left; if (up.first > -1) levels[2][up.first][up.second] = down; if (down.first > -1) levels[0][down.first][down.second] = up; pos = levels[currentDir][pos.first][pos.second]; ++current; } if (best < current) { best = current; count = 1; } else if (best == current) { ++count; } for (int i = (0); i < ((4)); ++i) levels[i] = tempLevels[i]; } cout << best << " " << count << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 5e3 + 5; int n, m; char a[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; int ans = 0, t = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (a[i][j] == '.') continue; int lstR[m + 5], lstC[n + 5]; int nxt[n + 5][m + 5][4]; memset(lstR, -1, sizeof lstR); memset(lstC, -1, sizeof lstC); memset(nxt, -1, sizeof nxt); for (int p1 = 0; p1 < n; p1++) for (int p2 = 0; p2 < m; p2++) { if (a[p1][p2] == '.') continue; if (~lstR[p2]) { nxt[p1][p2][0] = lstR[p2]; nxt[lstR[p2]][p2][1] = p1; } if (~lstC[p1]) { nxt[p1][p2][2] = lstC[p1]; nxt[p1][lstC[p1]][3] = p2; } lstR[p2] = p1, lstC[p1] = p2; } int curi = i, curj = j, cnt = 0; while (1) { cnt++; if (~nxt[curi][curj][1]) nxt[nxt[curi][curj][1]][curj][0] = nxt[curi][curj][0]; if (~nxt[curi][curj][0]) nxt[nxt[curi][curj][0]][curj][1] = nxt[curi][curj][1]; if (~nxt[curi][curj][2]) nxt[curi][nxt[curi][curj][2]][3] = nxt[curi][curj][3]; if (~nxt[curi][curj][3]) nxt[curi][nxt[curi][curj][3]][2] = nxt[curi][curj][2]; if (a[curi][curj] == 'U') { if (nxt[curi][curj][0] == -1) break; curi = nxt[curi][curj][0]; } else if (a[curi][curj] == 'D') { if (nxt[curi][curj][1] == -1) break; curi = nxt[curi][curj][1]; } else if (a[curi][curj] == 'L') { if (nxt[curi][curj][2] == -1) break; curj = nxt[curi][curj][2]; } else { if (nxt[curi][curj][3] == -1) break; curj = nxt[curi][curj][3]; } } if (ans < cnt) ans = cnt, t = 1; else if (ans == cnt) t++; } cout << ans << " " << t; return 0; }
#include <bits/stdc++.h> using namespace std; char mm[5005]; struct node { int i, j; node *u, *d, *l, *r; }; node mm2[5005]; void del(int i, int j, int m) { node *sp; if (mm2[i * m + j].l != NULL) { sp = mm2[i * m + j].l; sp->r = mm2[i * m + j].r; } if (mm2[i * m + j].r != NULL) { sp = mm2[i * m + j].r; sp->l = mm2[i * m + j].l; } if (mm2[i * m + j].u != NULL) { sp = mm2[i * m + j].u; sp->d = mm2[i * m + j].d; } if (mm2[i * m + j].d != NULL) { sp = mm2[i * m + j].d; sp->u = mm2[i * m + j].u; } } void makegraph(int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { mm2[i * m + j].i = i; mm2[i * m + j].j = j; if (j > 0) mm2[i * m + j].l = &mm2[i * m + j - 1]; else mm2[i * m + j].l = NULL; if (j < m - 1) mm2[i * m + j].r = &mm2[i * m + j + 1]; else mm2[i * m + j].r = NULL; if (i > 0) mm2[i * m + j].u = &mm2[(i - 1) * m + j]; else mm2[i * m + j].u = NULL; if (i < n - 1) mm2[i * m + j].d = &mm2[(i + 1) * m + j]; else mm2[i * m + j].d = NULL; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mm[i * m + j] == '.') del(i, j, m); } } } int cnt, ans, xx; void work(int i, int j, int m) { del(i, j, m); if (mm[i * m + j] == 'U') { if (mm2[i * m + j].u == NULL) return; else { cnt++; work(mm2[i * m + j].u->i, mm2[i * m + j].u->j, m); } } if (mm[i * m + j] == 'D') { if (mm2[i * m + j].d == NULL) return; else { cnt++; work(mm2[i * m + j].d->i, mm2[i * m + j].d->j, m); } } if (mm[i * m + j] == 'L') { if (mm2[i * m + j].l == NULL) return; else { cnt++; work(mm2[i * m + j].l->i, mm2[i * m + j].l->j, m); } } if (mm[i * m + j] == 'R') { if (mm2[i * m + j].r == NULL) return; else { cnt++; work(mm2[i * m + j].r->i, mm2[i * m + j].r->j, m); } } } int main() { int n, m; while (scanf("%d%d", &n, &m) == 2) { for (int i = 0; i < n; i++) scanf("%s", &mm[i * m]); ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mm[i * m + j] == '.') continue; makegraph(n, m); cnt = 0; work(i, j, m); if (cnt > ans) { ans = cnt; xx = 0; } if (cnt == ans) xx++; } } printf("%d %d\n", ans + 1, xx); } }
#include <bits/stdc++.h> using namespace std; const signed long long Infinity = 1000000001; const long double Epsilon = 1e-9; template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& V) { os << "["; for (__typeof(V.begin()) i = V.begin(); i != V.end(); ++i) os << *i << (i - V.begin() + 1 == ((int)V.size()) ? "" : ","); os << "]\n"; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& S) { os << "("; for (__typeof(S.begin()) i = S.begin(); i != S.end(); ++i) os << *i << (*i == *S.rbegin() ? "" : ","); os << ")\n"; return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& M) { os << "{"; for (__typeof(M.begin()) i = M.begin(); i != M.end(); ++i) os << *i << (i->first == M.rbegin()->first ? "" : ","); os << "}\n"; return os; } const int MAXN = 5010; int M, N; char buff[MAXN]; string A[MAXN]; vector<pair<int, int> > L[MAXN]; vector<pair<int, int> > R[MAXN]; vector<pair<int, int> > U[MAXN]; vector<pair<int, int> > D[MAXN]; pair<int, int> FAIL(-1, -1); void read_data() { scanf("%d %d", &M, &N); for (int(i) = (1); (i) <= (M); (i)++) { scanf("%s", buff); A[i] = " " + string(buff); } for (int(i) = (1); (i) <= (M); (i)++) { L[i].resize(N + 2, FAIL); R[i].resize(N + 2, FAIL); U[i].resize(N + 2, FAIL); D[i].resize(N + 2, FAIL); } } void off(int x, int y) { if (L[x][y] != FAIL) R[L[x][y].first][L[x][y].second] = R[x][y]; if (R[x][y] != FAIL) L[R[x][y].first][R[x][y].second] = L[x][y]; if (D[x][y] != FAIL) U[D[x][y].first][D[x][y].second] = U[x][y]; if (U[x][y] != FAIL) D[U[x][y].first][U[x][y].second] = D[x][y]; } void on(int x, int y) { if (L[x][y] != FAIL) R[L[x][y].first][L[x][y].second] = make_pair(x, y); if (R[x][y] != FAIL) L[R[x][y].first][R[x][y].second] = make_pair(x, y); if (D[x][y] != FAIL) U[D[x][y].first][D[x][y].second] = make_pair(x, y); if (U[x][y] != FAIL) D[U[x][y].first][U[x][y].second] = make_pair(x, y); } int check(int x, int y) { if (x < 1 or x > M or y < 1 or y > N) return 0; if (A[x][y] == '.') return 0; off(x, y); int result; if (A[x][y] == 'D') result = 1 + check(D[x][y].first, y); if (A[x][y] == 'U') result = 1 + check(U[x][y].first, y); if (A[x][y] == 'L') result = 1 + check(x, L[x][y].second); if (A[x][y] == 'R') result = 1 + check(x, R[x][y].second); on(x, y); return result; } void solve() { for (int(i) = (1); (i) <= (M); (i)++) { int last = -1; for (int(j) = (1); (j) <= (N); (j)++) if (A[i][j] != '.') { if (last != -1) L[i][j] = make_pair(i, last); last = j; } last = -1; for (int(j) = (N); (j) >= (1); (j)--) if (A[i][j] != '.') { if (last != -1) R[i][j] = make_pair(i, last); last = j; } } for (int(i) = (1); (i) <= (N); (i)++) { int last = -1; for (int(j) = (1); (j) <= (M); (j)++) if (A[j][i] != '.') { if (last != -1) U[j][i] = make_pair(last, i); last = j; } last = -1; for (int(j) = (M); (j) >= (1); (j)--) if (A[j][i] != '.') { if (last != -1) D[j][i] = make_pair(last, i); last = j; } } map<int, int> Re; for (int(i) = (1); (i) <= (M); (i)++) for (int(j) = (1); (j) <= (N); (j)++) if (A[i][j] != '.') Re[check(i, j)]++; printf("%d %d\n", Re.rbegin()->first, Re.rbegin()->second); } int main() { read_data(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5005; vector<vector<vector<int>>> nxt(4); int n, m; char grid[5005][5005]; int cnt = 0; vector<pair<int, int>> rollback; vector<vector<vector<int>>> base; int solve(int sx, int sy) { int ret = 0; while (sx >= 0 && sx < n && sy >= 0 && sy < m) { int L = nxt[0][sx][sy]; int R = nxt[1][sx][sy]; if (L >= 0) nxt[1][sx][L] = R, rollback.push_back(pair<int, int>(sx, L)); if (R < m) nxt[0][sx][R] = L, rollback.push_back(pair<int, int>(sx, R)); int U = nxt[2][sx][sy]; int D = nxt[3][sx][sy]; if (D < n) nxt[2][D][sy] = U, rollback.push_back(pair<int, int>(D, sy)); if (U >= 0) nxt[3][U][sy] = D, rollback.push_back(pair<int, int>(U, sy)); ret++; if (grid[sx][sy] == 'D') { sx = nxt[3][sx][sy]; } else if (grid[sx][sy] == 'R') { sy = nxt[1][sx][sy]; } else if (grid[sx][sy] == 'U') { sx = nxt[2][sx][sy]; } else { sy = nxt[0][sx][sy]; } } for (int i = 0; i < rollback.size(); i++) { int x, y; tie(x, y) = rollback[i]; nxt[0][x][y] = base[0][x][y]; nxt[1][x][y] = base[1][x][y]; nxt[2][x][y] = base[2][x][y]; nxt[3][x][y] = base[3][x][y]; } rollback.clear(); return ret; } int main() { srand(time(0)); scanf("%d%d", &n, &m); nxt[0] = vector<vector<int>>(n, vector<int>(m)); nxt[1] = vector<vector<int>>(n, vector<int>(m)); nxt[2] = vector<vector<int>>(n, vector<int>(m)); nxt[3] = vector<vector<int>>(n, vector<int>(m)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf(" %c", &grid[i][j]); for (int i = 0; i < n; i++) { int b4 = -1; for (int j = 0; j < m; j++) { nxt[0][i][j] = b4; if (grid[i][j] != '.') b4 = j; } b4 = m; for (int j = m - 1; j >= 0; --j) { nxt[1][i][j] = b4; if (grid[i][j] != '.') b4 = j; } } for (int j = 0; j < m; j++) { int b4 = -1; for (int i = 0; i < n; i++) { nxt[2][i][j] = b4; if (grid[i][j] != '.') b4 = i; } b4 = n; for (int i = n - 1; i >= 0; --i) { nxt[3][i][j] = b4; if (grid[i][j] != '.') b4 = i; } } int ans = 0, cnt = 1; base = nxt; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (grid[i][j] != '.') { int x = solve(i, j); if (x > ans) cnt = 0, ans = x; if (x == ans) cnt++; } } cout << ans << ' ' << cnt << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char** mat; short ***sparse, ***rem; void build() { sparse = new short**[n + 1]; rem = new short**[n + 1]; for (int i = 0; i <= n; ++i) { sparse[i] = new short*[m + 1]; rem[i] = new short*[m + 1]; } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { sparse[i][j] = new short[4]; memset(sparse[i][j], 0, sizeof(short) * 4); rem[i][j] = new short[4]; memset(rem[i][j], 0, sizeof(short) * 4); } } for (int i = 1; i <= n; ++i) { int prev = 0; for (int j = 1; j <= m; ++j) { if (mat[i][j]) { sparse[i][j][0] = prev; prev = j; } } prev = 0; for (int j = m; j > 0; --j) { if (mat[i][j]) { sparse[i][j][1] = prev; prev = j; } } } for (int j = 1; j <= m; ++j) { int prev = 0; for (int i = 1; i <= n; ++i) { if (mat[i][j]) { sparse[i][j][2] = prev; prev = i; } } prev = 0; for (int i = n; i > 0; --i) { if (mat[i][j]) { sparse[i][j][3] = prev; prev = i; } } } } void remove(int p, int q) { int prox, prev; prox = sparse[p][q][0]; prev = sparse[p][q][1]; if (prev) sparse[p][prev][0] = prox; if (prox) sparse[p][prox][1] = prev; rem[p][q][0] = prox; prox = sparse[p][q][1]; prev = sparse[p][q][0]; if (prev) sparse[p][prev][1] = prox; if (prox) sparse[p][prox][0] = prev; rem[p][q][1] = prox; prox = sparse[p][q][2]; prev = sparse[p][q][3]; if (prev) sparse[prev][q][2] = prox; if (prox) sparse[prox][q][3] = prev; rem[p][q][2] = prox; prox = sparse[p][q][3]; prev = sparse[p][q][2]; if (prev) sparse[prev][q][3] = prox; if (prox) sparse[prox][q][2] = prev; rem[p][q][3] = prox; } void insert(int p, int q) { int x; x = rem[p][q][0]; if (x) sparse[p][x][1] = q; x = rem[p][q][1]; if (x) sparse[p][x][0] = q; x = rem[p][q][2]; if (x) sparse[x][q][3] = p; x = rem[p][q][3]; if (x) sparse[x][q][2] = p; for (int i = 0; i < 4; ++i) rem[p][q][i] = 0; } int solve(int p, int q) { int tot = 0; stack<pair<int, int> > s; while (p and q) { int prox, prev; if (mat[p][q] == 'L') { prox = sparse[p][q][0]; if (p and q) remove(p, q); if (p and q) s.push(make_pair(p, q)); q = prox; } else if (mat[p][q] == 'R') { prox = sparse[p][q][1]; if (p and q) remove(p, q); if (p and q) s.push(make_pair(p, q)); q = prox; } else if (mat[p][q] == 'U') { prox = sparse[p][q][2]; if (p and q) remove(p, q); if (p and q) s.push(make_pair(p, q)); p = prox; } else { prox = sparse[p][q][3]; if (p and q) remove(p, q); if (p and q) s.push(make_pair(p, q)); p = prox; } tot++; } while (s.size()) { pair<int, int> p = s.top(); s.pop(); insert(p.first, p.second); } return tot; } int main(int argc, char* argv[]) { cin >> n >> m; mat = new char*[n + 1]; for (int i = 0; i <= n; ++i) mat[i] = new char[m + 1]; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> mat[i][j]; if (mat[i][j] == '.') mat[i][j] = 0; } } build(); int ans = 0, freq = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int x = mat[i][j] ? solve(i, j) : 0; if (x > ans) { ans = x; freq = 1; } else if (x == ans) freq++; } } cout << ans << " " << freq << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[4] = {0, 1, -1, 0}, dy[4] = {-1, 0, 0, 1}; const char D[4] = {'L', 'D', 'U', 'R'}; char p[5010][5010]; int x[5010], y[5010], mk[5010][5010], d[5010], w[5010][4], u[5010][4], a, b, i, j, k, t, s, tx, ty, n, m, cnt; int main() { scanf("%d%d", &n, &m); getchar(); cnt = 0; for (i = 0; i < (n); ++i) { gets(p[i]); for (j = 0; j < (m); ++j) if (p[i][j] != '.') { x[cnt] = i; y[cnt] = j; for (k = 0; k < (4); ++k) if (p[i][j] == D[k]) d[cnt] = k; mk[i][j] = cnt++; } } for (i = 0; i < (cnt); ++i) for (k = 0; k < (4); ++k) { tx = x[i] + dx[k]; ty = y[i] + dy[k]; w[i][k] = -1; while (tx >= 0 && tx < n && ty >= 0 && ty < m) { if (p[tx][ty] != '.') { w[i][k] = mk[tx][ty]; break; } tx += dx[k]; ty += dy[k]; } } a = -1; for (i = 0; i < (cnt); ++i) { t = i; s = 0; for (j = 0; j < (cnt); ++j) for (k = 0; k < (4); ++k) u[j][k] = w[j][k]; do { ++s; for (k = 0; k < (4); ++k) if (u[t][k] >= 0) u[u[t][k]][3 - k] = u[t][3 - k]; t = u[t][d[t]]; } while (t != -1); if (s > a) { a = s; b = 1; } else b += a == s ? 1 : 0; } printf("%d %d\n", a, b); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-8; const long double PI = 3.1415926535897932384626433832795; const int MAXN = 10100; int n, m; int u[MAXN], d[MAXN], l[MAXN], r[MAXN]; int U[MAXN], D[MAXN], L[MAXN], R[MAXN]; char c[MAXN]; int get(int x, int y) { if (0 <= x && x < n && 0 <= y && y < m) return x * m + y; return INF; } int leader(int *l, int v) { if (v > INF / 2) return INF; if (l[v] != v) l[v] = leader(l, l[v]); return l[v]; } int main() { cin >> n >> m; for (int i = 0; i < (int)(n); i++) for (int j = 0; j < (int)(m); j++) { int v = get(i, j); scanf(" %c", &c[v]); } int ans = 0, cnt = 0; for (int i = 0; i < (int)(n * m); i++) { for (int j = 0; j < (int)(n * m); j++) { int x = j / m, y = j % m; u[j] = d[j] = l[j] = r[j] = j; if (c[j] == '.') { d[j] = get(x + 1, y); u[j] = get(x - 1, y); l[j] = get(x, y - 1); r[j] = get(x, y + 1); } } int v = i, cur = 0; while (true) { if (v > INF / 2 || c[v] == '.') break; cur++; int x = v / m, y = v % m, nv = INF; d[v] = get(x + 1, y); u[v] = get(x - 1, y); l[v] = get(x, y - 1); r[v] = get(x, y + 1); if (c[v] == 'D') nv = leader(d, v); if (c[v] == 'U') nv = leader(u, v); if (c[v] == 'L') nv = leader(l, v); if (c[v] == 'R') nv = leader(r, v); v = nv; } if (cur > ans) { ans = cur; cnt = 0; } if (cur == ans) cnt++; } cout << ans << " " << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; const double PI = acos(-1.0); template <class T> T gcd(T a, T b) { for (T c; b; c = a, a = b, b = c % b) ; return a; } template <class T> void out(const vector<T> &a) { for (int i = 0; i < ((int)(a).size()); ++i) cout << a[i] << " "; cout << endl; } int countbit(int n) { return n == 0 ? 0 : 1 + countbit(n & (n - 1)); } const int d8[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; const int d4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; inline int dcmp(double x) { return (x > EPS) - (x < -EPS); } inline double cross(const complex<double> &a, const complex<double> &b) { return (conj(a) * b).imag(); } inline double dot(const complex<double> &a, const complex<double> &b) { return (conj(a) * b).real(); } const int N = 5000; char s[N][N + 10]; int idx[N][N]; pair<int, int> A[N]; int L[N], R[N], U[N], D[N]; int _L[N], _R[N], _U[N], _D[N]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%s", s[i]); int cnt = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (s[i][j] == '.') continue; idx[i][j] = cnt; A[cnt++] = make_pair(i, j); } for (int v = 0; v < cnt; ++v) { int i = A[v].first; int j = A[v].second; int u = idx[i][j]; L[u] = -1; for (int k = j - 1; k >= 0; --k) if (s[i][k] != '.') { L[u] = idx[i][k]; break; } R[u] = -1; for (int k = j + 1; k < m; ++k) if (s[i][k] != '.') { R[u] = idx[i][k]; break; } U[u] = -1; for (int k = i - 1; k >= 0; --k) if (s[k][j] != '.') { U[u] = idx[k][j]; break; } D[u] = -1; for (int k = i + 1; k < n; ++k) if (s[k][j] != '.') { D[u] = idx[k][j]; break; } } copy(L, L + cnt, _L); copy(R, R + cnt, _R); copy(U, U + cnt, _U); copy(D, D + cnt, _D); int best = 0, best_cnt = 0; for (int v = 0; v < cnt; ++v) { int i = A[v].first; int j = A[v].second; copy(_L, _L + cnt, L); copy(_R, _R + cnt, R); copy(_U, _U + cnt, U); copy(_D, _D + cnt, D); int sum = 0; while (true) { int u; if (s[i][j] == 'D') u = D[idx[i][j]]; else if (s[i][j] == 'U') u = U[idx[i][j]]; else if (s[i][j] == 'L') u = L[idx[i][j]]; else u = R[idx[i][j]]; ++sum; if (D[idx[i][j]] >= 0) U[D[idx[i][j]]] = U[idx[i][j]]; if (U[idx[i][j]] >= 0) D[U[idx[i][j]]] = D[idx[i][j]]; if (L[idx[i][j]] >= 0) R[L[idx[i][j]]] = R[idx[i][j]]; if (R[idx[i][j]] >= 0) L[R[idx[i][j]]] = L[idx[i][j]]; if (u < 0) break; i = A[u].first; j = A[u].second; } if (best < sum) { best = sum; best_cnt = 1; } else if (best == sum) best_cnt++; } printf("%d %d\n", best, best_cnt); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; const int inf = 5100; typedef struct a { int pos, num; } per; short n, m, si[maxn][maxn], ans, ctt; short tl, tr, tu, td, l[maxn][maxn], r[maxn][maxn], up[maxn][maxn], down[maxn][maxn]; void change(int x, int y) { tl = l[x][y], tr = r[x][y], tu = up[x][y], td = down[x][y]; if (tl != inf) r[x][tl] = tr; if (tr != inf) l[x][tr] = tl; if (td != inf) up[td][y] = tu; if (tu != inf) down[tu][y] = td; } void go_back(int x, int y, int ttl, int ttr, int ttu, int ttd) { if (ttl != inf) r[x][ttl] = y; if (ttr != inf) l[x][ttr] = y; if (ttd != inf) up[ttd][y] = x; if (ttu != inf) down[ttu][y] = x; } void print() { cout << "TEST\n"; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cout << l[i][j] << ' '; } cout << "\n"; } } void dfs(int x, int y, int cnt) { if (x == inf || y == inf) { if (cnt == ans) ctt++; else if (cnt > ans) { ans = cnt; ctt = 1; } return; } change(x, y); int ttl = tl, ttr = tr, ttu = tu, ttd = td; if (si[x][y] == 0) dfs(up[x][y], y, cnt + 1); else if (si[x][y] == 1) dfs(down[x][y], y, cnt + 1); else if (si[x][y] == 2) dfs(x, l[x][y], cnt + 1); else dfs(x, r[x][y], cnt + 1); go_back(x, y, ttl, ttr, ttu, ttd); return; } int main() { cin >> n >> m; char tc; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> tc; if (tc == '.') si[i][j] = -1; else if (tc == 'U') si[i][j] = 0; else if (tc == 'D') si[i][j] = 1; else if (tc == 'L') si[i][j] = 2; else si[i][j] = 3; } } for (int i = 1; i <= n; ++i) { int last = inf; for (int j = 1; j <= m; ++j) { if (si[i][j] != -1) { l[i][j] = last; last = j; } } last = inf; for (int j = m; j >= 1; --j) { if (si[i][j] != -1) { r[i][j] = last; last = j; } } } for (int j = 1; j <= m; ++j) { int last = inf; for (int i = 1; i <= n; ++i) { if (si[i][j] != -1) { up[i][j] = last; last = i; } } last = inf; for (int i = n; i >= 1; --i) { if (si[i][j] != -1) { down[i][j] = last; last = i; } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (si[i][j] != -1) dfs(i, j, 0); } } cout << ans << ' ' << ctt; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const double PI = 3.141592653589793; int licz, maxx, i, j, akt, n, m, L[5005], R[5005], U[5005], D[5005], sum, w; char dir[5005]; char t[5005]; vector<int> wek; void hide(int a) { L[R[a]] = L[a]; R[L[a]] = R[a]; U[D[a]] = U[a]; D[U[a]] = D[a]; } void show(int a) { L[R[a]] = a; R[L[a]] = a; U[D[a]] = a; D[U[a]] = a; } void wypisz() { for (int i = 1; i <= n * m; i++) printf("%d %d %d %d %d\n", i, L[i], U[i], R[i], D[i]); } int main() { scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { scanf("%s", &t); for (j = 0; j < m; j++) { w = i * m + j + 1; dir[w] = t[j]; if (j == 0) L[w] = 0; else L[w] = w - 1; if (j == m - 1) R[w] = 0; else R[w] = w + 1; if (i == 0) U[w] = 0; else U[w] = w - m; if (i == n - 1) D[w] = 0; else D[w] = w + m; } } for (i = 1; i <= m * n; i++) if (dir[i] == '.') hide(i); for (i = 1; i <= m * n; i++) if (dir[i] != '.') { akt = i; licz = 0; while (akt != 0) { licz++; wek.push_back(akt); hide(akt); if (dir[akt] == 'L') akt = L[akt]; else if (dir[akt] == 'R') akt = R[akt]; else if (dir[akt] == 'U') akt = U[akt]; else if (dir[akt] == 'D') akt = D[akt]; } while (wek.size() > 0) { show(wek.back()); wek.pop_back(); } if (licz == maxx) sum++; else if (licz > maxx) { sum = 1; maxx = licz; } } printf("%d %d\n", maxx, sum); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5005; int n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; char a[n + 1][m + 1]; pair<int, int> L[n + 1][m + 1], R[n + 1][m + 1], U[n + 1][m + 1], D[n + 1][m + 1], LL[n + 1][m + 1], RR[n + 1][m + 1], UU[n + 1][m + 1], DD[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } for (int i = 1; i <= n; i++) { pair<int, int> cur = make_pair(-1, -1); for (int j = 1; j <= m; j++) { if (a[i][j] != '.') { L[i][j] = cur; cur = make_pair(i, j); } } cur = make_pair(-1, -1); for (int j = m; j >= 1; j--) { if (a[i][j] != '.') { R[i][j] = cur; cur = make_pair(i, j); } } } for (int j = 1; j <= m; j++) { pair<int, int> cur = make_pair(-1, -1); for (int i = 1; i <= n; i++) { if (a[i][j] != '.') { U[i][j] = cur; cur = make_pair(i, j); } } cur = make_pair(-1, -1); for (int i = n; i >= 1; i--) { if (a[i][j] != '.') { D[i][j] = cur; cur = make_pair(i, j); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { UU[i][j] = U[i][j]; DD[i][j] = D[i][j]; LL[i][j] = L[i][j]; RR[i][j] = R[i][j]; } } int mx = 0, cur = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] != '.') { for (int ii = 1; ii <= n; ii++) { for (int jj = 1; jj <= m; jj++) { L[ii][jj] = LL[ii][jj]; R[ii][jj] = RR[ii][jj]; U[ii][jj] = UU[ii][jj]; D[ii][jj] = DD[ii][jj]; } } queue<pair<int, int> > q; q.push(make_pair(i, j)); int sum = 0; while (!q.empty()) { auto it = q.front(); q.pop(); sum++; if (R[it.first][it.second] != make_pair(-1, -1)) { L[R[it.first][it.second].first][R[it.first][it.second].second] = L[it.first][it.second]; } if (L[it.first][it.second] != make_pair(-1, -1)) { R[L[it.first][it.second].first][L[it.first][it.second].second] = R[it.first][it.second]; } if (U[it.first][it.second] != make_pair(-1, -1)) { D[U[it.first][it.second].first][U[it.first][it.second].second] = D[it.first][it.second]; } if (D[it.first][it.second] != make_pair(-1, -1)) { U[D[it.first][it.second].first][D[it.first][it.second].second] = U[it.first][it.second]; } if (a[it.first][it.second] == 'L') { if (L[it.first][it.second] != make_pair(-1, -1)) { q.push(L[it.first][it.second]); } } else if (a[it.first][it.second] == 'R') { if (R[it.first][it.second] != make_pair(-1, -1)) { q.push(R[it.first][it.second]); } } else if (a[it.first][it.second] == 'U') { if (U[it.first][it.second] != make_pair(-1, -1)) { q.push(U[it.first][it.second]); } } else { if (D[it.first][it.second] != make_pair(-1, -1)) { q.push(D[it.first][it.second]); } } } if (sum > mx) { mx = sum; cur = 1; } else if (sum == mx) { cur++; } } } } cout << mx << " " << cur; }
#include <bits/stdc++.h> long lal[5005] = {0}, lar[5005] = {0}, lau[5005] = {0}, lad[5005] = {0}, lastl[5005] = {0}, lastr[5005] = {0}, lastu[5005] = {0}, lastd[5005] = {0}; long num[5005][5005] = {0}; char c[5005][5005] = {'\0'}; int main() { long i, j, n, m, x, y, tx, ty, ans = 0, count, tot = 0, ansi, la; scanf("%ld %ld\n", &n, &m); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { scanf("%c", &c[i][j]); num[i][j] = ++tot; } scanf("\n"); } for (i = 1; i <= n; i++) for (j = 1, la = 0; j <= m; j++) { lastl[num[i][j]] = la; if (c[i][j] != '.') la = j; } for (i = 1; i <= n; i++) for (j = m, la = 0; j >= 1; j--) { lastr[num[i][j]] = la; if (c[i][j] != '.') la = j; } for (j = 1; j <= m; j++) for (i = 1, la = 0; i <= n; i++) { lastu[num[i][j]] = la; if (c[i][j] != '.') la = i; } for (j = 1; j <= m; j++) for (i = n, la = 0; i >= 1; i--) { lastd[num[i][j]] = la; if (c[i][j] != '.') la = i; } for (x = 1; x <= n; x++) for (y = 1; y <= m; y++) if (c[x][y] != '.') { count = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { lal[num[i][j]] = lastl[num[i][j]]; lar[num[i][j]] = lastr[num[i][j]]; lau[num[i][j]] = lastu[num[i][j]]; lad[num[i][j]] = lastd[num[i][j]]; } tx = x; ty = y; if (tx == 3 && ty == 3) i = 1; while (1) { lal[num[tx][lar[num[tx][ty]]]] = lal[num[tx][ty]]; lar[num[tx][lal[num[tx][ty]]]] = lar[num[tx][ty]]; lau[num[lad[num[tx][ty]]][ty]] = lau[num[tx][ty]]; lad[num[lau[num[tx][ty]]][ty]] = lad[num[tx][ty]]; count++; if (c[tx][ty] == 'L') { if (lal[num[tx][ty]] == 0) break; ty = lal[num[tx][ty]]; } else if (c[tx][ty] == 'R') { if (lar[num[tx][ty]] == 0) break; ty = lar[num[tx][ty]]; } else if (c[tx][ty] == 'U') { if (lau[num[tx][ty]] == 0) break; tx = lau[num[tx][ty]]; } else if (c[tx][ty] == 'D') { if (lad[num[tx][ty]] == 0) break; tx = lad[num[tx][ty]]; } } if (count > ans) { ans = count; ansi = 1; } else if (count == ans) ansi++; } printf("%ld %ld\n", ans, ansi); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char gride[5010]; vector<vector<char> > grid; vector<vector<int> > pesq, pesq2; vector<vector<int> > pdir, pdir2; vector<vector<int> > pcima, pcima2; vector<vector<int> > pbaxo, pbaxo2; void apaga(int i, int j) { if (pesq2[i][j] != -1) pdir2[i][pesq2[i][j]] = pdir2[i][j]; if (pdir2[i][j] != -1) pesq2[i][pdir2[i][j]] = pesq2[i][j]; if (pcima2[i][j] != -1) pbaxo2[pcima2[i][j]][j] = pbaxo2[i][j]; if (pbaxo2[i][j] != -1) pcima2[pbaxo2[i][j]][j] = pcima2[i][j]; } int simula(int i, int j) { if (grid[i][j] == 'U') { int k = pcima2[i][j]; if (k == -1) return 1; apaga(i, j); return 1 + simula(k, j); } if (grid[i][j] == 'D') { int k = pbaxo2[i][j]; if (k == -1) return 1; apaga(i, j); return 1 + simula(k, j); } if (grid[i][j] == 'L') { int k = pesq2[i][j]; if (k == -1) return 1; apaga(i, j); return 1 + simula(i, k); } if (grid[i][j] == 'R') { int k = pdir2[i][j]; if (k == -1) return 1; apaga(i, j); return 1 + simula(i, k); } return 0; } int main() { scanf("%d %d", &n, &m); grid.resize(n); for (int i = 0; i < n; i++) { scanf("%s", gride); grid[i].resize(m); for (int j = 0; j < m; j++) grid[i][j] = gride[j]; } pesq.resize(n); pdir.resize(n); pcima.resize(n); pbaxo.resize(n); for (int i = 0; i < n; i++) { pesq[i].resize(m); pdir[i].resize(m); pcima[i].resize(m); pbaxo[i].resize(m); int atu = -1; for (int j = 0; j < m; j++) if (grid[i][j] != '.') { pesq[i][j] = atu; atu = j; } atu = -1; for (int j = m - 1; j >= 0; j--) if (grid[i][j] != '.') { pdir[i][j] = atu; atu = j; } } for (int j = 0; j < m; j++) { int atu = -1; for (int i = 0; i < n; i++) if (grid[i][j] != '.') { pcima[i][j] = atu; atu = i; } atu = -1; for (int i = n - 1; i >= 0; i--) if (grid[i][j] != '.') { pbaxo[i][j] = atu; atu = i; } } int best = 0; int qts = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (grid[i][j] != '.') { pcima2 = pcima; pbaxo2 = pbaxo; pesq2 = pesq; pdir2 = pdir; int t = simula(i, j); if (t == best) qts++; else if (t > best) { best = t; qts = 1; } } printf("%d %d\n", best, qts); return 0; }
#include <bits/stdc++.h> struct node { int dir; int nxt[4]; int tmp[4]; } nn[6000]; char map[5010][5010]; int cnt, conv[5010][5010], N, M; int cv[300]; int main() { cv['U'] = 0; cv['L'] = 1; cv['R'] = 2; cv['D'] = 3; scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { scanf("%s", map[i]); for (int j = 0; j < M; j++) { if (map[i][j] != '.') { ++cnt; nn[cnt].dir = cv[map[i][j]]; conv[i][j] = cnt; } } } for (int i = 0; i < N; i++) { int pre = 0; for (int j = 0; j < M; j++) { if (conv[i][j] != 0) { int now = conv[i][j]; nn[now].nxt[cv['L']] = pre; nn[pre].nxt[cv['R']] = now; pre = now; } } } for (int j = 0; j < M; j++) { int pre = 0; for (int i = 0; i < N; i++) { if (conv[i][j] != 0) { int now = conv[i][j]; nn[now].nxt[cv['U']] = pre; nn[pre].nxt[cv['D']] = now; pre = now; } } } int maxcnt = 0, max = -1; for (int i = 1; i <= cnt; i++) { for (int j = 1; j <= cnt; j++) { for (int d = 0; d < 4; d++) nn[j].tmp[d] = nn[j].nxt[d]; } int np = i, now = 0; while (np) { now++; int nd = nn[np].dir, nxt, pre; nxt = nn[np].tmp[0]; pre = nn[np].tmp[3]; nn[nxt].tmp[3] = pre; nn[pre].tmp[0] = nxt; nxt = nn[np].tmp[1]; pre = nn[np].tmp[2]; nn[nxt].tmp[2] = pre; nn[pre].tmp[1] = nxt; np = nn[np].tmp[nd]; } if (now > max) { max = now; maxcnt = 0; } if (now == max) { maxcnt++; } } printf("%d %d\n", max, maxcnt); }