text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
int merge(int a, int b) {
if (a == -2) return b;
if (b == -2) return a;
if (!a || !b) return 0;
if (a == b) return a;
return -1;
}
struct dat {
int see[2][200001];
int pre[200001], nxt[200001];
int get(int x, int y) {
int p = -2;
if (!nxt[x]) p = merge(p, see[1][y]);
if (!pre[x]) p = merge(p, see[0][y]);
return p;
}
void del(int x) { nxt[pre[x]] = nxt[x], pre[nxt[x]] = pre[x]; }
void con(int x, int y) { nxt[x] = y, pre[y] = x; }
} a, b, c;
int n, m, k, h, t;
bool del[200001], good[3][200001];
int ans[200001];
struct cor {
int x, y, z;
int hash() { return (x - 1) * m * k + (y - 1) * k + z; }
int hash_x() { return (y - 1) * k + z; }
int hash_y() { return (x - 1) * k + z; }
int hash_z() { return (x - 1) * m + y; }
} q[200001];
void check(int hsh) {
if (del[hsh] || !hsh) return;
int z = (hsh % k == 0) ? k : (hsh % k);
(hsh -= z) /= k;
int y = (hsh % m) + 1;
(hsh -= y - 1) /= m;
int x = hsh + 1;
cor now = (cor){x, y, z};
int p = -2;
p = merge(p, a.get(now.hash(), now.hash_x()));
p = merge(p, b.get(now.hash(), now.hash_y()));
p = merge(p, c.get(now.hash(), now.hash_z()));
if (p == -1 || p == 0) {
del[now.hash()] = 1;
q[++t] = now;
a.del(now.hash());
b.del(now.hash());
c.del(now.hash());
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++)
scanf("%d", &a.see[0][(cor){0, i, j}.hash_x()]);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++)
scanf("%d", &a.see[1][(cor){n + 1, i, j}.hash_x()]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
scanf("%d", &b.see[0][(cor){i, 0, j}.hash_y()]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
scanf("%d", &b.see[1][(cor){i, m + 1, j}.hash_y()]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &c.see[0][(cor){i, j, 0}.hash_z()]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &c.see[1][(cor){i, j, k + 1}.hash_z()]);
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
for (int z = 1; z <= k; z++) {
if (x > 1) a.con((cor){x - 1, y, z}.hash(), (cor){x, y, z}.hash());
if (y > 1) b.con((cor){x, y - 1, z}.hash(), (cor){x, y, z}.hash());
if (z > 1) c.con((cor){x, y, z - 1}.hash(), (cor){x, y, z}.hash());
}
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
for (int z = 1; z <= k; z++) check((cor){x, y, z}.hash());
while (h < t) {
int hsh = q[++h].hash();
check(a.pre[hsh]);
check(a.nxt[hsh]);
check(b.pre[hsh]);
check(b.nxt[hsh]);
check(c.pre[hsh]);
check(c.nxt[hsh]);
}
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
for (int z = 1; z <= k; z++) {
cor now = (cor){x, y, z};
if (!del[now.hash()]) {
good[0][now.hash_x()] = 1;
good[1][now.hash_y()] = 1;
good[2][now.hash_z()] = 1;
}
}
for (int x = 1; x <= n; x++)
for (int y = 1; y <= m; y++)
for (int z = 1; z <= k; z++) {
cor now = (cor){x, y, z};
if ((a.see[0][now.hash_x()] || a.see[1][now.hash_x()]) &&
!good[0][now.hash_x()]) {
puts("-1");
return 0;
}
if ((b.see[0][now.hash_y()] || b.see[1][now.hash_y()]) &&
!good[1][now.hash_y()]) {
puts("-1");
return 0;
}
if ((c.see[0][now.hash_z()] || c.see[1][now.hash_z()]) &&
!good[2][now.hash_z()]) {
puts("-1");
return 0;
}
}
for (int x = 1; x <= n; x++, putchar('\n'))
for (int y = 1; y <= m; y++, putchar('\n'))
for (int z = 1; z <= k; z++) {
cor now = (cor){x, y, z};
int p = -2;
p = merge(p, a.get(now.hash(), now.hash_x()));
p = merge(p, b.get(now.hash(), now.hash_y()));
p = merge(p, c.get(now.hash(), now.hash_z()));
if (p == -2 || del[now.hash()]) p = 0;
printf("%d ", p);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return x;
}
void print(int x) {
if (x > 9) print(x / 10);
putchar((x % 10) ^ 48);
}
int n, m, k;
vector<int> a[200010], b[200010], c[200010], d[200010], e[200010], f[200010];
int L[4000010], R[4000010], D[4000010], U[4000010], F[4000010], B[4000010];
int gethash(int x, int y, int z) {
return x * (m + 2) * (k + 2) + y * (k + 2) + z;
}
void getid(int cur, int &x, int &y, int &z) {
z = cur % (k + 2);
y = cur / (k + 2) % (m + 2);
x = cur / (k + 2) / (m + 2);
}
bool is_bound(int cur) {
int z = cur % (k + 2);
int y = cur / (k + 2) % (m + 2);
int x = cur / (k + 2) / (m + 2);
return !x || x > n || !y || y > m || !z || z > k;
}
bool mrk[4000010];
void dfs(int x, int y, int z) {
if (!x || x > n || !y || y > m || !z || z > k) return;
int cur = gethash(x, y, z);
if (mrk[cur]) return;
int tmp = -1;
if (is_bound(L[cur])) {
if (!a[y][z]) goto DEL;
if (tmp == -1)
tmp = a[y][z];
else if (tmp ^ a[y][z])
goto DEL;
}
if (is_bound(R[cur])) {
if (!b[y][z]) goto DEL;
if (tmp == -1)
tmp = b[y][z];
else if (tmp ^ b[y][z])
goto DEL;
}
if (is_bound(D[cur])) {
if (!c[x][z]) goto DEL;
if (tmp == -1)
tmp = c[x][z];
else if (tmp ^ c[x][z])
goto DEL;
}
if (is_bound(U[cur])) {
if (!d[x][z]) goto DEL;
if (tmp == -1)
tmp = d[x][z];
else if (tmp ^ d[x][z])
goto DEL;
}
if (is_bound(F[cur])) {
if (!e[x][y]) goto DEL;
if (tmp == -1)
tmp = e[x][y];
else if (tmp ^ e[x][y])
goto DEL;
}
if (is_bound(B[cur])) {
if (!f[x][y]) goto DEL;
if (tmp == -1)
tmp = f[x][y];
else if (tmp ^ f[x][y])
goto DEL;
}
return;
DEL:
mrk[cur] = 1;
R[L[cur]] = R[cur];
L[R[cur]] = L[cur];
U[D[cur]] = U[cur];
D[U[cur]] = D[cur];
F[B[cur]] = F[cur];
B[F[cur]] = B[cur];
int a, b, c;
getid(L[cur], a, b, c);
dfs(a, b, c);
getid(R[cur], a, b, c);
dfs(a, b, c);
getid(D[cur], a, b, c);
dfs(a, b, c);
getid(U[cur], a, b, c);
dfs(a, b, c);
getid(F[cur], a, b, c);
dfs(a, b, c);
getid(B[cur], a, b, c);
dfs(a, b, c);
}
int col[4000010];
int main() {
n = read(), m = read(), k = read();
for (int i = 1; i <= m; i++) {
a[i].resize(k + 1);
for (int j = 1; j <= k; j++) a[i][j] = read();
}
for (int i = 1; i <= m; i++) {
b[i].resize(k + 1);
for (int j = 1; j <= k; j++) b[i][j] = read();
}
for (int i = 1; i <= n; i++) {
c[i].resize(k + 1);
for (int j = 1; j <= k; j++) c[i][j] = read();
}
for (int i = 1; i <= n; i++) {
d[i].resize(k + 1);
for (int j = 1; j <= k; j++) d[i][j] = read();
}
for (int i = 1; i <= n; i++) {
e[i].resize(m + 1);
for (int j = 1; j <= m; j++) e[i][j] = read();
}
for (int i = 1; i <= n; i++) {
f[i].resize(m + 1);
for (int j = 1; j <= m; j++) f[i][j] = read();
}
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++)
for (int u = 0; u <= k + 1; u++) {
int x = gethash(i, j, u);
if (i) L[x] = gethash(i - 1, j, u);
if (i <= n) R[x] = gethash(i + 1, j, u);
if (j) D[x] = gethash(i, j - 1, u);
if (j <= m) U[x] = gethash(i, j + 1, u);
if (u) F[x] = gethash(i, j, u - 1);
if (u <= k) B[x] = gethash(i, j, u + 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) dfs(i, j, 1), dfs(i, j, k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) dfs(i, 1, j), dfs(i, m, j);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) dfs(1, i, j), dfs(n, i, j);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
int x = gethash(0, i, j), y = gethash(n + 1, i, j);
if (a[i][j]) {
if (R[x] == y) return puts("-1"), 0;
col[R[x]] = a[i][j];
}
if (b[i][j]) {
if (L[y] == x) return puts("-1"), 0;
col[L[y]] = b[i][j];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
int x = gethash(i, 0, j), y = gethash(i, m + 1, j);
if (c[i][j]) {
if (U[x] == y) return puts("-1"), 0;
col[U[x]] = c[i][j];
}
if (d[i][j]) {
if (D[y] == x) return puts("-1"), 0;
col[D[y]] = d[i][j];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x = gethash(i, j, 0), y = gethash(i, j, k + 1);
if (e[i][j]) {
if (B[x] == y) return puts("-1"), 0;
col[B[x]] = e[i][j];
}
if (f[i][j]) {
if (F[y] == x) return puts("-1"), 0;
col[F[y]] = f[i][j];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int u = 1; u <= k; u++) print(col[gethash(i, j, u)]), putchar(' ');
puts("");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline char nc() {
return getchar();
static char buf[100000], *l = buf, *r = buf;
return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
? EOF
: *l++;
}
template <class T>
void read(T &x) {
x = 0;
int f = 1, ch = nc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = nc();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 - '0' + ch;
ch = nc();
}
x *= f;
}
const int maxn = 2e5 + 50;
int n, m, k;
int a[maxn], b[maxn], c[maxn], d[maxn], e[maxn], f[maxn];
int col[maxn];
bool inq[maxn];
set<int> sx[maxn];
set<int> sy[maxn];
set<int> sz[maxn];
void upd(int x, int y, int z, int c) {
int now = (((x)-1) * m * k + ((y)-1) * k + (z)-1);
if (col[now] == -1)
col[now] = c;
else if (col[now] != c)
col[now] = 0;
}
bool sol() {
static int q[maxn];
int hd = 0, tl = 0;
for (int y = 1, id = 0; y <= m; ++y)
for (int z = 1; z <= k; ++z, ++id)
for (int x = 1; x <= n; ++x) sx[id].insert(x);
for (int x = 1, id = 0; x <= n; ++x)
for (int z = 1; z <= k; ++z, ++id)
for (int y = 1; y <= m; ++y) sy[id].insert(y);
for (int x = 1, id = 0; x <= n; ++x)
for (int y = 1; y <= m; ++y, ++id)
for (int z = 1; z <= k; ++z) sz[id].insert(z);
memset(col, -1, sizeof(col));
for (int y = 1, id = 0; y <= m; ++y)
for (int z = 1; z <= k; ++z, ++id)
read(a[id]), upd(*sx[id].begin(), y, z, a[id]);
for (int y = 1, id = 0; y <= m; ++y)
for (int z = 1; z <= k; ++z, ++id)
read(b[id]), upd(*sx[id].rbegin(), y, z, b[id]);
for (int x = 1, id = 0; x <= n; ++x)
for (int z = 1; z <= k; ++z, ++id)
read(c[id]), upd(x, *sy[id].begin(), z, c[id]);
for (int x = 1, id = 0; x <= n; ++x)
for (int z = 1; z <= k; ++z, ++id)
read(d[id]), upd(x, *sy[id].rbegin(), z, d[id]);
for (int x = 1, id = 0; x <= n; ++x)
for (int y = 1; y <= m; ++y, ++id)
read(e[id]), upd(x, y, *sz[id].begin(), e[id]);
for (int x = 1, id = 0; x <= n; ++x)
for (int y = 1; y <= m; ++y, ++id)
read(f[id]), upd(x, y, *sz[id].rbegin(), f[id]);
for (int x = 1, id = 0; x <= n; ++x)
for (int y = 1; y <= m; ++y)
for (int z = 1; z <= k; ++z, ++id)
if (col[id] == 0) {
inq[id] = 1;
q[tl++] = id;
}
while (hd < tl) {
int u = q[hd++];
int x = u / (m * k);
u -= x * m * k;
++x;
int y = u / k;
u -= y * k;
++y;
int z = u;
++z;
u = (((y)-1) * k + (z)-1);
sx[u].erase(x);
if (!sx[u].empty()) {
int xx = *sx[u].begin(), now = (((xx)-1) * m * k + ((y)-1) * k + (z)-1);
upd(xx, y, z, a[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
xx = *sx[u].rbegin(), now = (((xx)-1) * m * k + ((y)-1) * k + (z)-1);
upd(xx, y, z, b[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
} else if (a[u] || b[u])
return 0;
u = (((x)-1) * k + (z)-1);
sy[u].erase(y);
if (!sy[u].empty()) {
int yy = *sy[u].begin(), now = (((x)-1) * m * k + ((yy)-1) * k + (z)-1);
upd(x, yy, z, c[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
yy = *sy[u].rbegin(), now = (((x)-1) * m * k + ((yy)-1) * k + (z)-1);
upd(x, yy, z, d[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
} else if (c[u] || d[u])
return 0;
u = (((x)-1) * m + (y)-1);
sz[u].erase(z);
if (!sz[u].empty()) {
int zz = *sz[u].begin(), now = (((x)-1) * m * k + ((y)-1) * k + (zz)-1);
upd(x, y, zz, e[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
zz = *sz[u].rbegin(), now = (((x)-1) * m * k + ((y)-1) * k + (zz)-1);
upd(x, y, zz, f[u]);
if (col[now] == 0 && !inq[now]) inq[now] = 1, q[tl++] = now;
} else if (e[u] || f[u])
return 0;
}
for (int x = 1, id = 0; x <= n; ++x)
for (int y = 1; y <= m; ++y)
for (int z = 1; z <= k; ++z, ++id) {
if (col[id] == -1) col[id] = 0;
printf("%d\n", col[id]);
}
}
int main() {
read(n), read(m), read(k);
if (!sol()) puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
int allocator_pos = 0;
char allocator_memory[(int)200 * 1024 * 1024];
inline void *operator new(size_t n) {
char *res = allocator_memory + allocator_pos;
allocator_pos += n;
assert(allocator_pos <= (int)200 * 1024 * 1024);
return (void *)res;
}
inline void operator delete(void *) noexcept {}
inline void operator delete(void *, size_t) {}
struct triple {
int x, y, z;
triple() {}
triple(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
};
int n, m, k;
vector<vector<int>> up, down, left228, right228, top, bottom;
void Read(vector<vector<int>> &have, int a, int b) {
have.resize(a, vector<int>(b));
for (auto &i : have)
for (auto &j : i) cin >> j;
}
void read() {
cin >> n >> m >> k;
Read(left228, m, k);
Read(right228, m, k);
Read(top, n, k);
Read(bottom, n, k);
Read(down, n, m);
Read(up, n, m);
}
bool operator==(const triple &a, const triple &b) {
return tie(a.x, a.y, a.z) == tie(b.x, b.y, b.z);
}
vector<vector<vector<triple>>> par[3];
vector<vector<vector<int>>> Min[3], Max[3];
triple get_par(triple a, int it) {
if (a == par[it][a.x][a.y][a.z]) return a;
return par[it][a.x][a.y][a.z] = get_par(par[it][a.x][a.y][a.z], it);
}
void uni(triple a, triple b, int it) {
a = get_par(a, it);
b = get_par(b, it);
if (a == b) return;
par[it][b.x][b.y][b.z] = a;
chkmin(Min[it][a.x][a.y][a.z], Min[it][b.x][b.y][b.z]);
chkmax(Max[it][a.x][a.y][a.z], Max[it][b.x][b.y][b.z]);
}
void no() {
cout << -1 << endl;
exit(0);
}
vector<vector<vector<bool>>> used;
queue<triple> q;
int get_color(int x, int y, int z) {
set<int> color;
triple a;
if (x != 0) {
a = {x - 1, y, z};
a = get_par(a, 0);
}
if (x == 0 || (Min[0][a.x][a.y][a.z] == 0 && used[x - 1][y][z])) {
color.insert(left228[y][z]);
}
if (x != n - 1) {
a = {x + 1, y, z};
a = get_par(a, 0);
}
if (x == n - 1 || (Max[0][a.x][a.y][a.z] == n - 1 && used[x + 1][y][z])) {
color.insert(right228[y][z]);
}
if (y != 0) {
a = {x, y - 1, z};
a = get_par(a, 1);
}
if (y == 0 || (Min[1][a.x][a.y][a.z] == 0 && used[x][y - 1][z])) {
color.insert(top[x][z]);
}
if (y != m - 1) {
a = {x, y + 1, z};
a = get_par(a, 1);
}
if (y == m - 1 || (Max[1][a.x][a.y][a.z] == m - 1 && used[x][y + 1][z])) {
color.insert(bottom[x][z]);
}
if (z != 0) {
a = {x, y, z - 1};
a = get_par(a, 2);
}
if (z == 0 || (Min[2][a.x][a.y][a.z] == 0 && used[x][y][z - 1])) {
color.insert(down[x][y]);
}
if (z != k - 1) {
a = {x, y, z + 1};
a = get_par(a, 2);
}
if (z == k - 1 || (Max[2][a.x][a.y][a.z] == k - 1 && used[x][y][z + 1])) {
color.insert(up[x][y]);
}
if (color.empty()) return -1;
if (color.size() >= 2) return 0;
return *color.begin();
}
void build() {
used.assign(n, vector<vector<bool>>(m, vector<bool>(k, false)));
for (int i = 0; i < 3; i++) {
par[i].resize(n, vector<vector<triple>>(m, vector<triple>(k)));
Min[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
Max[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
for (int it = 0; it < 3; it++) {
par[it][x][y][z] = {x, y, z};
}
Min[0][x][y][z] = x;
Max[0][x][y][z] = x;
Min[1][x][y][z] = y;
Max[1][x][y][z] = y;
Min[2][x][y][z] = z;
Max[2][x][y][z] = z;
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
if (!get_color(x, y, z)) {
q.push({x, y, z});
used[x][y][z] = true;
}
}
}
}
}
vector<int> dx = {-1, 1, 0, 0, 0, 0};
vector<int> dy = {0, 0, -1, 1, 0, 0};
vector<int> dz = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z) {
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (!used[nx][ny][nz]) continue;
uni({x, y, z}, {nx, ny, nz}, i / 2);
}
}
void bfs() {
while (!q.empty()) {
auto v = q.front();
int x = v.x, y = v.y, z = v.z;
q.pop();
del(x, y, z);
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
if (used[nx][ny][nz]) {
auto a = triple(nx, ny, nz);
a = get_par(a, i / 2);
if (i / 2 == 0) {
if (i % 2 == 0) {
nx = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nx = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else if (i / 2 == 1) {
if (i % 2 == 0) {
ny = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
ny = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else {
if (i % 2 == 0) {
nz = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nz = Max[i / 2][a.x][a.y][a.z] + 1;
}
}
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
}
if (used[nx][ny][nz]) continue;
used[nx][ny][nz] = true;
q.push({nx, ny, nz});
}
}
}
void check_ans() {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int x = 0; x < n; x++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (left228[y][z] || right228[y][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int y = 0; y < m; y++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (top[x][z] || bottom[x][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
bool flag = false;
for (int z = 0; z < k; z++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (up[x][y] || down[x][y]) {
no();
}
}
}
}
}
void run() {
build();
bfs();
check_ans();
}
void write() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
int ans = get_color(x, y, z);
if (ans == -1) ans = 0;
if (used[x][y][z]) ans = 0;
cout << ans << " ";
}
}
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
stack<pair<int, int> > s0, s1, s2, s3, s4, s5;
std::vector<std::vector<std::vector<int> > > a(200005), w(200005);
int n, m, k;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i <= n + 1; i++) {
a[i].resize(m + 5);
w[i].resize(m + 5);
}
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++) {
a[i][j].resize(k + 5);
w[i][j].resize(k + 5);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int l = 1; l <= k; l++) a[i][j][l] = -1;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
s0.push(make_pair(i, j));
scanf("%d", &a[0][i][j]);
w[0][i][j] = 1;
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
s1.push(make_pair(i, j));
scanf("%d", &a[n + 1][i][j]);
w[n + 1][i][j] = n;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
s2.push(make_pair(i, j));
scanf("%d", &a[i][0][j]);
w[i][0][j] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
s3.push(make_pair(i, j));
scanf("%d", &a[i][m + 1][j]);
w[i][m + 1][j] = m;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
s4.push(make_pair(i, j));
scanf("%d", &a[i][j][0]);
w[i][j][0] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
s5.push(make_pair(i, j));
scanf("%d", &a[i][j][k + 1]);
w[i][j][k + 1] = k;
}
bool change = true;
while (change) {
change = false;
while (!s0.empty()) {
int I = s0.top().first, J = s0.top().second;
s0.pop();
for (;; w[0][I][J]++) {
int *D = &w[0][I][J], *C = &a[0][I][J], *W = &a[w[0][I][J]][I][J];
int X = w[0][I][J], Y = I, Z = J;
if (*D == n + 1) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s1.push(make_pair(Y, Z));
s2.push(make_pair(X, Z));
s3.push(make_pair(X, Z));
s4.push(make_pair(X, Y));
s5.push(make_pair(X, Y));
}
if (*D == n && *C) {
printf("-1");
return 0;
}
}
}
while (!s1.empty()) {
int I = s1.top().first, J = s1.top().second;
s1.pop();
for (;; w[n + 1][I][J]--) {
int *D = &w[n + 1][I][J], *C = &a[n + 1][I][J],
*W = &a[w[n + 1][I][J]][I][J];
int X = w[n + 1][I][J], Y = I, Z = J;
if (*D == 0) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s0.push(make_pair(Y, Z));
s2.push(make_pair(X, Z));
s3.push(make_pair(X, Z));
s4.push(make_pair(X, Y));
s5.push(make_pair(X, Y));
}
if (*D == 0 && *C) {
printf("-1");
return 0;
}
}
}
while (!s2.empty()) {
int I = s2.top().first, J = s2.top().second;
s2.pop();
for (;; w[I][0][J]++) {
int *D = &w[I][0][J], *C = &a[I][0][J], *W = &a[I][w[I][0][J]][J];
int X = I, Y = w[I][0][J], Z = J;
if (*D == m + 1) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s0.push(make_pair(Y, Z));
s1.push(make_pair(Y, Z));
s3.push(make_pair(X, Z));
s4.push(make_pair(X, Y));
s5.push(make_pair(X, Y));
}
}
}
while (!s3.empty()) {
int I = s3.top().first, J = s3.top().second;
s3.pop();
for (;; w[I][m + 1][J]--) {
int *D = &w[I][m + 1][J], *C = &a[I][m + 1][J],
*W = &a[I][w[I][m + 1][J]][J];
int X = I, Y = w[I][m + 1][J], Z = J;
if (*D == 0) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s0.push(make_pair(Y, Z));
s1.push(make_pair(Y, Z));
s2.push(make_pair(X, Z));
s4.push(make_pair(X, Y));
s5.push(make_pair(X, Y));
}
if (*D == 0 && *C) {
printf("-1");
return 0;
}
}
}
while (!s4.empty()) {
int I = s4.top().first, J = s4.top().second;
s4.pop();
for (;; w[I][J][0]++) {
int *D = &w[I][J][0], *C = &a[I][J][0], *W = &a[I][J][w[I][J][0]];
int X = I, Y = J, Z = w[I][J][0];
if (*D == k + 1) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s0.push(make_pair(Y, Z));
s1.push(make_pair(Y, Z));
s2.push(make_pair(X, Z));
s3.push(make_pair(X, Z));
s5.push(make_pair(X, Y));
}
if (*D == k && *C) {
printf("-1");
return 0;
}
}
}
while (!s5.empty()) {
int I = s5.top().first, J = s5.top().second;
s5.pop();
for (;; w[I][J][k + 1]--) {
int *D = &w[I][J][k + 1], *C = &a[I][J][k + 1],
*W = &a[I][J][w[I][J][k + 1]];
int X = I, Y = J, Z = w[I][J][k + 1];
if (*D == 0) {
if (*C) {
printf("-1");
return 0;
} else
break;
}
if (*W == -1) {
*W = *C;
change = true;
if (*C) break;
} else if (*W == 0) {
continue;
} else if (*W == *C) {
if (*C) break;
} else {
*W = 0;
change = true;
s0.push(make_pair(Y, Z));
s1.push(make_pair(Y, Z));
s2.push(make_pair(X, Z));
s3.push(make_pair(X, Z));
s4.push(make_pair(X, Y));
}
if (*D == 0 && *C) {
printf("-1");
return 0;
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int l = 1; l <= k; l++) {
if (~a[i][j][l])
printf("%d ", a[i][j][l]);
else
printf("0 ");
}
printf("\n");
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
vvi x0, x1, y0, y1, z0, z1;
vvvi output;
vvvi what;
const vector<int> dx = {1, -1, 0, 0, 0, 0};
const vector<int> dy = {0, 0, 1, -1, 0, 0};
const vector<int> dz = {0, 0, 0, 0, 1, -1};
int n, m, p;
cin >> n >> m >> p;
x0.assign(m, vi(p, 0));
x1.assign(m, vi(p, 0));
y0.assign(n, vi(p, 0));
y1.assign(n, vi(p, 0));
z0.assign(n, vi(m, 0));
z1.assign(n, vi(m, 0));
output.assign(n, vvi(m, vi(p, -1)));
what.assign(n, vvi(m, vi(p, 0)));
for (int i = 0; i < m; i++)
for (int j = 0; j < p; j++) cin >> x0[i][j];
for (int i = 0; i < m; i++)
for (int j = 0; j < p; j++) cin >> x1[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++) cin >> y0[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++) cin >> y1[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> z0[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> z1[i][j];
auto fail = []() {
cout << -1 << '\n';
exit(0);
};
auto cons = [&](int x, int y, int z, int id) {
if (id == 0) return x0[y][z];
if (id == 1) return x1[y][z];
if (id == 2) return y0[x][z];
if (id == 3) return y1[x][z];
if (id == 4) return z0[x][y];
if (id == 5) return z1[x][y];
assert(false);
};
vector<vector<int> > q;
auto add = [&](int x, int y, int z, int id) {
int f = cons(x, y, z, id);
if (x < 0 || y < 0 || z < 0 || x >= n || y >= m || z >= p) {
if (f > 0) fail();
return;
}
what[x][y][z] |= (1 << id);
q.push_back({x, y, z});
};
int s = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < p; j++) {
add(0, i, j, 0);
add(n - 1, i, j, 1);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++) {
add(i, 0, j, 2);
add(i, m - 1, j, 3);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
add(i, j, 0, 4);
add(i, j, p - 1, 5);
}
while (s < (int)q.size()) {
vector<int> r = q[s];
s++;
int x = r[0];
int y = r[1];
int z = r[2];
vector<int> needs;
for (int f = 0; f < 6; f++) {
if ((what[x][y][z] >> f) & 1) needs.push_back(cons(x, y, z, f));
}
for (int a : needs) {
if (a == 0) output[x][y][z] = 0;
for (int b : needs) {
if (a != b) output[x][y][z] = 0;
}
}
if (output[x][y][z] == 0) {
for (int f = 0; f < 6; f++) {
if ((what[x][y][z] >> f) & 1) {
int nx = x + dx[f];
int ny = y + dy[f];
int nz = z + dz[f];
add(nx, ny, nz, f);
}
}
what[x][y][z] = 0;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < p; k++) {
if (output[i][j][k] == -1) {
vector<int> needs;
for (int f = 0; f < 6; f++) {
if ((what[i][j][k] >> f) & 1) needs.push_back(cons(i, j, k, f));
}
for (int a : needs) {
assert(a != 0);
for (int b : needs) {
assert(a == b);
}
}
output[i][j][k] = (needs.empty() ? 0 : needs.front());
}
cout << output[i][j][k] << ' ';
}
cout << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> dx = {1, -1, 0, 0, 0, 0};
vector<int> dy = {0, 0, 1, -1, 0, 0};
vector<int> dz = {0, 0, 0, 0, 1, -1};
struct cube {
int x, y, z;
int dir;
int col;
cube() {}
cube(int _x, int _y, int _z, int _dir, int _col) {
x = _x;
y = _y;
z = _z;
dir = _dir;
col = _col;
}
};
int main() {
queue<cube> q;
int n, m, k;
cin >> n >> m >> k;
int emp[n][m][k];
int col[n][m][k];
vector<cube> in[n][m][k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 0;
col[i][j][p] = -1;
in[i][j][p].clear();
}
}
}
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
cin >> col[0][j][p];
if (col[0][j][p] == 0) {
for (int i = 0; i < n; i++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(0, j, p, 0, col[0][j][p]));
}
}
}
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
cin >> col[n - 1][j][p];
if (col[n - 1][j][p] == 0) {
for (int i = 0; i < n; i++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(n - 1, j, p, 1, col[n - 1][j][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int p = 0; p < k; p++) {
cin >> col[i][0][p];
if (col[i][0][p] == 0) {
for (int j = 0; j < m; j++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, 0, p, 2, col[i][0][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int p = 0; p < k; p++) {
cin >> col[i][m - 1][p];
if (col[i][m - 1][p] == 0) {
for (int j = 0; j < m; j++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, m - 1, p, 3, col[i][m - 1][p]));
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> col[i][j][0];
if (col[i][j][0] == 0) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, j, 0, 4, col[i][j][0]));
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> col[i][j][k - 1];
if (col[i][j][k - 1] == 0) {
for (int p = 0; p < k; p++) {
emp[i][j][p] = 1;
}
} else {
q.push(cube(i, j, k - 1, 5, col[i][j][k - 1]));
}
}
}
int ans[n][m][k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
ans[i][j][p] = -1;
}
}
}
while (!q.empty()) {
auto cur = q.front();
q.pop();
if (cur.x < 0 || cur.x >= n || cur.y < 0 || cur.y >= m || cur.z < 0 ||
cur.z >= k) {
cout << -1;
return 0;
}
if (emp[cur.x][cur.y][cur.z]) {
q.push(cube(cur.x + dx[cur.dir], cur.y + dy[cur.dir], cur.z + dz[cur.dir],
cur.dir, cur.col));
continue;
}
if (ans[cur.x][cur.y][cur.z] == -1) {
ans[cur.x][cur.y][cur.z] = cur.col;
in[cur.x][cur.y][cur.z].push_back(cur);
continue;
}
bool correct = true;
for (auto &f : in[cur.x][cur.y][cur.z]) {
if (f.col != cur.col) {
correct = false;
}
}
in[cur.x][cur.y][cur.z].push_back(cur);
if (correct) {
continue;
}
emp[cur.x][cur.y][cur.z] = 1;
ans[cur.x][cur.y][cur.z] = 0;
for (auto &f : in[cur.x][cur.y][cur.z]) {
q.push(cube(f.x + dx[f.dir], f.y + dy[f.dir], f.z + dz[f.dir], f.dir,
f.col));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int p = 0; p < k; p++) {
if (ans[i][j][p] == -1) {
cout << 0 << " ";
continue;
}
cout << ans[i][j][p] << " ";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, dx[] = {1, -1, 0, 0, 0, 0}, dy[] = {0, 0, 1, -1, 0, 0},
dz[] = {0, 0, 0, 0, 1, -1};
int n, m, kk, a[N];
vector<int> v[N];
inline int id(int x, int y, int z) {
return (x - 1) * m * kk + (y - 1) * kk + z;
}
inline bool chk(int x, int y, int z) {
return x >= 1 && x <= n && y >= 1 && y <= m && z >= 1 && z <= kk;
}
void del(int, int, int);
void ins(int d, int x, int y, int z, int c) {
if (!chk(x, y, z)) {
if (c) {
cout << -1 << endl;
exit(0);
}
return;
}
int tt = id(x, y, z);
if (!c) {
if (a[tt] > 0) del(x, y, z);
a[tt] = 0;
ins(d, x + dx[d], y + dy[d], z + dz[d], c);
} else if (a[tt] == -1 || a[tt] == c) {
a[tt] = c;
v[tt].push_back(d);
} else if (!a[tt])
ins(d, x + dx[d], y + dy[d], z + dz[d], c);
else
del(x, y, z), a[tt] = 0, ins(d, x + dx[d], y + dy[d], z + dz[d], c);
}
void del(int x, int y, int z) {
int tt = id(x, y, z);
while (!v[tt].empty()) {
int d = v[tt].back();
v[tt].pop_back();
ins(d, x + dx[d], y + dy[d], z + dz[d], a[tt]);
}
}
inline int read() {
int x;
cin >> x;
return x;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> kk;
memset(a, -1, sizeof(a));
for (int i = 1; i <= m; i++)
for (int j = 1; j <= kk; j++) ins(0, 1, i, j, read());
for (int i = 1; i <= m; i++)
for (int j = 1; j <= kk; j++) ins(1, n, i, j, read());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= kk; j++) ins(2, i, 1, j, read());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= kk; j++) ins(3, i, m, j, read());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) ins(4, i, j, 1, read());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) ins(5, i, j, kk, read());
for (int i = 1; i <= n * m * kk; i++) cout << max(0, a[i]) << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, t, a[200010][6], dx[6] = {1, -1, 0, 0, 0, 0},
dy[6] = {0, 0, 1, -1, 0, 0},
dz[6] = {0, 0, 0, 0, 1, -1};
bool p[200010];
inline int get(int i, int j, int k) {
return (i - 1) * m * t + (j - 1) * t + k;
}
inline void chk(int i);
inline void orz(int i, int j, int k) {
int x, y, z;
x = (i - 1) / (m * t) + 1;
y = (i - 1) % (m * t) / t + 1;
z = (i - 1) % t + 1;
while (x > 0 && x <= n && y > 0 && y <= m && z > 0 && z <= t &&
p[get(x, y, z)])
x += dx[j], y += dy[j], z += dz[j];
if (!(x > 0 && x <= n && y > 0 && y <= m && z > 0 && z <= t))
if (k == 0)
return;
else {
printf("-1\n");
exit(0);
}
a[get(x, y, z)][j] = k;
chk(get(x, y, z));
}
inline void chk(int i) {
int j, k = 0;
for (j = 0; j < 6; j++)
if (a[i][j] == 0 || a[i][j] > 0 && k > 0 && a[i][j] != k)
break;
else if (a[i][j] > 0)
k = a[i][j];
if (j == 6) return;
p[i] = 1;
for (j = 0; j < 6; j++)
if (a[i][j] >= 0) orz(i, j, a[i][j]);
}
int main() {
int i, j, k;
scanf("%d%d%d", &n, &m, &t);
for (i = 1; i <= n * m * t; i++)
for (j = 0; j < 6; j++) a[i][j] = -1;
for (i = 1; i <= m; i++)
for (j = 1; j <= t; j++) {
scanf("%d", &k);
a[get(1, i, j)][0] = k;
}
for (i = 1; i <= m; i++)
for (j = 1; j <= t; j++) {
scanf("%d", &k);
a[get(n, i, j)][1] = k;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= t; j++) {
scanf("%d", &k);
a[get(i, 1, j)][2] = k;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= t; j++) {
scanf("%d", &k);
a[get(i, m, j)][3] = k;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
scanf("%d", &k);
a[get(i, j, 1)][4] = k;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
scanf("%d", &k);
a[get(i, j, t)][5] = k;
}
for (i = 1; i <= n * m * t; i++)
if (!p[i]) chk(i);
for (i = 1; i <= n * m * t; i++) {
for (j = 0, k = 0; j < 6; j++)
if (a[i][j] > 0) k = a[i][j];
if (p[i]) k = 0;
printf("%d ", k);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
int allocator_pos = 0;
char allocator_memory[(int)256 * 1024 * 1024];
inline void *operator new(size_t n) {
char *res = allocator_memory + allocator_pos;
allocator_pos += n;
assert(allocator_pos <= (int)256 * 1024 * 1024);
return (void *)res;
}
inline void operator delete(void *) noexcept {}
inline void operator delete(void *, size_t) {}
struct triple {
int x, y, z;
triple() {}
triple(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
};
int n, m, k;
vector<vector<int>> up, down, left228, right228, top, bottom;
void Read(vector<vector<int>> &have, int a, int b) {
have.resize(a, vector<int>(b));
for (auto &i : have)
for (auto &j : i) cin >> j;
}
void read() {
cin >> n >> m >> k;
Read(left228, m, k);
Read(right228, m, k);
Read(top, n, k);
Read(bottom, n, k);
Read(down, n, m);
Read(up, n, m);
}
bool operator==(const triple &a, const triple &b) {
return tie(a.x, a.y, a.z) == tie(b.x, b.y, b.z);
}
vector<vector<vector<triple>>> par[3];
vector<vector<vector<int>>> Min[3], Max[3];
triple get_par(triple a, int it) {
if (a == par[it][a.x][a.y][a.z]) return a;
return par[it][a.x][a.y][a.z] = get_par(par[it][a.x][a.y][a.z], it);
}
void uni(triple a, triple b, int it) {
a = get_par(a, it);
b = get_par(b, it);
if (a == b) return;
par[it][b.x][b.y][b.z] = a;
chkmin(Min[it][a.x][a.y][a.z], Min[it][b.x][b.y][b.z]);
chkmax(Max[it][a.x][a.y][a.z], Max[it][b.x][b.y][b.z]);
}
void no() {
cout << -1 << endl;
exit(0);
}
vector<vector<vector<bool>>> used;
queue<triple> q;
int get_color(int x, int y, int z) {
set<int> color;
triple a;
if (x != 0) {
a = {x - 1, y, z};
a = get_par(a, 0);
}
if (x == 0 || (Min[0][a.x][a.y][a.z] == 0 && used[x - 1][y][z])) {
color.insert(left228[y][z]);
}
if (x != n - 1) {
a = {x + 1, y, z};
a = get_par(a, 0);
}
if (x == n - 1 || (Max[0][a.x][a.y][a.z] == n - 1 && used[x + 1][y][z])) {
color.insert(right228[y][z]);
}
if (y != 0) {
a = {x, y - 1, z};
a = get_par(a, 1);
}
if (y == 0 || (Min[1][a.x][a.y][a.z] == 0 && used[x][y - 1][z])) {
color.insert(top[x][z]);
}
if (y != m - 1) {
a = {x, y + 1, z};
a = get_par(a, 1);
}
if (y == m - 1 || (Max[1][a.x][a.y][a.z] == m - 1 && used[x][y + 1][z])) {
color.insert(bottom[x][z]);
}
if (z != 0) {
a = {x, y, z - 1};
a = get_par(a, 2);
}
if (z == 0 || (Min[2][a.x][a.y][a.z] == 0 && used[x][y][z - 1])) {
color.insert(down[x][y]);
}
if (z != k - 1) {
a = {x, y, z + 1};
a = get_par(a, 2);
}
if (z == k - 1 || (Max[2][a.x][a.y][a.z] == k - 1 && used[x][y][z + 1])) {
color.insert(up[x][y]);
}
if (color.empty()) return -1;
if (color.size() >= 2) return 0;
return *color.begin();
}
void build() {
used.assign(n, vector<vector<bool>>(m, vector<bool>(k, false)));
for (int i = 0; i < 3; i++) {
par[i].resize(n, vector<vector<triple>>(m, vector<triple>(k)));
Min[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
Max[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
for (int it = 0; it < 3; it++) {
par[it][x][y][z] = {x, y, z};
}
Min[0][x][y][z] = x;
Max[0][x][y][z] = x;
Min[1][x][y][z] = y;
Max[1][x][y][z] = y;
Min[2][x][y][z] = z;
Max[2][x][y][z] = z;
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
if (!get_color(x, y, z)) {
q.push({x, y, z});
used[x][y][z] = true;
}
}
}
}
}
vector<int> dx = {-1, 1, 0, 0, 0, 0};
vector<int> dy = {0, 0, -1, 1, 0, 0};
vector<int> dz = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z) {
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (!used[nx][ny][nz]) continue;
uni({x, y, z}, {nx, ny, nz}, i / 2);
}
}
void bfs() {
while (!q.empty()) {
auto v = q.front();
int x = v.x, y = v.y, z = v.z;
q.pop();
del(x, y, z);
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
if (used[nx][ny][nz]) {
auto a = triple(nx, ny, nz);
a = get_par(a, i / 2);
if (i / 2 == 0) {
if (i % 2 == 0) {
nx = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nx = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else if (i / 2 == 1) {
if (i % 2 == 0) {
ny = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
ny = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else {
if (i % 2 == 0) {
nz = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nz = Max[i / 2][a.x][a.y][a.z] + 1;
}
}
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
}
if (used[nx][ny][nz]) continue;
used[nx][ny][nz] = true;
q.push({nx, ny, nz});
}
}
}
void check_ans() {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int x = 0; x < n; x++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (left228[y][z] || right228[y][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int y = 0; y < m; y++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (top[x][z] || bottom[x][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
bool flag = false;
for (int z = 0; z < k; z++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (up[x][y] || down[x][y]) {
no();
}
}
}
}
}
void run() {
build();
bfs();
check_ans();
}
void write() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
int ans = get_color(x, y, z);
if (ans == -1) ans = 0;
if (used[x][y][z]) ans = 0;
cout << ans << " ";
}
}
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int DX[6] = {+1, -1, 0, 0, 0, 0}, DY[6] = {0, 0, +1, -1, 0, 0},
DZ[6] = {0, 0, 0, 0, +1, -1};
struct Laser {
int x, y, z, dir, want;
Laser() {}
Laser(int x, int y, int z, int dir, int want)
: x(x), y(y), z(z), dir(dir), want(want) {}
void step() {
x += DX[dir];
y += DY[dir];
z += DZ[dir];
}
};
int na, nb, nc;
Laser laser[6 * MAXN];
int nlaser;
int ans[MAXN];
vector<int> who[MAXN];
queue<int> q;
bool solve() {
for (int i = (0); i < (na * nb * nc); ++i) ans[i] = -1, who[i].clear();
q = queue<int>();
for (int i = (0); i < (nlaser); ++i) q.push(i);
while (!q.empty()) {
int at = q.front();
q.pop();
int x = laser[at].x, y = laser[at].y, z = laser[at].z,
want = laser[at].want;
if (x < 0 || x >= na || y < 0 || y >= nb || z < 0 || z >= nc) {
if (want != 0) return false;
} else {
int target = x * nb * nc + y * nc + z;
if (want == 0) {
for (int i = (0); i < (((int)(who[target]).size())); ++i) {
int to = who[target][i];
laser[to].step();
q.push(to);
}
who[target].clear();
ans[target] = 0;
laser[at].step();
q.push(at);
} else {
if (ans[target] == -1) ans[target] = want;
if (ans[target] != want) {
for (int i = (0); i < (((int)(who[target]).size())); ++i) {
int to = who[target][i];
laser[to].step();
q.push(to);
}
who[target].clear();
ans[target] = 0;
laser[at].step();
q.push(at);
} else {
who[target].push_back(at);
}
}
}
}
for (int i = (0); i < (na * nb * nc); ++i)
if (ans[i] == -1) ans[i] = 0;
return true;
}
void read(int x, int y, int z, int dir) {
int want;
scanf("%d", &want);
laser[nlaser++] = Laser(x, y, z, dir, want);
}
void run() {
scanf("%d%d%d", &na, &nb, &nc);
nlaser = 0;
for (int i = (0); i < (nb); ++i)
for (int j = (0); j < (nc); ++j) read(0, i, j, 0);
for (int i = (0); i < (nb); ++i)
for (int j = (0); j < (nc); ++j) read(na - 1, i, j, 1);
for (int i = (0); i < (na); ++i)
for (int j = (0); j < (nc); ++j) read(i, 0, j, 2);
for (int i = (0); i < (na); ++i)
for (int j = (0); j < (nc); ++j) read(i, nb - 1, j, 3);
for (int i = (0); i < (na); ++i)
for (int j = (0); j < (nb); ++j) read(i, j, 0, 4);
for (int i = (0); i < (na); ++i)
for (int j = (0); j < (nb); ++j) read(i, j, nc - 1, 5);
if (!solve()) {
printf("-1\n");
return;
}
for (int i = (0); i < (na); ++i) {
if (i != 0) puts("");
for (int j = (0); j < (nb); ++j) {
for (int k = (0); k < (nc); ++k) {
if (k != 0) printf(" ");
printf("%d", ans[i * nb * nc + j * nc + k]);
}
puts("");
}
}
}
int main() {
run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
void re(long double& x) {
string t;
re(t);
x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
template <class T>
void re(complex<T>& x) {
T a, b;
re(a, b);
x = {a, b};
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = (0); i < ((int)a.size()); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = (0); i < (SZ); ++i) re(a[i]);
}
} // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
} // namespace output
using namespace output;
namespace io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if ((int)second.size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace io
using namespace io;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
int n, m, k;
vector<vector<int>> bel[3], abv[3];
vector<vector<vector<bool>>> ok;
vector<vector<deque<int>>> ord[3];
void bad() {
ps(-1);
exit(0);
}
void check(vector<vector<int>> a, vector<vector<int>> b) {
for (int i = (0); i < ((int)a.size()); ++i)
for (int j = (0); j < ((int)a[0].size()); ++j) {
if ((a[i][j] == 0) != (b[i][j] == 0)) bad();
}
}
set<int> col;
bool OK(int ind, int x, int y, int p) {
if (ind == 0) return ok[p][x][y];
if (ind == 1) return ok[x][p][y];
return ok[x][y][p];
}
void deal(int ind, int x, int y, int p) {
while ((int)ord[ind][x][y].size() && !OK(ind, x, y, ord[ind][x][y].back()))
ord[ind][x][y].pop_back();
while ((int)ord[ind][x][y].size() && !OK(ind, x, y, ord[ind][x][y].front()))
ord[ind][x][y].pop_front();
if ((int)ord[ind][x][y].size()) {
if (ord[ind][x][y].front() == p) col.insert(bel[ind][x][y]);
if (ord[ind][x][y].back() == p) col.insert(abv[ind][x][y]);
}
}
array<int, 3> DEAL(int ind, int x, int y, int p) {
if (p == ord[ind][x][y].front()) {
deal(ind, x, y, p);
if (!(int)ord[ind][x][y].size()) bad();
return {x, y, ord[ind][x][y].front()};
}
if (p == ord[ind][x][y].back()) {
deal(ind, x, y, p);
if (!(int)ord[ind][x][y].size()) bad();
return {x, y, ord[ind][x][y].back()};
}
return {-1, -1, -1};
}
int main() {
setIO();
re(n, m, k);
bel[0] = vector<vector<int>>(m, vector<int>(k));
for (int b = (0); b < (m); ++b)
for (int c = (0); c < (k); ++c) re(bel[0][b][c]);
abv[0] = vector<vector<int>>(m, vector<int>(k));
for (int b = (0); b < (m); ++b)
for (int c = (0); c < (k); ++c) re(abv[0][b][c]);
bel[1] = vector<vector<int>>(n, vector<int>(k));
for (int b = (0); b < (n); ++b)
for (int c = (0); c < (k); ++c) re(bel[1][b][c]);
abv[1] = vector<vector<int>>(n, vector<int>(k));
for (int b = (0); b < (n); ++b)
for (int c = (0); c < (k); ++c) re(abv[1][b][c]);
bel[2] = vector<vector<int>>(n, vector<int>(m));
for (int b = (0); b < (n); ++b)
for (int c = (0); c < (m); ++c) re(bel[2][b][c]);
abv[2] = vector<vector<int>>(n, vector<int>(m));
for (int b = (0); b < (n); ++b)
for (int c = (0); c < (m); ++c) re(abv[2][b][c]);
for (int i = (0); i < (3); ++i) check(bel[i], abv[i]);
queue<array<int, 3>> q;
ok.assign(n, vector<vector<bool>>(m, vector<bool>(k)));
ord[0].resize(m);
for (int i = (0); i < (m); ++i) ord[0][i].resize(k);
ord[1].resize(n);
for (int i = (0); i < (n); ++i) ord[1][i].resize(k);
ord[2].resize(n);
for (int i = (0); i < (n); ++i) ord[2][i].resize(m);
for (int a = (0); a < (n); ++a)
for (int b = (0); b < (m); ++b)
for (int c = (0); c < (k); ++c) {
if (!bel[0][b][c] || !abv[0][b][c]) continue;
if (!bel[1][a][c] || !abv[1][a][c]) continue;
if (!bel[2][a][b] || !abv[2][a][b]) continue;
ok[a][b][c] = 1;
q.push({a, b, c});
ord[0][b][c].push_back(a);
ord[1][a][c].push_back(b);
ord[2][a][b].push_back(c);
}
array<int, 3> ID = {-1, -1, -1};
while ((int)q.size()) {
auto a = q.front();
q.pop();
if (!ok[a[0]][a[1]][a[2]]) continue;
col.clear();
deal(0, a[1], a[2], a[0]);
deal(1, a[0], a[2], a[1]);
deal(2, a[0], a[1], a[2]);
if ((int)col.size() > 1) {
ok[a[0]][a[1]][a[2]] = 0;
auto A = DEAL(0, a[1], a[2], a[0]);
if (A != ID) q.push({A[2], A[0], A[1]});
auto B = DEAL(1, a[0], a[2], a[1]);
if (B != ID) q.push({B[0], B[2], B[1]});
auto C = DEAL(2, a[0], a[1], a[2]);
if (C != ID) q.push({C[0], C[1], C[2]});
}
}
for (int x = (0); x < (n); ++x)
for (int y = (0); y < (m); ++y)
for (int z = (0); z < (k); ++z) {
if (!ok[x][y][z])
pr(0);
else {
col.clear();
array<int, 3> a = {x, y, z};
col.clear();
deal(0, a[1], a[2], a[0]);
deal(1, a[0], a[2], a[1]);
deal(2, a[0], a[1], a[2]);
assert((int)col.size() <= 1);
if ((int)col.size())
pr(*begin(col));
else
pr(0);
}
pr(' ');
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "RDLU";
const long long MOD2 = (long long)1000000007 * (long long)1000000007;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
int col[200135];
vector<int> vp[200135];
int gid(int x, int y, int z) { return (x - 1) * m * k + (y - 1) * k + z; }
inline void wk(int dir, int C, int x, int y, int z) {
if (x < 1 || x > n || y < 1 || y > m || z < 1 || z > k) {
if (C == 0) return;
et();
}
int id = gid(x, y, z);
if (col[id] == 0)
wk(dir, C, dx[dir] + x, dy[dir] + y, dz[dir] + z);
else if (col[id] < 0) {
col[id] = C;
if (C == 0)
wk(dir, C, dx[dir] + x, dy[dir] + y, dz[dir] + z);
else
vp[id].push_back(dir);
} else if (col[id] != C) {
int cc = col[id];
col[id] = 0;
for (int dir : vp[id]) wk(dir, cc, dx[dir] + x, dy[dir] + y, dz[dir] + z);
wk(dir, C, dx[dir] + x, dy[dir] + y, dz[dir] + z);
} else
vp[id].push_back(dir);
}
void fmain(int tid) {
scanf("%d%d%d", &n, &m, &k);
memset(col, -1, sizeof col);
int x;
for (int(i) = 1; (i) <= (int)(m); (i)++)
for (int(j) = 1; (j) <= (int)(k); (j)++) {
scanf("%d", &x);
wk(0, x, 1, i, j);
}
for (int(i) = 1; (i) <= (int)(m); (i)++)
for (int(j) = 1; (j) <= (int)(k); (j)++) {
scanf("%d", &x);
wk(1, x, n, i, j);
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(k); (j)++) {
scanf("%d", &x);
wk(2, x, i, 1, j);
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(k); (j)++) {
scanf("%d", &x);
wk(3, x, i, m, j);
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(m); (j)++) {
scanf("%d", &x);
wk(4, x, i, j, 1);
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(m); (j)++) {
scanf("%d", &x);
wk(5, x, i, j, k);
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(m); (j)++)
for (int(z) = 1; (z) <= (int)(k); (z)++) {
int c = col[gid(i, j, z)];
if (c < 0) c = 0;
printf("%d ", c);
}
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
struct triple {
int x, y, z;
triple() {}
triple(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
};
int n, m, k;
vector<vector<int>> up, down, left228, right228, top, bottom;
void Read(vector<vector<int>> &have, int a, int b) {
have.resize(a, vector<int>(b));
for (auto &i : have)
for (auto &j : i) cin >> j;
}
void read() {
cin >> n >> m >> k;
Read(left228, m, k);
Read(right228, m, k);
Read(top, n, k);
Read(bottom, n, k);
Read(down, n, m);
Read(up, n, m);
}
bool operator==(const triple &a, const triple &b) {
return tie(a.x, a.y, a.z) == tie(b.x, b.y, b.z);
}
vector<vector<vector<triple>>> par[3];
vector<vector<vector<int>>> Min[3], Max[3];
triple get_par(triple a, int it) {
if (a == par[it][a.x][a.y][a.z]) return a;
return par[it][a.x][a.y][a.z] = get_par(par[it][a.x][a.y][a.z], it);
}
void uni(triple a, triple b, int it) {
a = get_par(a, it);
b = get_par(b, it);
if (a == b) return;
par[it][b.x][b.y][b.z] = a;
chkmin(Min[it][a.x][a.y][a.z], Min[it][b.x][b.y][b.z]);
chkmax(Max[it][a.x][a.y][a.z], Max[it][b.x][b.y][b.z]);
}
void no() {
cout << -1 << endl;
exit(0);
}
vector<vector<vector<bool>>> used;
queue<triple> q;
int get_color(int x, int y, int z) {
set<int> color;
triple a;
if (x != 0) {
a = {x - 1, y, z};
a = get_par(a, 0);
}
if (x == 0 || (Min[0][a.x][a.y][a.z] == 0 && used[x - 1][y][z])) {
color.insert(left228[y][z]);
}
if (x != n - 1) {
a = {x + 1, y, z};
a = get_par(a, 0);
}
if (x == n - 1 || (Max[0][a.x][a.y][a.z] == n - 1 && used[x + 1][y][z])) {
color.insert(right228[y][z]);
}
if (y != 0) {
a = {x, y - 1, z};
a = get_par(a, 1);
}
if (y == 0 || (Min[1][a.x][a.y][a.z] == 0 && used[x][y - 1][z])) {
color.insert(top[x][z]);
}
if (y != m - 1) {
a = {x, y + 1, z};
a = get_par(a, 1);
}
if (y == m - 1 || (Max[1][a.x][a.y][a.z] == m - 1 && used[x][y + 1][z])) {
color.insert(bottom[x][z]);
}
if (z != 0) {
a = {x, y, z - 1};
a = get_par(a, 2);
}
if (z == 0 || (Min[2][a.x][a.y][a.z] == 0 && used[x][y][z - 1])) {
color.insert(down[x][y]);
}
if (z != k - 1) {
a = {x, y, z + 1};
a = get_par(a, 2);
}
if (z == k - 1 || (Max[2][a.x][a.y][a.z] == k - 1 && used[x][y][z + 1])) {
color.insert(up[x][y]);
}
if (color.empty()) return -1;
if (color.size() >= 2) return 0;
return *color.begin();
}
void build() {
used.assign(n, vector<vector<bool>>(m, vector<bool>(k, false)));
for (int i = 0; i < 3; i++) {
par[i].resize(n, vector<vector<triple>>(m, vector<triple>(k)));
Min[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
Max[i].resize(n, vector<vector<int>>(m, vector<int>(k)));
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
for (int it = 0; it < 3; it++) {
par[it][x][y][z] = {x, y, z};
}
Min[0][x][y][z] = x;
Max[0][x][y][z] = x;
Min[1][x][y][z] = y;
Max[1][x][y][z] = y;
Min[2][x][y][z] = z;
Max[2][x][y][z] = z;
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
if (!get_color(x, y, z)) {
q.push({x, y, z});
used[x][y][z] = true;
}
}
}
}
}
vector<int> dx = {-1, 1, 0, 0, 0, 0};
vector<int> dy = {0, 0, -1, 1, 0, 0};
vector<int> dz = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z) {
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (!used[nx][ny][nz]) continue;
uni({x, y, z}, {nx, ny, nz}, i / 2);
}
}
void bfs() {
while (!q.empty()) {
auto v = q.front();
int x = v.x, y = v.y, z = v.z;
q.pop();
del(x, y, z);
for (int i = 0; i < 6; i++) {
int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
if (used[nx][ny][nz]) {
auto a = triple(nx, ny, nz);
a = get_par(a, i / 2);
if (i / 2 == 0) {
if (i % 2 == 0) {
nx = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nx = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else if (i / 2 == 1) {
if (i % 2 == 0) {
ny = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
ny = Max[i / 2][a.x][a.y][a.z] + 1;
}
} else {
if (i % 2 == 0) {
nz = Min[i / 2][a.x][a.y][a.z] - 1;
} else {
nz = Max[i / 2][a.x][a.y][a.z] + 1;
}
}
if (!check(nx, ny, nz)) continue;
if (get_color(nx, ny, nz)) continue;
}
if (used[nx][ny][nz]) continue;
assert(!used[nx][ny][nz]);
used[nx][ny][nz] = true;
q.push({nx, ny, nz});
}
}
}
void check_ans() {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int x = 0; x < n; x++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (left228[y][z] || right228[y][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int z = 0; z < k; z++) {
bool flag = false;
for (int y = 0; y < m; y++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (top[x][z] || bottom[x][z]) {
no();
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
bool flag = false;
for (int z = 0; z < k; z++) {
if (!used[x][y][z]) {
flag = true;
}
}
if (!flag) {
if (up[x][y] || down[x][y]) {
no();
}
}
}
}
}
void run() {
build();
bfs();
check_ans();
}
void write() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
for (int z = 0; z < k; z++) {
int ans = get_color(x, y, z);
if (ans == -1) ans = 0;
if (used[x][y][z]) ans = 0;
cout << ans << " ";
}
}
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
#include <bits/stdc++.h>
const int N = 4e5 + 5;
const int dx[] = {1, -1, 0, 0, 0, 0}, dy[] = {0, 0, 1, -1, 0, 0},
dz[] = {0, 0, 0, 0, 1, -1};
std::vector<int> sensors[N];
int n, m, k, a[N], c;
inline int id(int x, int y, int z) { return (x - 1) * m * k + (y - 1) * k + z; }
inline bool in(int x, int y, int z) {
return x >= 1 && x <= n && y >= 1 && y <= m && z >= 1 && z <= k;
}
void color(int d, int x, int y, int z, int c) {
if (!in(x, y, z)) {
if (c) {
puts("-1");
exit(0);
}
return;
}
int t = id(x, y, z);
auto erase = [&]() {
while (!sensors[t].empty()) {
int d = sensors[t].back();
sensors[t].pop_back();
color(d, x + dx[d], y + dy[d], z + dz[d], a[t]);
}
a[t] = 0;
};
if (!c) {
if (a[t] > 0) {
erase();
} else {
a[t] = 0;
}
color(d, x + dx[d], y + dy[d], z + dz[d], c);
} else if (a[t] == -1 || a[t] == c) {
a[t] = c;
sensors[t].push_back(d);
} else if (!a[t]) {
color(d, x + dx[d], y + dy[d], z + dz[d], c);
} else {
erase();
color(d, x + dx[d], y + dy[d], z + dz[d], c);
}
}
int main() {
std::memset(a, -1, sizeof(a));
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d", &c);
color(0, 1, i, j, c);
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d", &c);
color(1, n, i, j, c);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d", &c);
color(2, i, 1, j, c);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d", &c);
color(3, i, m, j, c);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &c);
color(4, i, j, 1, c);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &c);
color(5, i, j, k, c);
}
}
for (int i = 1; i <= n * m * k; ++i) {
printf("%d ", std::max(a[i], 0));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
int chkmax(T& a, T b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
template <class T>
int chkmin(T& a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class iterator>
void output(iterator begin, iterator end, ostream& out = cerr) {
while (begin != end) {
out << (*begin) << " ";
begin++;
}
out << endl;
}
template <class T>
void output(T x, ostream& out = cerr) {
output(x.begin(), x.end(), out);
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, m, k;
vector<vector<int> > dir{{1, 0, 0}, {-1, 0, 0}, {0, 1, 0},
{0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
struct keks {
vector<int> dir_used;
set<int> colors;
int must_empty;
keks() {
dir_used.resize(6, -2);
must_empty = 0;
}
void change(int pos, int val) {
dir_used[pos] = val;
if (val == -1) {
must_empty = 1;
} else {
colors.insert(val);
if (colors.size() > 1) {
must_empty = 1;
}
}
}
};
vector<vector<vector<keks> > > superkek;
queue<vector<int> > q;
vector<vector<vector<int> > > ans;
void bfs() {
while (!q.empty()) {
vector<int> v = q.front();
q.pop();
int OUT = (v[0] == 3 && v[1] == 3 && v[2] == 3);
ans[v[0]][v[1]][v[2]] = -1;
for (int it = 0; it < 6; ++it) {
int x = v[0] + dir[it][0];
int y = v[1] + dir[it][1];
int z = v[2] + dir[it][2];
int clr = superkek[v[0]][v[1]][v[2]].dir_used[it];
if (clr != -2) {
int ok = (x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k);
if (clr != -1 && !ok) {
cout << -1 << endl;
exit(0);
}
if (ok) {
int before = superkek[x][y][z].must_empty;
int kikos = 0;
if (superkek[x][y][z].dir_used[it] == -2) {
kikos = 1;
}
superkek[x][y][z].change(it, clr);
int now = superkek[x][y][z].must_empty;
if (now == 1 && kikos) {
q.push({x, y, z});
}
}
}
}
}
}
void colour() {
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
for (int z = 0; z < k; ++z) {
if (ans[x][y][z] != -1) {
ans[x][y][z] = (superkek[x][y][z].colors.empty()
? 0
: (*superkek[x][y][z].colors.begin()));
}
}
}
}
}
vector<vector<vector<int> > > req(3), sat(3);
void no() {
cout << -1 << endl;
exit(0);
}
void check() {
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
for (int z = 0; z < k; ++z) {
if (ans[x][y][z] != -1) {
sat[0][x][y] = 1;
sat[1][x][z] = 1;
sat[2][y][z] = 1;
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (req[0][i][j] != sat[0][i][j]) {
no();
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (req[1][i][j] != sat[1][i][j]) {
no();
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < k; ++j) {
if (req[2][i][j] != sat[2][i][j]) {
no();
}
}
}
}
signed main() {
fast_io();
cin >> n >> m >> k;
superkek.resize(n, vector<vector<keks> >(m, vector<keks>(k)));
req[0].resize(n, vector<int>(m));
req[1].resize(n, vector<int>(k));
req[2].resize(m, vector<int>(k));
sat[0].resize(n, vector<int>(m));
sat[1].resize(n, vector<int>(k));
sat[2].resize(m, vector<int>(k));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < k; ++j) {
int clr;
cin >> clr;
clr--;
superkek[0][i][j].change(0, clr);
if (clr != -1) {
req[2][i][j] = 1;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < k; ++j) {
int clr;
cin >> clr;
clr--;
superkek[n - 1][i][j].change(1, clr);
if (clr != -1) {
req[2][i][j] = 1;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
int clr;
cin >> clr;
clr--;
superkek[i][0][j].change(2, clr);
if (clr != -1) {
req[1][i][j] = 1;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
int clr;
cin >> clr;
clr--;
superkek[i][m - 1][j].change(3, clr);
if (clr != -1) {
req[1][i][j] = 1;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int clr;
cin >> clr;
clr--;
superkek[i][j][0].change(4, clr);
if (clr != -1) {
req[0][i][j] = 1;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int clr;
cin >> clr;
clr--;
superkek[i][j][k - 1].change(5, clr);
if (clr != -1) {
req[0][i][j] = 1;
}
}
}
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
for (int z = 0; z < k; ++z) {
if (superkek[x][y][z].must_empty) {
q.push({x, y, z});
}
}
}
}
ans.resize(n, vector<vector<int> >(m, vector<int>(k, -2)));
bfs();
colour();
check();
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
for (int z = 0; z < k; ++z) {
cout << ans[x][y][z] + 1 << " ";
}
cout << "\n";
}
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
struct pos {
long long x, y, z, dx, dy, dz, color;
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, k;
cin >> n >> m >> k;
vector<vector<vector<long long>>> result(
n, vector<vector<long long>>(m, vector<long long>(k, -1)));
queue<pos> todo;
map<tuple<long long, long long, long long>, vector<pos>> done;
for (long long i = 0; i < m; i++)
for (long long j = 0; j < k; j++) {
long long x;
cin >> x;
todo.push({0, i, j, 1, 0, 0, x});
}
for (long long i = 0; i < m; i++)
for (long long j = 0; j < k; j++) {
long long x;
cin >> x;
todo.push({n - 1, i, j, -1, 0, 0, x});
}
for (long long i = 0; i < n; i++)
for (long long j = 0; j < k; j++) {
long long x;
cin >> x;
todo.push({i, 0, j, 0, 1, 0, x});
}
for (long long i = 0; i < n; i++)
for (long long j = 0; j < k; j++) {
long long x;
cin >> x;
todo.push({i, m - 1, j, 0, -1, 0, x});
}
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) {
long long x;
cin >> x;
todo.push({i, j, 0, 0, 0, 1, x});
}
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) {
long long x;
cin >> x;
todo.push({i, j, k - 1, 0, 0, -1, x});
}
while (!todo.empty()) {
pos p = todo.front();
todo.pop();
if (done.find(tuple<long long, long long, long long>{p.x, p.y, p.z}) ==
done.end())
done[tuple<long long, long long, long long>{p.x, p.y, p.z}] =
vector<pos>();
done[tuple<long long, long long, long long>{p.x, p.y, p.z}].push_back(p);
if (p.color == 0 ||
(result[p.x][p.y][p.z] != -1 && result[p.x][p.y][p.z] != p.color)) {
result[p.x][p.y][p.z] = 0;
for (pos pp :
done[tuple<long long, long long, long long>{p.x, p.y, p.z}]) {
if (pp.x + pp.dx < 0 || pp.x + pp.dx >= n || pp.y + pp.dy < 0 ||
pp.y + pp.dy >= m || pp.z + pp.dz < 0 || pp.z + pp.dz >= k) {
if (pp.color != 0) {
cout << -1 << endl;
return 0;
}
} else {
todo.push({pp.x + pp.dx, pp.y + pp.dy, pp.z + pp.dz, pp.dx, pp.dy,
pp.dz, pp.color});
}
}
done[tuple<long long, long long, long long>{p.x, p.y, p.z}].clear();
} else if (result[p.x][p.y][p.z] == -1) {
result[p.x][p.y][p.z] = p.color;
}
}
for (long long a = 0; a < n; a++) {
for (long long b = 0; b < m; b++) {
for (long long c = 0; c < k; c++) {
cout << max(0LL, result[a][b][c]) << " ";
}
cout << endl;
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
template <class T1, class T2>
inline bool cmin(T1 &a, const T2 &b) {
return b < a ? (a = b, true) : false;
}
template <class T1, class T2>
inline bool cmax(T1 &a, const T2 &b) {
return a < b ? (a = b, true) : false;
}
template <class Type>
Type read() {
Type a;
bool b;
unsigned char c;
while (c = getchar() - 48, (c > 9) & (c != 253))
;
for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
;
return b ? -a : a;
}
int (*rd)() = read<int>;
const int N = 2e5, D[][3] = {
1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1,
};
int n, m, k, sz[3], col[6][N];
bool vis[N][6], gg[N];
std::set<int> c[N];
std::queue<int> q;
int id(int x, int y, int z) { return (x * m + y) * k + z; }
void real(int b, int *res) {
res[0] = b / k / m;
res[1] = b / k % m;
res[2] = b % k;
}
void rm(int id) {
if (gg[id]) return;
gg[id] = true;
q.push(id);
}
int observer(int d, int *co) {
int res = 0;
for (int i = 0; i < 3; ++i) {
if (!D[d][i]) res = res * sz[i] + co[i];
}
return res;
}
void cast(int d, int x, int y, int z) {
int co[] = {x, y, z}, b = id(x, y, z), o = observer(d, co);
vis[b][d] = true;
if (col[d][o] == 0) return rm(b);
c[b].insert(col[d][o]);
if (c[b].size() > 1) rm(b);
}
int main() {
n = sz[0] = rd();
m = sz[1] = rd();
k = sz[2] = rd();
for (int i = 0; i < m * k; ++i) col[0][i] = rd(), cast(0, 0, i / k, i % k);
for (int i = 0; i < m * k; ++i)
col[1][i] = rd(), cast(1, n - 1, i / k, i % k);
for (int i = 0; i < n * k; ++i) col[2][i] = rd(), cast(2, i / k, 0, i % k);
for (int i = 0; i < n * k; ++i)
col[3][i] = rd(), cast(3, i / k, m - 1, i % k);
for (int i = 0; i < n * m; ++i) col[4][i] = rd(), cast(4, i / m, i % m, 0);
for (int i = 0; i < n * m; ++i)
col[5][i] = rd(), cast(5, i / m, i % m, k - 1);
while (!q.empty()) {
int b = q.front();
q.pop();
for (int i = 0; i < 6; ++i) {
if (vis[b][i]) {
int co[3];
bool in = true;
real(b, co);
while (in && gg[id(co[0], co[1], co[2])]) {
for (int j = 0; j < 3; ++j) {
co[j] += D[i][j];
in &= co[j] >= 0 && co[j] < sz[j];
}
}
if (in)
cast(i, co[0], co[1], co[2]);
else {
real(b, co);
if (col[i][observer(i, co)]) {
puts("-1");
return 0;
}
}
}
}
}
for (int i = 0, b = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int l = 0; l < k; ++l) {
printf("%d ", gg[b] || c[b].empty() ? 0 : *c[b].begin());
++b;
}
puts("");
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
inline int id(int x, int y, int z) {
return x * (m + 2) * (k + 2) + y * (k + 2) + z;
}
int num[2000005], up[2000005];
bool vis[2000005];
struct Point {
int x, y, z;
Point() {}
Point(int a, int b, int c) : x(a), y(b), z(c) {}
};
queue<Point> q;
bool bfs() {
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
int u = id(0, i, j), v = id(n + 1, i, j);
up[u] = 1;
if (!vis[id(1, i, j)]) {
int t = id(1, i, j);
if (!num[u] || (num[t] != -1 && num[t] != num[u])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(1, i, j));
} else
num[t] = num[u];
}
up[v] = n;
if (!vis[id(n, i, j)]) {
int t = id(n, i, j);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(n, i, j));
} else
num[t] = num[v];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
int u = id(i, 0, j), v = id(i, m + 1, j);
up[u] = 1;
if (!vis[id(i, 1, j)]) {
int t = id(i, 1, j);
if (!num[u] || (num[t] != -1 && num[t] != num[u])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(i, 1, j));
} else
num[t] = num[u];
}
up[v] = m;
if (!vis[id(i, m, j)]) {
int t = id(i, m, j);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(i, m, j));
} else
num[t] = num[v];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int u = id(i, j, 0), v = id(i, j, k + 1);
up[u] = 1;
if (!vis[id(i, j, 1)]) {
int t = id(i, j, 1);
if (!num[u] || (num[t] != -1 && num[t] != num[u])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(i, j, 1));
} else
num[t] = num[u];
}
up[v] = k;
if (!vis[id(i, j, k)]) {
int t = id(i, j, k);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(i, j, k));
} else
num[t] = num[v];
}
}
while (!q.empty()) {
Point t = q.front();
q.pop();
int x = t.x, y = t.y, z = t.z;
int u = id(x, y, z);
int v = id(0, y, z);
while (up[v] <= n && vis[id(up[v], y, z)]) up[v]++;
if (up[v] <= n && !vis[id(up[v], y, z)]) {
int t = id(up[v], y, z);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(up[v], y, z));
} else
num[t] = num[v];
}
v = id(n + 1, y, z);
while (up[v] && vis[id(up[v], y, z)]) up[v]--;
if (up[v] && !vis[id(up[v], y, z)]) {
int t = id(up[v], y, z);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(up[v], y, z));
} else
num[t] = num[v];
}
v = id(x, 0, z);
while (up[v] <= m && vis[id(x, up[v], z)]) up[v]++;
if (up[v] <= m && !vis[id(x, up[v], z)]) {
int t = id(x, up[v], z);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(x, up[v], z));
} else
num[t] = num[v];
}
v = id(x, m + 1, z);
while (up[v] && vis[id(x, up[v], z)]) up[v]--;
if (up[v] && !vis[id(x, up[v], z)]) {
int t = id(x, up[v], z);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(x, up[v], z));
} else
num[t] = num[v];
}
v = id(x, y, 0);
while (up[v] <= k && vis[id(x, y, up[v])]) up[v]++;
if (up[v] <= k && !vis[id(x, y, up[v])]) {
int t = id(x, y, up[v]);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(x, y, up[v]));
} else
num[t] = num[v];
}
v = id(x, y, k + 1);
while (up[v] && vis[id(x, y, up[v])]) up[v]--;
if (up[v] && !vis[id(x, y, up[v])]) {
int t = id(x, y, up[v]);
if (!num[v] || (num[t] != -1 && num[t] != num[v])) {
num[t] = 0;
vis[t] = 1;
q.push(Point(x, y, up[v]));
} else
num[t] = num[v];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int l = 1; l <= k; l++)
if (num[id(i, j, l)] == -1) num[id(i, j, l)] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
if (num[id(0, i, j)] && up[id(0, i, j)] > n) return 0;
if (num[id(n + 1, i, j)] && up[id(n + 1, i, j)] < 1) return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
if (num[id(i, 0, j)] && up[id(i, 0, j)] > m) return 0;
if (num[id(i, m + 1, j)] && up[id(i, m + 1, j)] < 1) return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (num[id(i, j, 0)] && up[id(i, j, 0)] > k) return 0;
if (num[id(i, j, k + 1)] && up[id(i, j, k + 1)] < 1) return 0;
}
return 1;
}
int main() {
memset(num, 255, sizeof(num));
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) scanf("%d", &num[id(0, i, j)]);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) scanf("%d", &num[id(n + 1, i, j)]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) scanf("%d", &num[id(i, 0, j)]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) scanf("%d", &num[id(i, m + 1, j)]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &num[id(i, j, 0)]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &num[id(i, j, k + 1)]);
if (!bfs()) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int l = 1; l <= k; l++) printf("%d ", num[id(i, j, l)]);
printf("\n");
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar();
bool flag = false;
while (!isdigit(c)) {
if (c == '-') flag = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
if (flag) x = -x;
}
int n, m, k;
bool flag;
int dx[6] = {1, -1, 0, 0, 0, 0}, dy[6] = {0, 0, 1, -1, 0, 0},
dz[6] = {0, 0, 0, 0, 1, -1}, c[400010];
vector<int> ve[400010];
void add(int x, int y, int z, int v, int t);
void del(int x, int y, int z);
bool check(int x, int y, int z) {
return x >= 1 && x <= n && y >= 1 && y <= m && z >= 1 && z <= k;
}
void add(int x, int y, int z, int v, int t) {
if (!check(x, y, z)) {
if (v) flag = true;
return;
}
int p = (((x - 1) * m + y - 1) * k + z);
if (!v) {
if (c[p] > 0) del(x, y, z);
c[p] = 0, add(x + dx[t], y + dy[t], z + dz[t], v, t);
return;
}
if (c[p]) {
if (c[p] == v || c[p] == -1)
c[p] = v, ve[p].push_back(t);
else
del(x, y, z), c[p] = 0, add(x + dx[t], y + dy[t], z + dz[t], v, t);
} else
add(x + dx[t], y + dy[t], z + dz[t], v, t);
}
void del(int x, int y, int z) {
int p = (((x - 1) * m + y - 1) * k + z);
while (!ve[p].empty()) {
int t = ve[p].back(), v = c[p];
ve[p].pop_back(), add(x + dx[t], y + dy[t], z + dz[t], v, t);
}
}
int main() {
read(n), read(m), read(k), memset(c, -1, sizeof(c));
for (int i = 1; i <= m; ++i)
for (int j = 1, v; j <= k; ++j) read(v), add(1, i, j, v, 0);
for (int i = 1; i <= m; ++i)
for (int j = 1, v; j <= k; ++j) read(v), add(n, i, j, v, 1);
for (int i = 1; i <= n; ++i)
for (int j = 1, v; j <= k; ++j) read(v), add(i, 1, j, v, 2);
for (int i = 1; i <= n; ++i)
for (int j = 1, v; j <= k; ++j) read(v), add(i, m, j, v, 3);
for (int i = 1; i <= n; ++i)
for (int j = 1, v; j <= m; ++j) read(v), add(i, j, 1, v, 4);
for (int i = 1; i <= n; ++i)
for (int j = 1, v; j <= m; ++j) read(v), add(i, j, k, v, 5);
if (flag) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
for (int l = 1; l <= k; ++l)
printf("%d%c", max(c[(((i - 1) * m + j - 1) * k + l)], 0),
" \n"[l == k]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
int n, m, K, x, a[N];
vector<int> vec[N];
bool IN(int x, int y, int z) {
return 1 <= x && x <= n && 1 <= y && y <= m && 1 <= z && z <= K;
}
int ID(int x, int y, int z) {
assert(IN(x, y, z));
return (x - 1) * m * K + (y - 1) * K + (z - 1);
}
void GG() {
puts("-1");
exit(0);
}
void erase(int, int, int);
void Insert(int, int, int, int, int);
bool flg = 0;
void insert(int d, int x, int y, int z, int c) {
if (!IN(x, y, z)) {
if (c) GG();
return;
}
int id = ID(x, y, z);
if (!c) {
if (a[id] > 0) erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
} else if (a[id] == -1 || a[id] == c) {
a[id] = c;
vec[id].push_back(d);
} else if (!a[id])
Insert(d, x, y, z, c);
else {
erase(x, y, z);
a[id] = 0;
Insert(d, x, y, z, c);
}
}
void Insert(int d, int x, int y, int z, int c) {
insert(d, x + dx[d], y + dy[d], z + dz[d], c);
}
void erase(int x, int y, int z) {
int id = ID(x, y, z);
for (; vec[id].size();) {
int v = vec[id].back();
vec[id].pop_back();
Insert(v, x, y, z, a[id]);
}
}
int main() {
scanf("%d%d%d", &n, &m, &K);
memset(a, -1, sizeof(a));
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(0, 1, i, j, x);
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(1, n, i, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(2, i, 1, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(K); j++)
scanf("%d", &x), insert(3, i, m, j, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(4, i, j, 1, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
scanf("%d", &x), insert(5, i, j, K, x);
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
for (int k = (int)(1); k <= (int)(K); k++) {
int ans = a[ID(i, j, k)];
printf("%d%c", max(ans, 0), k == K ? '\n' : ' ');
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long inf = 1000 * 1000 * 1000 + 7;
long long inf64 = inf * inf;
long long mod = 228228227;
long long math_mod = 998244353;
const int maxn = 50005;
float START_TIME;
mt19937 gen(time(0));
int n, m, k;
vector<vector<vector<int>>> f;
vector<vector<tuple<int, int, int>>> LEFT;
vector<vector<tuple<int, int, int>>> RIGHT;
vector<vector<tuple<int, int, int>>> TOP;
vector<vector<tuple<int, int, int>>> BOTTOM;
vector<vector<tuple<int, int, int>>> FRONT;
vector<vector<tuple<int, int, int>>> BACK;
vector<vector<int>> LEFT_BLOCK;
vector<vector<int>> RIGHT_BLOCK;
vector<vector<int>> TOP_BLOCK;
vector<vector<int>> BOTTOM_BLOCK;
vector<vector<int>> FRONT_BLOCK;
vector<vector<int>> BACK_BLOCK;
bool is_in_borders(int x, int y, int z) {
return (x > 0 && y > 0 && z > 0 && x <= n && y <= m && z <= k);
}
bool check(int x, int y, int z) {
tuple<int, int, int> tup = make_tuple(x, y, z);
unordered_set<int> u;
if (LEFT[y][z] == tup) u.insert(LEFT_BLOCK[y][z]);
if (RIGHT[y][z] == tup) u.insert(RIGHT_BLOCK[y][z]);
if (TOP[x][z] == tup) u.insert(TOP_BLOCK[x][z]);
if (BOTTOM[x][z] == tup) u.insert(BOTTOM_BLOCK[x][z]);
if (FRONT[x][y] == tup) u.insert(FRONT_BLOCK[x][y]);
if (BACK[x][y] == tup) u.insert(BACK_BLOCK[x][y]);
if ((u.find(0) != u.end()) || (u.size() > 1)) {
return false;
}
return true;
}
void NO() {
cout << -1;
exit(0);
}
void rec(int x, int y, int z) {
if ((f[x][y][z] != -1) || (check(x, y, z))) return;
f[x][y][z] = 1;
while (true) {
int X, Y, Z;
tie(X, Y, Z) = LEFT[y][z];
if (f[X][Y][Z] == -1) break;
LEFT[y][z] = make_tuple(X + 1, Y, Z);
if (!is_in_borders(X + 1, Y, Z)) {
if (LEFT_BLOCK[Y][Z] != 0)
NO();
else
break;
}
if (f[X + 1][Y][Z] == -1) {
rec(X + 1, Y, Z);
break;
}
}
while (true) {
int X, Y, Z;
tie(X, Y, Z) = RIGHT[y][z];
if (f[X][Y][Z] == -1) break;
RIGHT[y][z] = make_tuple(X - 1, Y, Z);
if (!is_in_borders(X - 1, Y, Z)) {
if (RIGHT_BLOCK[Y][Z] != 0)
NO();
else
break;
}
if (f[X - 1][Y][Z] == -1) {
rec(X - 1, Y, Z);
break;
}
}
while (true) {
int X, Y, Z;
tie(X, Y, Z) = BOTTOM[x][z];
if (f[X][Y][Z] == -1) break;
BOTTOM[x][z] = make_tuple(X, Y + 1, Z);
if (!is_in_borders(X, Y + 1, Z)) {
if (BOTTOM_BLOCK[X][Z] != 0)
NO();
else
break;
}
if (f[X][Y + 1][Z] == -1) {
rec(X, Y + 1, Z);
break;
}
}
while (true) {
int X, Y, Z;
tie(X, Y, Z) = TOP[x][z];
if (f[X][Y][Z] == -1) break;
TOP[x][z] = make_tuple(X, Y - 1, Z);
if (!is_in_borders(X, Y - 1, Z)) {
if (TOP_BLOCK[X][Z] != 0)
NO();
else
break;
}
if (f[X][Y - 1][Z] == -1) {
rec(X, Y - 1, Z);
break;
}
}
while (true) {
int X, Y, Z;
tie(X, Y, Z) = FRONT[x][y];
if (f[X][Y][Z] == -1) break;
FRONT[x][y] = make_tuple(X, Y, Z + 1);
if (!is_in_borders(X, Y, Z + 1)) {
if (FRONT_BLOCK[X][Y] != 0)
NO();
else
break;
}
if (f[X][Y][Z + 1] == -1) {
rec(X, Y, Z + 1);
break;
}
}
while (true) {
int X, Y, Z;
tie(X, Y, Z) = BACK[x][y];
if (f[X][Y][Z] == -1) break;
BACK[x][y] = make_tuple(X, Y, Z - 1);
if (!is_in_borders(X, Y, Z - 1)) {
if (BACK_BLOCK[X][Y] != 0)
NO();
else
break;
}
if (f[X][Y][Z - 1] == -1) {
rec(X, Y, Z - 1);
break;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> k;
f.resize(n + 2, vector<vector<int>>(m + 2, vector<int>(k + 2, -1)));
LEFT.resize(m + 1, vector<tuple<int, int, int>>(k + 1));
RIGHT.resize(m + 1, vector<tuple<int, int, int>>(k + 1));
TOP.resize(n + 1, vector<tuple<int, int, int>>(k + 1));
BOTTOM.resize(n + 1, vector<tuple<int, int, int>>(k + 1));
FRONT.resize(n + 1, vector<tuple<int, int, int>>(m + 1));
BACK.resize(n + 1, vector<tuple<int, int, int>>(m + 1));
LEFT_BLOCK.resize(m + 1, vector<int>(k + 1, 0));
RIGHT_BLOCK.resize(m + 1, vector<int>(k + 1, 0));
TOP_BLOCK.resize(n + 1, vector<int>(k + 1, 0));
BOTTOM_BLOCK.resize(n + 1, vector<int>(k + 1, 0));
FRONT_BLOCK.resize(n + 1, vector<int>(m + 1, 0));
BACK_BLOCK.resize(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
LEFT[i][j] = make_tuple(1, i, j);
RIGHT[i][j] = make_tuple(n, i, j);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
BOTTOM[i][j] = make_tuple(i, 1, j);
TOP[i][j] = make_tuple(i, m, j);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
FRONT[i][j] = make_tuple(i, j, 1);
BACK[i][j] = make_tuple(i, j, k);
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
cin >> LEFT_BLOCK[i][j];
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
cin >> RIGHT_BLOCK[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
cin >> BOTTOM_BLOCK[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
cin >> TOP_BLOCK[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> FRONT_BLOCK[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> BACK_BLOCK[i][j];
}
}
bool ff = false;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
for (int z = 1; z <= k; ++z) {
if ((f[i][j][z] != -1) || ff) continue;
rec(i, j, z);
}
}
}
cout << endl;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= m; ++y) {
for (int z = 1; z <= k; ++z) {
if (f[x][y][z] != -1)
f[x][y][z] = 0;
else {
f[x][y][z] = 0;
tuple<int, int, int> tup = make_tuple(x, y, z);
if (LEFT[y][z] == tup) f[x][y][z] = LEFT_BLOCK[y][z];
if (RIGHT[y][z] == tup) f[x][y][z] = RIGHT_BLOCK[y][z];
if (TOP[x][z] == tup) f[x][y][z] = TOP_BLOCK[x][z];
if (BOTTOM[x][z] == tup) f[x][y][z] = BOTTOM_BLOCK[x][z];
if (FRONT[x][y] == tup) f[x][y][z] = FRONT_BLOCK[x][y];
if (BACK[x][y] == tup) f[x][y][z] = BACK_BLOCK[x][y];
}
cout << f[x][y][z] << " ";
}
}
}
cerr << endl << (clock() - START_TIME) / CLOCKS_PER_SEC << " sec." << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 3e5 + 100;
int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x;
}
int n, m, k, gg, ans[maxn], obs[maxn];
int encode(const int x, const int y, const int z) {
return ((x - 1) * m + (y - 1)) * k + z;
}
const int dx[] = {1, -1, 0, 0, 0, 0}, dy[] = {0, 0, 1, -1, 0, 0},
dz[] = {0, 0, 0, 0, 1, -1};
void dfs(const int x, const int y, const int z, const int d, const int c) {
if (!x || !y || !z || x > n || y > m || z > k) {
if (c) gg = true;
return;
}
const int now = encode(x, y, z);
if (c) {
if (ans[now] == -1 || ans[now] == c) {
ans[now] = c;
obs[now] |= (1 << d);
return;
}
if (ans[now] == 0) {
dfs(x + dx[d], y + dy[d], z + dz[d], d, c);
return;
}
}
if (ans[now] > 0) {
for (int i = 0; i < 6; i++)
if ((obs[now] >> i) & 1) {
obs[now] ^= (1 << i);
dfs(x + dx[i], y + dy[i], z + dz[i], i, ans[now]);
if (gg) return;
}
}
ans[now] = 0;
dfs(x + dx[d], y + dy[d], z + dz[d], d, c);
}
int main() {
n = read(), m = read(), k = read();
const int N = n * m * k;
for (int i = 1; i <= N; i++) ans[i] = -1;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
dfs(1, i, j, 0, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
dfs(n, i, j, 1, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
dfs(i, 1, j, 2, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
dfs(i, m, j, 3, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
dfs(i, j, 1, 4, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
dfs(i, j, k, 5, read());
if (gg) return puts("-1"), 0;
}
for (int i = 1; i <= N; i++) printf("%d ", ans[i] == -1 ? 0 : ans[i]);
puts("");
return 0;
}
|
#include <bits/stdc++.h>
int read() {
static int c, x;
while ((c = getchar()) < 48) {
}
x = c & 15;
while ((c = getchar()) >= 48) x = x * 10 + (c & 15);
return x;
}
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
bool fail;
int n, m, k;
int a[200010], b[200010];
void dfs(const int d, const int x, const int y, const int z, const int c) {
if (!x || !y || !z || x > n || y > m || z > k) {
if (c) fail = true;
return;
}
const int now = ((x - 1) * m + (y - 1)) * k + z;
if (c) {
if (a[now] == -1 || a[now] == c) {
a[now] = c;
b[now] |= 1 << d;
return;
}
if (a[now] == 0) {
dfs(d, x + dx[d], y + dy[d], z + dz[d], c);
return;
}
}
if (a[now] > 0) {
for (int i = 0; i != 6; ++i) {
if (b[now] & (1 << i)) {
b[now] ^= 1 << i;
dfs(i, x + dx[i], y + dy[i], z + dz[i], a[now]);
if (fail) return;
}
}
}
a[now] = 0;
dfs(d, x + dx[d], y + dy[d], z + dz[d], c);
}
int main() {
n = read();
m = read();
k = read();
const int N = n * m * k;
for (int i = 1; i <= N; ++i) {
a[i] = -1;
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
if (!fail) dfs(0, 1, i, j, read());
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= k; ++j) {
if (!fail) dfs(1, n, i, j, read());
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
if (!fail) dfs(2, i, 1, j, read());
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
if (!fail) dfs(3, i, m, j, read());
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!fail) dfs(4, i, j, 1, read());
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!fail) dfs(5, i, j, k, read());
}
}
if (fail) {
puts("-1");
return 0;
}
for (int i = 1; i <= N; ++i) {
printf("%d ", (a[i] > 0) ? a[i] : 0);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, ok, nn, mm, kk;
queue<pair<pair<int, int>, int> > q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
pair<int, int> mk[m + 1][k + 1], nk[n + 1][k + 1], nm[n + 1][m + 1],
qmk[m + 1][k + 1], qnk[n + 1][k + 1], qnm[n + 1][m + 1];
int nmk[n + 1][m + 1][k + 1];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
cin >> mk[i][j].first;
if (mk[i][j].first == 0) mk[i][j].first = -1;
qmk[i][j].first = 1;
q.push(make_pair(make_pair(1, i), j));
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
cin >> mk[i][j].second;
if (mk[i][j].second == 0) mk[i][j].second = -1;
qmk[i][j].second = n;
q.push(make_pair(make_pair(n, i), j));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
cin >> nk[i][j].first;
if (nk[i][j].first == 0) nk[i][j].first = -1;
qnk[i][j].first = 1;
q.push(make_pair(make_pair(i, 1), j));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
cin >> nk[i][j].second;
if (nk[i][j].second == 0) nk[i][j].second = -1;
qnk[i][j].second = m;
q.push(make_pair(make_pair(i, m), j));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> nm[i][j].first;
if (nm[i][j].first == 0) nm[i][j].first = -1;
qnm[i][j].first = 1;
q.push(make_pair(make_pair(i, j), 1));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> nm[i][j].second;
if (nm[i][j].second == 0) nm[i][j].second = -1;
qnm[i][j].second = k;
q.push(make_pair(make_pair(i, j), k));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int u = 1; u <= k; u++) nmk[i][j][u] = 0;
while (!q.empty()) {
nn = q.front().first.first;
mm = q.front().first.second;
kk = q.front().second;
q.pop();
ok = 1;
if (nmk[nn][mm][kk] != -1) {
if (qnm[nn][mm].first == kk)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == nm[nn][mm].first))
nmk[nn][mm][kk] = nm[nn][mm].first;
else
ok = -1;
if (qnm[nn][mm].second == kk)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == nm[nn][mm].second))
nmk[nn][mm][kk] = nm[nn][mm].second;
else
ok = -1;
if (qnk[nn][kk].first == mm)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == nk[nn][kk].first))
nmk[nn][mm][kk] = nk[nn][kk].first;
else
ok = -1;
if (qnk[nn][kk].second == mm)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == nk[nn][kk].second))
nmk[nn][mm][kk] = nk[nn][kk].second;
else
ok = -1;
if (qmk[mm][kk].first == nn)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == mk[mm][kk].first))
nmk[nn][mm][kk] = mk[mm][kk].first;
else
ok = -1;
if (qmk[mm][kk].second == nn)
if (ok == 1 &&
(nmk[nn][mm][kk] == 0 || nmk[nn][mm][kk] == mk[mm][kk].second))
nmk[nn][mm][kk] = mk[mm][kk].second;
else
ok = -1;
}
if (ok == -1 || nmk[nn][mm][kk] == -1) {
nmk[nn][mm][kk] = -1;
if (qnm[nn][mm].first == kk) {
qnm[nn][mm].first++;
if (kk < k) q.push(make_pair(make_pair(nn, mm), kk + 1));
}
if (qnm[nn][mm].second == kk) {
qnm[nn][mm].second--;
if (kk > 1) q.push(make_pair(make_pair(nn, mm), kk - 1));
}
if (qnk[nn][kk].first == mm) {
qnk[nn][kk].first++;
if (mm < m) q.push(make_pair(make_pair(nn, mm + 1), kk));
}
if (qnk[nn][kk].second == mm) {
qnk[nn][kk].second--;
if (mm > 1) q.push(make_pair(make_pair(nn, mm - 1), kk));
}
if (qmk[mm][kk].first == nn) {
qmk[mm][kk].first++;
if (nn < n) q.push(make_pair(make_pair(nn + 1, mm), kk));
}
if (qmk[mm][kk].second == nn) {
qmk[mm][kk].second--;
if (nn > 1) q.push(make_pair(make_pair(nn - 1, mm), kk));
}
}
}
ok = 1;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) {
if (qmk[i][j].first > n && mk[i][j].first != -1) ok = -1;
if (qmk[i][j].second < 1 && mk[i][j].second != -1) ok = -1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
if (qnk[i][j].first > m && nk[i][j].first != -1) ok = -1;
if (qnk[i][j].second < 1 && nk[i][j].second != -1) ok = -1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (qnm[i][j].first > k && nm[i][j].first != -1) ok = -1;
if (qnm[i][j].second < 1 && nm[i][j].second != -1) ok = -1;
}
if (ok == -1) {
cout << "-1";
return 0;
} else
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int u = 1; u <= k; u++)
if (nmk[i][j][u] != -1)
cout << nmk[i][j][u] << ' ';
else
cout << '0' << ' ';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, m, k, dx[6] = {1, -1, 0, 0, 0, 0}, dy[6] = {0, 0, 1, -1, 0, 0},
dz[6] = {0, 0, 0, 0, 1, -1}, a[N];
vector<int> vec[N];
void No() {
cout << -1;
exit(0);
}
int id(int x, int y, int z) { return (x - 1) * m * k + (y - 1) * k + z; }
bool in(int x, int y, int z) {
return 1 <= x && x <= n && 1 <= y && y <= m && 1 <= z && z <= k;
}
void erase(int, int, int);
void insert(int d, int x, int y, int z, int c) {
if (!in(x, y, z)) {
if (c) No();
return;
}
int t = id(x, y, z);
if (!c) {
if (a[t] > 0) erase(x, y, z);
a[t] = 0;
insert(d, x + dx[d], y + dy[d], z + dz[d], c);
} else if (!~a[t] || a[t] == c) {
a[t] = c;
vec[t].push_back(d);
} else if (!a[t])
insert(d, x + dx[d], y + dy[d], z + dz[d], c);
else
erase(x, y, z), a[t] = 0, insert(d, x + dx[d], y + dy[d], z + dz[d], c);
}
void erase(int x, int y, int z) {
int t = id(x, y, z);
while (!vec[t].empty()) {
int d = vec[t].back();
vec[t].pop_back();
insert(d, x + dx[d], y + dy[d], z + dz[d], a[t]);
}
}
int main() {
memset(a, -1, sizeof(a));
cin >> n >> m >> k;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) insert(0, 1, i, j, gi());
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++) insert(1, n, i, j, gi());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) insert(2, i, 1, j, gi());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) insert(3, i, m, j, gi());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) insert(4, i, j, 1, gi());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) insert(5, i, j, k, gi());
for (int i = 1; i <= n * m * k; i++) cout << max(0, a[i]) << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <typename T>
struct vec3 {
int N[3];
vector<T> data;
vec3() = default;
vec3(int N0, int N1, int N2, T val = 0) : N{N0, N1, N2} {
data.resize(N0 * N1 * N2, val);
}
T& operator()(int i0, int i1, int i2) {
return data[(i0 * N[1] + i1) * N[2] + i2];
}
};
template <typename T>
struct vec2 {
int N[2];
vector<T> data;
vec2() = default;
vec2(int N0, int N1, T val = 0) : N{N0, N1} { data.resize(N0 * N1, val); }
T& operator()(int i0, int i1) { return data[i0 * N[1] + i1]; }
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N[3];
for (int i = 0; i < 3; i++) cin >> N[i];
vec2<int> A[3][2];
int dim[3][2];
for (int k = 0; k < 3; k++)
for (int l = 0; l < 2; l++) {
dim[k][0] = k ? 0 : 1;
dim[k][1] = 3 - k - dim[k][0];
A[k][l] = vec2<int>(N[dim[k][0]], N[dim[k][1]]);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) cin >> A[k][l](i, j);
}
vec3<int> ans(N[0], N[1], N[2], -1);
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (!A[k][0](i, j) || !A[k][1](i, j)) {
if (A[k][0](i, j) || A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
for (idx[k] = 0; idx[k] < N[k]; idx[k]++)
ans(idx[0], idx[1], idx[2]) = 0;
}
vec3<int> border[8];
for (int c = 0; c < 8; c++) border[c] = vec3<int>(N[0], N[1], N[2], 0);
vec3<int> cur[8];
for (int c = 0; c < 8; c++) cur[c] = vec3<int>(N[0], N[1], N[2], -1);
vec2<int> B[8][3];
for (int c = 0; c < 8; c++)
for (int k = 0; k < 3; k++) {
int x = (c >> k) & 1;
B[c][k] = vec2<int>(N[dim[k][0]], N[dim[k][1]], 0);
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++) {
int i_r = i, j_r = j;
if ((c >> dim[k][0]) & 1) i_r = N[dim[k][0]] - 1 - i;
if ((c >> dim[k][1]) & 1) j_r = N[dim[k][1]] - 1 - j;
B[c][k](i_r, j_r) = A[k][x](i, j);
}
}
for (int r = 0; r < min(N[0], min(N[1], N[2])); r++)
for (int c = 0; c < 8; c++) {
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (i == 0) border[c](i, j, k) |= 1;
if (j == 0) border[c](i, j, k) |= 2;
if (k == 0) border[c](i, j, k) |= 4;
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (ans(i, j, k) == 0) cur[c](i_r, j_r, k_r) = 0;
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
if (!border[c](i, j, k)) continue;
int idx[3] = {i, j, k};
if (cur[c](i, j, k) == -1)
for (int l = 0; l < 3; l++)
if ((border[c](i, j, k) >> l) & 1) {
if (cur[c](i, j, k) == -1)
cur[c](i, j, k) = B[c][l](idx[dim[l][0]], idx[dim[l][1]]);
else if (cur[c](i, j, k) !=
B[c][l](idx[dim[l][0]], idx[dim[l][1]]))
cur[c](i, j, k) = 0;
}
int b = border[c](i, j, k);
if (cur[c](i, j, k) == 0) {
for (int l = 0; l < 3; l++) {
idx[l]++;
if (idx[l] != N[l])
border[c](idx[0], idx[1], idx[2]) |= b & (1 << l);
idx[l]--;
}
} else {
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++) {
int i_r = i, j_r = j, k_r = k;
if (c & 1) i_r = N[0] - 1 - i;
if (c & 2) j_r = N[1] - 1 - j;
if (c & 4) k_r = N[2] - 1 - k;
if (cur[c](i, j, k) != -1) {
if (ans(i_r, j_r, k_r) != -1 &&
cur[c](i, j, k) != ans(i_r, j_r, k_r))
ans(i_r, j_r, k_r) = 0;
else
ans(i_r, j_r, k_r) = cur[c](i, j, k);
}
}
}
for (int k = 0; k < 3; k++)
for (int i = 0; i < N[dim[k][0]]; i++)
for (int j = 0; j < N[dim[k][1]]; j++)
if (A[k][0](i, j)) {
int idx[3];
idx[dim[k][0]] = i;
idx[dim[k][1]] = j;
idx[k] = 0;
while (idx[k] < N[k] && !ans(idx[0], idx[1], idx[2])) idx[k]++;
if (idx[k] == N[k] || ans(idx[0], idx[1], idx[2]) != A[k][0](i, j)) {
cout << "-1\n";
return 0;
}
idx[k] = N[k] - 1;
while (idx[k] >= 0 && !ans(idx[0], idx[1], idx[2])) idx[k]--;
if (idx[k] == -1 || ans(idx[0], idx[1], idx[2]) != A[k][1](i, j)) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < N[0]; i++)
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
if (ans(i, j, k) == -1) ans(i, j, k) = 0;
for (int i = 0; i < N[0]; i++) {
if (i) cout << "\n";
for (int j = 0; j < N[1]; j++)
for (int k = 0; k < N[2]; k++)
cout << ans(i, j, k) << ((k == N[2] - 1) ? "\n" : " ");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
template <typename T>
void chkmax(T &x, T y) {
x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int n, m, t, l, r;
int bx[MAXN], by[MAXN], bz[MAXN];
vector<vector<int>> b[6];
vector<vector<vector<int>>> a;
vector<vector<vector<vector<bool>>>> vis;
void Make(vector<vector<int>> &b, int n, int m) {
b.resize(n + 2);
for (int i = 0; i <= n + 1; i++) b[i].resize(m + 2);
}
bool inside(int x, int y, int z) {
return 1 <= x && x <= n && 1 <= y && y <= m && 1 <= z && z <= t;
}
void add(int x, int y, int z, int d, int v) {
assert(!vis[x][y][z][d]);
vis[x][y][z][d] = true;
if (a[x][y][z] == 0) return;
if (v == 0) {
r++, a[x][y][z] = 0;
bx[r] = x, by[r] = y, bz[r] = z;
} else if (a[x][y][z] == -1 || a[x][y][z] == v)
a[x][y][z] = v;
else {
r++, a[x][y][z] = 0;
bx[r] = x, by[r] = y, bz[r] = z;
}
}
int main() {
read(n), read(m), read(t), l = 1;
a.resize(n + 2), vis.resize(n + 2);
for (int i = 0; i <= n + 1; i++) {
a[i].resize(m + 2);
vis[i].resize(m + 2);
for (int j = 0; j <= m + 1; j++) {
a[i][j].resize(t + 2);
vis[i][j].resize(t + 2);
for (int k = 0; k <= t + 1; k++) vis[i][j][k].resize(6);
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int k = 1; k <= t; k++) a[i][j][k] = -1;
Make(b[0], m, t);
Make(b[1], m, t);
Make(b[2], n, t);
Make(b[3], n, t);
Make(b[4], n, m);
Make(b[5], n, m);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= t; j++) {
read(b[0][i][j]);
add(1, i, j, 0, b[0][i][j]);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= t; j++) {
read(b[1][i][j]);
add(n, i, j, 1, b[1][i][j]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= t; j++) {
read(b[2][i][j]);
add(i, 1, j, 2, b[2][i][j]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= t; j++) {
read(b[3][i][j]);
add(i, m, j, 3, b[3][i][j]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
read(b[4][i][j]);
add(i, j, 1, 4, b[4][i][j]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
read(b[5][i][j]);
add(i, j, t, 5, b[5][i][j]);
}
while (l <= r) {
for (int i = 0; i <= 5; i++) {
int x = bx[l], y = by[l], z = bz[l], v = 0;
if (i <= 1)
v = b[i][y][z];
else if (i <= 3)
v = b[i][x][z];
else
v = b[i][x][y];
if (vis[x][y][z][i]) {
while (inside(x, y, z) && a[x][y][z] == 0) {
x += dx[i];
y += dy[i];
z += dz[i];
}
if (!inside(x, y, z)) {
if (v != 0) {
puts("-1");
return 0;
}
} else
add(x, y, z, i, v);
}
}
l++;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= t; k++) printf("%d ", max(a[i][j][k], 0));
printf("\n");
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &a, const T &b) {
a = a > b ? a : b;
}
template <typename T>
void chkmin(T &a, const T &b) {
a = a < b ? a : b;
}
const int MAXN = 300005;
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
int clr[MAXN], n, m, K;
vector<int> opt[MAXN];
int get_id(int x, int y, int z) { return (x - 1) * m * K + (y - 1) * K + z; }
void update(int d, int c, int x, int y, int z) {
if (x < 1 || x > n || y < 1 || y > m || z < 1 || z > K) {
if (c == 0) return;
puts("-1");
exit(0);
}
int id = get_id(x, y, z);
auto go_next = [&]() { update(d, c, x + dx[d], y + dy[d], z + dz[d]); };
if (clr[id] == 0) {
go_next();
} else if (clr[id] < 0) {
clr[id] = c;
if (c == 0)
go_next();
else
opt[id].push_back(d);
} else if (clr[id] != c) {
int cc = clr[id];
clr[id] = 0;
for (int o : opt[id]) update(o, cc, x + dx[o], y + dy[o], z + dz[o]);
go_next();
} else
opt[id].push_back(d);
}
int main() {
memset(clr, -1, sizeof(clr));
scanf("%d%d%d", &n, &m, &K);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= K; j++) {
int x;
scanf("%d", &x);
update(0, x, 1, i, j);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= K; j++) {
int x;
scanf("%d", &x);
update(1, x, n, i, j);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= K; j++) {
int x;
scanf("%d", &x);
update(2, x, i, 1, j);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= K; j++) {
int x;
scanf("%d", &x);
update(3, x, i, m, j);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
update(4, x, i, j, 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
update(5, x, i, j, K);
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++, puts(""))
for (int k = 1; k <= K; k++) {
int f = clr[get_id(i, j, k)];
printf("%d ", f < 0 ? 0 : f);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct fastio {
char s[100000];
int it, len;
fastio() { it = len = 0; }
inline char get() {
if (it < len) return s[it++];
it = 0;
len = fread(s, 1, 100000, stdin);
if (len == 0)
return EOF;
else
return s[it++];
}
bool notend() {
char c = get();
while (c == ' ' || c == '\n') c = get();
if (it > 0) it--;
return c != EOF;
}
} _buff;
inline long long getnum() {
long long r = 0;
bool ng = 0;
char c;
c = _buff.get();
while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
if (c == '-') ng = 1, c = _buff.get();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
char c = _buff.get();
while (c <= 32) c = _buff.get();
return c;
}
const int mod = 1e9 + 7;
long long qpow(long long x, long long k) {
return k == 0 ? 1
: 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
int n, m, k;
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
int getid(int x, int y, int z) {
assert(x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k);
return x * m * k + y * k + z;
}
const int maxn = 200111;
int a[maxn];
vector<int> d[maxn];
void fail() {
puts("-1");
exit(0);
}
bool in_range(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k;
}
void del(int x, int y, int z);
void add1(int op, int x, int y, int z, int c);
void add(int op, int x, int y, int z, int c) {
if (!in_range(x, y, z)) {
if (c != 0) fail();
return;
}
if (c == 0) {
int id = getid(x, y, z);
if (a[id] > 0) del(x, y, z);
a[id] = 0;
add1(op, x, y, z, c);
} else {
int id = getid(x, y, z);
if (a[id] == 0) {
add1(op, x, y, z, c);
} else if (a[id] > 0 && a[id] != c) {
del(x, y, z);
add1(op, x, y, z, c);
} else {
a[id] = c;
d[id].push_back(op);
}
}
}
void add1(int op, int x, int y, int z, int c) {
add(op, x + dx[op], y + dy[op], z + dz[op], c);
}
void del(int x, int y, int z) {
int id = getid(x, y, z);
assert(a[id] > 0);
int c = a[id];
a[id] = 0;
for (auto t : d[id]) {
add1(t, x, y, z, c);
}
}
int main() {
n = getnum(), m = getnum(), k = getnum();
memset(a, -1, sizeof(a));
for (int i = 0; i < m; i++)
for (int j = 0; j < k; j++) {
int x;
x = getnum();
add(0, 0, i, j, x);
}
for (int i = 0; i < m; i++)
for (int j = 0; j < k; j++) {
int x;
x = getnum();
add(1, n - 1, i, j, x);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) {
int x;
x = getnum();
add(2, i, 0, j, x);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) {
int x;
x = getnum();
add(3, i, m - 1, j, x);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int x;
x = getnum();
add(4, i, j, 0, x);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int x;
x = getnum();
add(5, i, j, k - 1, x);
}
for (int i = 0; i < n * m * k; i++) {
if (a[i] == -1) a[i] = 0;
printf("%d ", a[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(long long a) {
if (a < 0) {
a = -a;
putchar('-');
}
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(long long a) {
write(a);
puts("");
}
inline void wri(long long a) {
write(a);
putchar(' ');
}
inline unsigned long long rnd() {
return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
vector<vector<int> > a[6], A[6];
vector<vector<vector<int> > > vis;
struct da {
int x, y, z;
};
int cnt[200005];
vector<vector<vector<vector<da> > > > v;
int n, m, k, N[6], M[6];
da get(da jb) {
int id = jb.x, x = jb.y, y = jb.z;
if (id < 2)
return (da){A[id][x][y], x, y};
else if (id < 4)
return (da){x, A[id][x][y], y};
else
return (da){x, y, A[id][x][y]};
}
void GG() {
puts("-1");
exit(0);
}
bool ou(int x, int l, int r) { return x < l || x > r; }
void ins(da zs);
void bao(da zs) {
int id = zs.x, x = zs.y, y = zs.z;
A[id][x][y] += id % 2 ? -1 : 1;
ins(zs);
}
void erase(da zb) {
vis[zb.x][zb.y][zb.z] = 0;
vector<da> wzp;
swap(wzp, v[zb.x][zb.y][zb.z]);
for (auto i : wzp) bao(i);
}
void ins(da zs) {
da zb = get(zs);
int id = zs.x, x = zs.y, y = zs.z;
if (ou(zb.x, 1, n) || ou(zb.y, 1, m) || ou(zb.z, 1, k)) {
if (a[id][x][y]) GG();
return;
}
if (vis[zb.x][zb.y][zb.z]) {
v[zb.x][zb.y][zb.z].push_back(zs);
if (!a[id][x][y]) erase(zb);
set<int> s;
for (auto i : v[zb.x][zb.y][zb.z]) s.insert(a[i.x][i.y][i.z]);
if (s.size() == 2) erase(zb);
} else {
bao(zs);
}
}
int main() {
n = read();
m = read();
k = read();
for (int i = (int)(0); i <= (int)(5); i++) {
N[i] = i / 2 ? n : m;
M[i] = i / 2 == 2 ? m : k;
a[i].assign(N[i] + 2, vector<int>(M[i] + 2, 0));
A[i].assign(N[i] + 2, vector<int>(M[i] + 2, 0));
}
v.resize(n + 2);
vis.resize(n + 2);
for (int i = (int)(1); i <= (int)(n); i++) {
v[i].resize(m + 2);
vis[i].resize(m + 2);
for (int j = (int)(1); j <= (int)(m); j++) {
v[i][j].resize(k + 2);
vis[i][j].resize(k + 2);
}
}
for (int i = (int)(0); i <= (int)(5); i++) {
for (int j = (int)(1); j <= (int)(N[i]); j++) {
for (int o = (int)(1); o <= (int)(M[i]); o++) {
A[i][j][o] = i % 2 ? n + m + k - N[i] - M[i] : 1;
a[i][j][o] = read();
}
}
}
for (int i = (int)(1); i <= (int)(n); i++)
for (int j = (int)(1); j <= (int)(m); j++)
for (int o = (int)(1); o <= (int)(k); o++) vis[i][j][o] = 1;
for (int i = (int)(0); i <= (int)(5); i++) {
for (int j = (int)(1); j <= (int)(N[i]); j++) {
for (int o = (int)(1); o <= (int)(M[i]); o++) {
ins((da){i, j, o});
}
}
}
for (int i = (int)(1); i <= (int)(n); i++) {
for (int j = (int)(1); j <= (int)(m); j++) {
for (int o = (int)(1); o <= (int)(k); o++) {
if (v[i][j][o].size()) {
da t = v[i][j][o][0];
wri(a[t.x][t.y][t.z]);
} else
wri(0);
}
puts("");
}
puts("");
}
}
|
#include <bits/stdc++.h>
using namespace std;
static const int INF = 500000000;
template <class T>
void debug(T a, T b) {
for (; a != b; ++a) cerr << *a << ' ';
cerr << endl;
}
int w, h, n;
char buf[55][55];
pair<int, int> moveTo[55][55][4][2];
int state[55][55][4][2];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
cin >> h >> n;
for (int i = 0; i < h; ++i) cin >> buf[i];
w = strlen(buf[0]);
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j)
for (int k = 0; k < 4; ++k)
for (int l = 0; l < 2; ++l) {
int cx = j, cy = i;
while (1) {
int px = cx + dx[k], py = cy + dy[k];
if (px < 0 || py < 0 || px >= w || py >= h ||
buf[py][px] != buf[cy][cx])
break;
cx = px;
cy = py;
}
int add;
if (l == 0)
add = 3;
else
add = 1;
int k2 = (k + add) % 4;
int px, py;
while (1) {
px = cx + dx[k2], py = cy + dy[k2];
if (px < 0 || py < 0 || px >= w || py >= h ||
buf[py][px] != buf[cy][cx])
break;
cx = px;
cy = py;
}
px = cx + dx[k];
py = cy + dy[k];
if (px < 0 || py < 0 || px >= w || py >= h || buf[py][px] == '0') {
state[i][j][k][l] = 1;
moveTo[i][j][k][l] = make_pair(cy, cx);
} else {
moveTo[i][j][k][l] = make_pair(py, px);
}
}
int cx = 0, cy = 0, dir = 0, hand = 0;
for (int hoge = 0; hoge < n; ++hoge) {
pair<int, int> nxt = moveTo[cy][cx][dir][hand];
int flag = state[cy][cx][dir][hand];
cy = nxt.first;
cx = nxt.second;
if (flag) {
if (hand == 0)
hand = 1;
else {
hand = 0;
dir = (dir + 1) % 4;
}
}
}
printf("%c\n", buf[cy][cx]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 200;
const int N = 100200;
const int kind = 26;
const int inf = 1 << 29;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double sq2 = sqrt(2.0);
bool cal[60][60][4][2];
bool poss[60][60][4][2];
pair<int, int> go[60][60][4][2];
int mp[60][60];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string s;
int n, m;
int main() {
while (scanf("%d%d", &m, &n) == 2) {
memset(cal, 0, sizeof(cal));
memset(poss, 0, sizeof(poss));
for (int i = 0; i < m; i++) {
cin >> s;
mp[i + 1][0] = 0;
mp[i + 1][s.size() + 1] = 0;
for (int j = 0; j < s.size(); j++) {
mp[i + 1][j + 1] = s[j] - '0';
}
}
for (int i = 0; i < 60; i++) {
mp[0][i] = mp[m + 1][i] = 0;
}
pair<int, int> cur = make_pair(1, 1);
int dp = 0, cp = 0;
while (n--) {
pair<int, int> tmp = cur;
if (cal[cur.first][cur.second][dp][cp]) {
if (poss[cur.first][cur.second][dp][cp]) {
cur = go[cur.first][cur.second][dp][cp];
continue;
} else {
if (cp == 1) {
cp = 0;
dp = (dp + 1) % 4;
} else
cp = 1;
continue;
}
}
while (mp[cur.first + dx[dp]][cur.second + dy[dp]] ==
mp[tmp.first][tmp.second]) {
cur.first += dx[dp];
cur.second += dy[dp];
}
int ddx, ddy;
if (dp == 0)
ddx = -1, ddy = 0;
else if (dp == 1)
ddx = 0, ddy = 1;
else if (dp == 2)
ddx = 1, ddy = 0;
else
ddx = 0, ddy = -1;
if (cp == 1) {
ddx *= -1;
ddy *= -1;
}
while (mp[cur.first + ddx][cur.second + ddy] ==
mp[tmp.first][tmp.second]) {
cur.first += ddx;
cur.second += ddy;
}
cal[tmp.first][tmp.second][dp][cp] = 1;
if (mp[cur.first + dx[dp]][cur.second + dy[dp]] == 0) {
poss[tmp.first][tmp.second][dp][cp] = 0;
if (cp == 1) {
cp = 0;
dp = (dp + 1) % 4;
} else {
cp = 1;
}
continue;
} else {
poss[tmp.first][tmp.second][dp][cp] = 1;
cur.first += dx[dp];
cur.second += dy[dp];
go[tmp.first][tmp.second][dp][cp] = cur;
continue;
}
}
printf("%d\n", mp[cur.first][cur.second]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct state {
int i, j, dp, cp;
};
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
state n[50][50][4][2];
state current;
char b[50][51];
int r, c;
int moves;
void read() {
cin >> r >> moves;
for (int i = 0; i < r; i++) {
cin >> b[i];
}
c = strlen(b[0]);
}
void write() { cout << b[current.i][current.j] << "\n"; }
inline bool valid(int i, int j) {
return i > -1 && i < r && j > -1 && j < c && b[i][j] != '0';
}
void preprocess(int i, int j, int dp, int cp) {
int ni = i, nj = j;
while (valid(ni, nj) && b[ni][nj] == b[i][j]) {
ni += di[dp];
nj += dj[dp];
}
ni -= di[dp];
nj -= dj[dp];
int p = (dp + (cp ? +1 : -1) + 4) % 4;
while (valid(ni, nj) && b[ni][nj] == b[i][j]) {
ni += di[p];
nj += dj[p];
}
ni -= di[p];
nj -= dj[p];
ni += di[dp];
nj += dj[dp];
if (valid(ni, nj)) {
n[i][j][dp][cp].i = ni;
n[i][j][dp][cp].j = nj;
n[i][j][dp][cp].dp = dp;
n[i][j][dp][cp].cp = cp;
} else {
n[i][j][dp][cp].i = i;
n[i][j][dp][cp].j = j;
n[i][j][dp][cp].dp = !cp ? dp : (dp + 1) % 4;
n[i][j][dp][cp].cp = !cp;
}
}
void preprocess() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
for (int dp = 0; dp < 4; dp++) {
for (int cp = 0; cp < 2; cp++) {
preprocess(i, j, dp, cp);
}
}
}
}
}
void move() {
for (int i = 0; i < moves; i++) {
current = n[current.i][current.j][current.dp][current.cp];
}
}
int main() {
current.i = current.j = current.dp = current.cp = 0;
read();
preprocess();
move();
write();
return 0;
}
|
#include <bits/stdc++.h>
char field[55][55];
int n, steps, m;
int next[55][55][4][2][4];
int main() {
int i, j, k, dir, r;
scanf("%d %d", &n, &steps);
for (i = 0; i < n; i++) scanf("%s", field[i]);
m = strlen(field[0]);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
for (k = 0; k < 4; k++)
for (dir = 0; dir < 2; dir++)
for (r = 0; r < 4; r++) next[i][j][k][dir][r] = -1;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
if (field[i][j] != '0') {
int left, right, top, bottom;
left = right = j;
top = bottom = i;
while (left > 0 && field[i][left - 1] == field[i][j]) left--;
while (right < m - 1 && field[i][right + 1] == field[i][j]) right++;
while (top > 0 && field[top - 1][j] == field[i][j]) top--;
while (bottom < n - 1 && field[bottom + 1][j] == field[i][j]) bottom++;
for (k = 0; k < 4; k++) {
for (dir = 0; dir < 2; dir++) {
int nx, ny;
if (k == 0) {
nx = top - 1;
ny = (1 - dir ? left : right);
} else if (k == 1) {
ny = right + 1;
nx = (1 - dir ? top : bottom);
} else if (k == 2) {
nx = bottom + 1;
ny = (1 - dir ? right : left);
} else {
ny = left - 1;
nx = (1 - dir ? bottom : top);
}
if (nx < 0 || ny < 0 || nx >= n || ny >= m ||
field[nx][ny] == '0') {
next[i][j][k][dir][0] = i;
next[i][j][k][dir][1] = j;
next[i][j][k][dir][2] = (k + dir) % 4;
next[i][j][k][dir][3] = 1 - dir;
continue;
}
next[i][j][k][dir][0] = nx;
next[i][j][k][dir][1] = ny;
next[i][j][k][dir][2] = k;
next[i][j][k][dir][3] = dir;
}
}
}
}
int cx = 0, cy = 0, ck = 1, cdir = 0;
for (i = 0; i < steps; i++) {
int tx, ty, tk, tdir;
tx = cx;
ty = cy;
tk = ck;
tdir = cdir;
cx = next[tx][ty][tk][tdir][0];
cy = next[tx][ty][tk][tdir][1];
ck = next[tx][ty][tk][tdir][2];
cdir = next[tx][ty][tk][tdir][3];
}
printf("%c\n", field[cx][cy]);
return 0;
}
|
#include <bits/stdc++.h>
typedef struct {
int x;
int y;
int dp;
int cp;
} point;
point p[50][50][4][2];
int main() {
int m, n, w, h, x, y, dp, cp, i, j, k, l;
char s[52][52];
int d1[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
int d2[4][2] = {1, 0, 0, -1, -1, 0, 0, 1};
for (i = 0; i <= 51; i++) {
for (j = 0; j <= 51; j++) {
s[i][j] = '0';
}
}
scanf("%d %d", &m, &n);
for (i = 1; i <= m; i++) scanf("%s", &s[i][1]);
w = m;
h = strlen(s[1]) - 1;
for (i = 1; i <= w; i++) s[i][h + 1] = '0';
for (i = 0; i < w; i++) {
for (j = 0; j < h; j++) {
if (s[i + 1][j + 1] == '0') continue;
for (k = 0; k < 4; k++) {
for (l = 0; l < 2; l++) {
x = i + 1;
y = j + 1;
while (s[x + d1[k][0]][y + d1[k][1]] == s[i + 1][j + 1]) {
x += d1[k][0];
y += d1[k][1];
}
while (s[x + d2[(k + l * 2) % 4][0]][y + d2[(k + l * 2) % 4][1]] ==
s[i + 1][j + 1]) {
x += d2[(k + l * 2) % 4][0];
y += d2[(k + l * 2) % 4][1];
}
x += d1[k][0];
y += d1[k][1];
if (s[x][y] == '0') {
p[i][j][k][l].x = i;
p[i][j][k][l].y = j;
if (l == 0) {
p[i][j][k][l].dp = k;
p[i][j][k][l].cp = 1;
} else {
p[i][j][k][l].dp = (k + 1) % 4;
p[i][j][k][l].cp = 0;
}
} else {
p[i][j][k][l].x = x - 1;
p[i][j][k][l].y = y - 1;
p[i][j][k][l].dp = k;
p[i][j][k][l].cp = l;
}
}
}
}
}
x = 0;
y = 0;
dp = 2;
cp = 0;
for (i = 0; i < n; i++) {
point tmp = p[x][y][dp][cp];
x = tmp.x;
y = tmp.y;
dp = tmp.dp;
cp = tmp.cp;
}
printf("%d\n", s[x + 1][y + 1] - '0');
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int n, m, cp, dp, t;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
char c[55][55];
pair<int, int> d[55][55][4], cur = make_pair(0, 0);
void inc() {
if (cp == 0)
cp = 1;
else
dp = (dp + 1) % 4, cp = 0;
}
void go() {
cur = d[cur.first][cur.second][dp];
int to = (dp + 3) % 4;
if (cp) to = (dp + 1) % 4;
cur = d[cur.first][cur.second][to];
int tox = cur.first + dx[dp], toy = cur.second + dy[dp];
if (tox < n && tox >= 0 && toy < m && toy >= 0 && c[tox][toy] != '0')
cur = make_pair(tox, toy);
else
inc();
}
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++) scanf("%s", c[i]);
m = strlen(c[0]);
for (int i = 0; i < n; i++)
for (int j = m - 1; j >= 0; j--)
if (j != m - 1 && c[i][j] == c[i][j + 1])
d[i][j][0] = d[i][j + 1][0];
else
d[i][j][0] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (j && c[i][j] == c[i][j - 1])
d[i][j][2] = d[i][j - 1][2];
else
d[i][j][2] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i && c[i][j] == c[i - 1][j])
d[i][j][3] = d[i - 1][j][3];
else
d[i][j][3] = make_pair(i, j);
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < m; j++)
if (i != n - 1 && c[i][j] == c[i + 1][j])
d[i][j][1] = d[i + 1][j][1];
else
d[i][j][1] = make_pair(i, j);
for (int i = 0; i < t; i++) go();
printf("%c\n", c[cur.first][cur.second]);
return 0;
}
|
#include <bits/stdc++.h>
struct ip_t {
int x, y;
int dir1, dir2;
} ip;
char in[80][80];
int m, n, w;
int tops[80][80];
int lefts[80][80];
int rights[80][80];
int bottoms[80][80];
int ndir1[4][4] = {{4, 1, 4, 0}, {1, 4, 2, 4}, {4, 2, 4, 3}, {0, 4, 3, 4}};
int ndir2[4][4] = {{4, 0, 4, 1}, {2, 4, 1, 4}, {4, 3, 4, 2}, {3, 4, 0, 4}};
bool valid(int x, int y) {
if (x < 0 || x >= w) return false;
if (y < 0 || y >= m) return false;
if (in[y][x] == 0) return false;
return true;
}
int step() {
if (ip.dir2 == 0) ip.y = tops[ip.y][ip.x];
if (ip.dir2 == 1) ip.x = rights[ip.y][ip.x];
if (ip.dir2 == 2) ip.y = bottoms[ip.y][ip.x];
if (ip.dir2 == 3) ip.x = lefts[ip.y][ip.x];
int cx = ip.x, cy = ip.y;
if (ip.dir1 == 0) {
ip.y = tops[ip.y][ip.x];
cy = ip.y - 1;
}
if (ip.dir1 == 1) {
ip.x = rights[ip.y][ip.x];
cx = ip.x + 1;
}
if (ip.dir1 == 2) {
ip.y = bottoms[ip.y][ip.x];
cy = ip.y + 1;
}
if (ip.dir1 == 3) {
ip.x = lefts[ip.y][ip.x];
cx = ip.x - 1;
}
if (valid(cx, cy)) {
ip.x = cx;
ip.y = cy;
return 1;
} else {
int d1 = ip.dir1, d2 = ip.dir2;
ip.dir1 = ndir1[d1][d2];
ip.dir2 = ndir2[d1][d2];
return 0;
}
}
int main() {
scanf("%d%d", &m, &n);
char asdf;
scanf("%c", &asdf);
for (int i = 0; i < (m); ++i) {
gets(in[i]);
}
w = strlen(in[0]);
for (int i = 0; i < (m); ++i)
for (int j = 0; j < (w); ++j) in[i][j] -= '0';
for (int i = 0; i < (80); ++i) {
tops[0][i] = 0;
lefts[i][0] = 0;
rights[i][w - 1] = w - 1;
bottoms[m - 1][i] = m - 1;
}
for (int x = 0; x < (w); ++x) {
for (int i = (m - 2); i >= (0); --i) {
if (in[i][x] == in[i + 1][x])
bottoms[i][x] = bottoms[i + 1][x];
else
bottoms[i][x] = i;
}
for (int i = (1); i <= (m - 1); ++i) {
if (in[i][x] == in[i - 1][x])
tops[i][x] = tops[i - 1][x];
else
tops[i][x] = i;
}
}
for (int y = 0; y < (m); ++y) {
for (int i = (w - 2); i >= (0); --i) {
if (in[y][i] == in[y][i + 1])
rights[y][i] = rights[y][i + 1];
else
rights[y][i] = i;
}
for (int i = (1); i <= (w - 1); ++i) {
if (in[y][i] == in[y][i - 1])
lefts[y][i] = lefts[y][i - 1];
else
lefts[y][i] = i;
}
}
ip.x = ip.y = 0;
ip.dir1 = 1;
ip.dir2 = 0;
while (n) {
step();
--n;
}
printf("%d\n", (int)in[ip.y][ip.x]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
short INF = 51;
struct block {
short xl, yl, xr, yr;
short col;
block(short a, short b, short c, short d, short f) {
xl = a;
yl = b;
xr = c;
yr = d;
col = f;
}
block() {
xl = -1;
yl = -1;
xr = INF;
yr = INF;
col = -1;
}
};
int main() {
short n, n2;
int m;
cin >> n >> m;
vector<string> tab(n);
for (short i = 0; i < n; i++) {
cin >> tab[i];
}
n2 = tab[0].size();
vector<vector<short> > was(n + 2, vector<short>(n2 + 2, -1));
vector<block> vec;
short coun = -1;
for (short i = 0; i < n; i++) {
for (short j = 0; j < n2; j++) {
if (was[i + 1][j + 1] == -1) {
coun++;
short maxx = 0, maxy = 0;
queue<pair<short, short> > q;
q.push({i, j});
while (!q.empty()) {
short x = q.front().first;
short y = q.front().second;
q.pop();
if (was[x + 1][y + 1] != -1) {
continue;
}
was[x + 1][y + 1] = coun;
if (y + 1 < n2) {
if (tab[x][y] == tab[x][y + 1]) {
q.push({x, y + 1});
} else {
maxy = y;
}
} else {
maxy = y;
}
if (x + 1 < n) {
if (tab[x + 1][y] == tab[x][y]) {
q.push({x + 1, y});
} else {
maxx = x;
}
} else {
maxx = x;
}
}
vec.push_back(block(i + 1, j + 1, maxx + 1, maxy + 1, tab[i][j] - '0'));
}
}
}
vec.push_back(block(-1, -1, -1, -1, 0));
coun++;
for (short i = 0; i <= n + 1; i++) {
was[i][0] = coun;
was[i][n2 + 1] = coun;
}
for (short i = 0; i <= n2 + 1; i++) {
was[n + 1][i] = coun;
was[0][i] = coun;
}
short dp = 0, cp = 1, bp = 0;
vector<vector<short> > ans(coun, vector<short>(8, -1));
bool w = true;
for (int i = 0; i < m; i++) {
if (w) {
if (dp == 4 && cp == 1 && ans[bp][0] != -1) {
w = false;
m %= i - ans[bp][0];
i = 0;
}
w = false;
if (dp == 4 && cp == 0 && ans[bp][1] != -1) {
w = false;
m %= i - ans[bp][1];
i = 0;
}
if (dp == 0 && cp == 1 && ans[bp][2] != -1) {
w = false;
m %= i - ans[bp][2];
i = 0;
}
if (dp == 0 && cp == 0 && ans[bp][3] != -1) {
w = false;
m %= i - ans[bp][3];
i = 0;
}
if (dp == 1 && cp == 1 && ans[bp][4] != -1) {
w = false;
m %= i - ans[bp][4];
i = 0;
}
if (dp == 1 && cp == 0 && ans[bp][5] != -1) {
w = false;
m %= i - ans[bp][5];
i = 0;
}
if (dp == 2 && cp == 1 && ans[bp][6] != -1) {
w = false;
m %= i - ans[bp][6];
i = 0;
}
if (dp == 2 && cp == 0 && ans[bp][7] != -1) {
w = false;
m %= i - ans[bp][7];
i = 0;
}
}
if (dp == 4 && cp == 1) {
ans[bp][0] = i;
if (vec[was[vec[bp].xl - 1][vec[bp].yl]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xl - 1][vec[bp].yl];
}
continue;
}
if (dp == 4 && cp == 0) {
ans[bp][1] = i;
if (vec[was[vec[bp].xl - 1][vec[bp].yr]].col == 0) {
dp = 0;
cp = 1;
} else {
bp = was[vec[bp].xl - 1][vec[bp].yr];
}
continue;
}
if (dp == 0 && cp == 1) {
ans[bp][2] = i;
if (vec[was[vec[bp].xl][vec[bp].yr + 1]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xl][vec[bp].yr + 1];
}
continue;
}
if (dp == 0 && cp == 0) {
ans[bp][3] = i;
if (vec[was[vec[bp].xr][vec[bp].yr + 1]].col == 0) {
dp = 1;
cp = 1;
} else {
bp = was[vec[bp].xr][vec[bp].yr + 1];
}
continue;
}
if (dp == 1 && cp == 1) {
ans[bp][4] = i;
if (vec[was[vec[bp].xr + 1][vec[bp].yr]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xr + 1][vec[bp].yr];
}
continue;
}
if (dp == 1 && cp == 0) {
ans[bp][5] = i;
if (vec[was[vec[bp].xr + 1][vec[bp].yl]].col == 0) {
dp = 2;
cp = 1;
} else {
bp = was[vec[bp].xr + 1][vec[bp].yl];
}
continue;
}
if (dp == 2 && cp == 1) {
ans[bp][6] = i;
if (vec[was[vec[bp].xr][vec[bp].yl - 1]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xr][vec[bp].yl - 1];
}
continue;
}
if (dp == 2 && cp == 0) {
ans[bp][7] = i;
if (vec[was[vec[bp].xl][vec[bp].yl - 1]].col == 0) {
dp = 4;
cp = 1;
} else {
bp = was[vec[bp].xl][vec[bp].yl - 1];
}
continue;
}
}
cout << vec[bp].col;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
point(int xv = 0, int yv = 0) {
x = xv;
y = yv;
}
} edge[55][55][4];
char s[55];
int gr[55][55];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int lr[4][4] = {{0, 2, 0, 1}, {1, 0, 2, 0}, {0, 1, 0, 2}, {2, 0, 1, 0}};
int co[4][4] = {{4, 1, 4, 0}, {1, 4, 2, 4}, {4, 2, 4, 3}, {0, 4, 3, 4}};
int main() {
int n, k, i, j, cp, dp, gx, gy, m, K;
point d[4];
point bp(1, 1);
scanf("%d%d", &n, &K);
memset(gr, 0, sizeof(gr));
for (i = 1; i <= n; i++) {
scanf("%s", s);
m = strlen(s);
for (j = 1; j <= m; j++) gr[i][j] = s[j - 1] - '0';
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
for (k = 0; k < 4; k++) {
d[k].x = i;
d[k].y = j;
while (gr[d[k].x + dir[k][0]][d[k].y + dir[k][1]] == gr[d[k].x][d[k].y])
d[k].x += dir[k][0], d[k].y += dir[k][1];
}
edge[i][j][0].x = edge[i][j][1].x = d[0].x;
edge[i][j][1].y = edge[i][j][2].y = d[1].y;
edge[i][j][2].x = edge[i][j][3].x = d[2].x;
edge[i][j][3].y = edge[i][j][0].y = d[3].y;
}
}
cp = 0;
dp = 1;
while (K--) {
gx = edge[bp.x][bp.y][co[dp][cp]].x;
gy = edge[bp.x][bp.y][co[dp][cp]].y;
gx = gx + dir[dp][0];
gy = gy + dir[dp][1];
if (gr[gx][gy] == 0) {
if (lr[dp][cp] == 1) {
cp = (dp + 1) % 4;
} else {
dp = (dp + 1) % 4;
cp = (dp - 1 + 4) % 4;
}
} else {
bp.x = gx;
bp.y = gy;
}
}
printf("%d\n", gr[bp.x][bp.y]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
enum { UP = 0, RIGHT, DOWN, LEFT, DIRS };
struct BLOCK {
int r, c;
int w, h;
char col;
void edge(int dir, int& new_r, int& new_c) {
dir %= DIRS;
if (dir == UP) {
new_r = r;
} else if (dir == LEFT) {
new_c = c;
} else if (dir == RIGHT) {
new_c = c + w - 1;
} else if (dir == DOWN) {
new_r = r + h - 1;
} else {
throw "Bad direction!";
}
}
};
void move(int r, int c, int dir, int& new_r, int& new_c) {
new_r = r;
new_c = c;
if (dir == UP)
new_r--;
else if (dir == DOWN)
new_r++;
else if (dir == RIGHT)
new_c++;
else if (dir == LEFT)
new_c--;
else
throw "BAD DIR!";
}
struct PTR {
int bl;
int dir;
int rot;
void fix() {
if ((rot % DIRS) == LEFT) {
rot = RIGHT;
} else if ((rot % DIRS) == RIGHT) {
rot = LEFT;
dir++;
dir %= DIRS;
}
}
} my_ptr;
string G[70];
int comp[70][70];
vector<BLOCK> block;
bool invalid(int r, int c, int M, int K) {
if (r < 0) return true;
if (c < 0) return true;
if (r >= M) return true;
if (c >= K) return true;
return false;
}
void flood_fill(int r, int c, int b, int M, int K) {
if (invalid(r, c, M, K)) return;
if (comp[r][c] != (-1)) return;
if (G[r][c] != block[b].col) return;
comp[r][c] = b;
int new_w = c - block[b].c + 1;
int new_h = r - block[b].r + 1;
block[b].w = max(block[b].w, new_w);
block[b].h = max(block[b].h, new_h);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int x = i;
int y = j;
if (x < 0) x *= -1;
if (y < 0) y *= -1;
if ((x + y) != 1) continue;
flood_fill(r + i, c + j, b, M, K);
}
}
}
string s(int d) {
if (d < 0) d += DIRS;
if (d == UP) return "up";
if (d == RIGHT) return "right";
if (d == LEFT) return "left";
if (d == DOWN) return "down";
throw "wat!?";
}
int main() {
int M, N;
cin >> M >> N;
cin.ignore();
for (int r = 0; r < M; r++) {
getline(cin, G[r]);
}
int K = G[0].size();
for (int r = 0; r < M; r++) {
for (int c = 0; c < K; c++) {
comp[r][c] = (-1);
}
}
for (int r = 0; r < M; r++) {
for (int c = 0; c < K; c++) {
if (G[r][c] == '0') continue;
if (comp[r][c] == (-1)) {
BLOCK nb;
nb.r = r;
nb.c = c;
nb.w = 1;
nb.h = 1;
nb.col = G[r][c];
block.push_back(nb);
flood_fill(r, c, block.size() - 1, M, K);
}
}
}
my_ptr.bl = 0;
my_ptr.dir = RIGHT;
my_ptr.rot = LEFT;
for (int i = 0; i < N; i++) {
int r, c;
block[my_ptr.bl].edge(my_ptr.dir, r, c);
block[my_ptr.bl].edge((my_ptr.dir + my_ptr.rot) % DIRS, r, c);
int rp, cp;
move(r, c, my_ptr.dir, rp, cp);
if ((invalid(rp, cp, M, K)) || (comp[rp][cp] == (-1))) {
my_ptr.fix();
} else {
my_ptr.bl = comp[rp][cp];
}
}
cout << block[my_ptr.bl].col << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct pkt {
int x, y;
};
const int INF = 1000000000;
int main() {
ios_base::sync_with_stdio(0);
int n, m, k;
cin >> n >> k;
vector<string> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
m = a[0].size();
pkt f[50][50][4][2];
vector<vector<bool> > odw(n, vector<bool>(m, false));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!odw[i][j] && a[i][j] != '0') {
int minix = i, maxix = i;
int miniy = j, maxiy = j;
while (maxix < n && a[maxix][j] == a[i][j]) ++maxix;
--maxix;
while (maxiy < m && a[i][maxiy] == a[i][j]) ++maxiy;
--maxiy;
for (int l = minix; l <= maxix; ++l) {
for (int o = miniy; o <= maxiy; ++o) {
odw[l][o] = true;
f[l][o][0][0] = {minix - 1, maxiy};
f[l][o][0][1] = {minix - 1, miniy};
f[l][o][1][0] = {maxix, maxiy + 1};
f[l][o][1][1] = {minix, maxiy + 1};
f[l][o][2][0] = {maxix + 1, miniy};
f[l][o][2][1] = {maxix + 1, maxiy};
f[l][o][3][0] = {minix, miniy - 1};
f[l][o][3][1] = {maxix, miniy - 1};
}
}
}
}
}
pkt akt = {0, 0};
int kier1 = 1, kier2 = 1;
for (int i = 0; i < k; ++i) {
pkt nast = f[akt.x][akt.y][kier1][kier2];
if (nast.x >= 0 && nast.x < n && nast.y >= 0 && nast.y < m &&
a[nast.x][nast.y] != '0')
akt = nast;
else {
if (kier2 == 1)
kier2 = 0;
else {
kier2 = 1;
kier1 = (kier1 + 1) % 4;
}
}
}
cout << a[akt.x][akt.y];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, d[5][3][100][100];
char a[100][100];
bool was[100][100];
string s;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s;
m = s.length();
for (int j = 0; j < m; j++) a[i][j] = s[j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!was[i][j]) {
int ii = i, jj = j;
while (ii < n && a[ii][j] == a[i][j]) ii++;
ii--;
while (jj < m && a[i][jj] == a[i][j]) jj++;
jj--;
for (int g = i; g <= ii; g++)
for (int h = j; h <= jj; h++) {
was[g][h] = true;
d[1][0][g][h] = ii * 100 + jj;
d[1][1][g][h] = i * 100 + jj;
d[2][0][g][h] = ii * 100 + j;
d[2][1][g][h] = ii * 100 + jj;
d[3][0][g][h] = i * 100 + j;
d[3][1][g][h] = ii * 100 + j;
d[4][0][g][h] = i * 100 + jj;
d[4][1][g][h] = i * 100 + j;
}
}
int in = 0, jn = 0, cp = 1, dp = 1, iin, jjn;
while (k) {
bool flag = false;
k--;
iin = in, jjn = jn;
in = d[dp][cp][iin][jjn] / 100;
jn = d[dp][cp][iin][jjn] % 100;
if (dp == 1 && jn < m - 1 && a[in][jn + 1] != '0')
jn++, flag = true;
else if (dp == 2 && in < n - 1 && a[in + 1][jn] != '0')
in++, flag = true;
else if (dp == 3 && jn > 0 && a[in][jn - 1] != '0')
jn--, flag = true;
else if (dp == 4 && in > 0 && a[in - 1][jn] != '0')
in--, flag = true;
if (!flag)
if (cp == 1)
cp = 0;
else
cp = 1, dp = dp % 4 + 1;
}
cout << a[in][jn];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct square {
int color;
bool used;
pair<int, int> m[2][2];
};
int n, m;
square t[51][51];
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && t[x][y].color;
}
int main() {
int k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
m = (int)s.length();
for (int j = 0; j < m; ++j) t[i][j].color = s[j] - '0';
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (!t[i][j].used) {
int x = i, y = j, cur_color = t[i][j].color;
while (x < n && t[x][j].color == cur_color) ++x;
while (y < m && t[i][y].color == cur_color) ++y;
for (int ii = i; ii < x; ++ii)
for (int jj = j; jj < y; ++jj) {
t[ii][jj].m[0][0].first = i;
t[ii][jj].m[0][0].second = j;
t[ii][jj].m[0][1].first = i;
t[ii][jj].m[0][1].second = y - 1;
t[ii][jj].m[1][0].first = x - 1;
t[ii][jj].m[1][0].second = j;
t[ii][jj].m[1][1].first = x - 1;
t[ii][jj].m[1][1].second = y - 1;
t[ii][jj].used = 1;
}
}
int dp = 1, cp = 0, x = 0, y = 0;
for (int step = 0; step < k; ++step) {
int xx, yy;
if (!dp)
if (!cp) {
xx = t[x][y].m[0][0].first;
yy = t[x][y].m[0][0].second;
} else {
xx = t[x][y].m[0][1].first;
yy = t[x][y].m[0][1].second;
}
if (dp == 1)
if (!cp) {
xx = t[x][y].m[0][1].first;
yy = t[x][y].m[0][1].second;
} else {
xx = t[x][y].m[1][1].first;
yy = t[x][y].m[1][1].second;
}
if (dp == 2)
if (!cp) {
xx = t[x][y].m[1][1].first;
yy = t[x][y].m[1][1].second;
} else {
xx = t[x][y].m[1][0].first;
yy = t[x][y].m[1][0].second;
}
if (dp == 3)
if (!cp) {
xx = t[x][y].m[1][0].first;
yy = t[x][y].m[1][0].second;
} else {
xx = t[x][y].m[0][0].first;
yy = t[x][y].m[0][0].second;
}
if (!dp) {
if (check(xx - 1, yy)) {
x = xx - 1;
y = yy;
} else if (!cp)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
continue;
}
if (dp == 1) {
if (check(xx, yy + 1)) {
x = xx;
y = yy + 1;
} else if (!cp)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
continue;
}
if (dp == 2) {
if (check(xx + 1, yy)) {
x = xx + 1;
y = yy;
} else if (!cp)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
continue;
}
if (dp == 3) {
if (check(xx, yy - 1)) {
x = xx;
y = yy - 1;
} else if (!cp)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
continue;
}
}
cout << t[x][y].color;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 50 + 10;
char board[M][M];
int dp[4][M][M];
int main() {
int n, m;
cin >> m >> n;
int COL = -1;
for (int i = 0; i < m; i++) {
string s;
cin >> s;
COL = s.length();
for (int j = 0; j < COL; j++) {
board[i + 1][j + 1] = s[j];
}
}
for (int row = 1; row <= m; row++) {
dp[0][row][COL] = COL;
for (int col = COL - 1; col >= 1; col--) {
dp[0][row][col] =
(board[row][col] == board[row][col + 1] ? dp[0][row][col + 1] : col);
}
dp[1][row][1] = 1;
for (int col = 2; col <= COL; col++) {
dp[1][row][col] =
(board[row][col] == board[row][col - 1] ? dp[1][row][col - 1] : col);
}
}
for (int col = 1; col <= COL; col++) {
dp[2][1][col] = 1;
for (int row = 2; row <= m; row++) {
dp[2][row][col] =
(board[row][col] == board[row - 1][col] ? dp[2][row - 1][col] : row);
}
dp[3][m][col] = m;
for (int row = m - 1; row >= 1; row--) {
dp[3][row][col] =
(board[row][col] == board[row + 1][col] ? dp[3][row + 1][col] : row);
}
}
int cnt = 1;
int BProw = 1;
int BPcol = 1;
int DP = 0;
int CP = 2;
while (cnt <= n) {
if (DP == 0 || DP == 1) {
BPcol = dp[DP][BProw][BPcol];
BProw = dp[CP][BProw][BPcol];
} else {
BProw = dp[DP][BProw][BPcol];
BPcol = dp[CP][BProw][BPcol];
}
if (DP == 0 || DP == 1) {
int step = 1 - 2 * DP;
if (BPcol + step >= 1 && BPcol + step <= COL &&
board[BProw][BPcol + step] != '0') {
BPcol = BPcol + step;
cnt++;
} else {
if (DP == 0) {
if (CP == 2) {
CP = 3;
cnt++;
} else {
DP = 3;
CP = 0;
cnt++;
}
} else {
if (CP == 3) {
CP = 2;
cnt++;
} else {
DP = 2;
CP = 1;
cnt++;
}
}
}
} else {
int step = 2 * (DP - 2) - 1;
if (BProw + step >= 1 && BProw + step <= m &&
board[BProw + step][BPcol] != '0') {
BProw = BProw + step;
cnt++;
} else {
if (DP == 2) {
if (CP == 1) {
CP = 0;
cnt++;
} else {
DP = 0;
CP = 2;
cnt++;
}
} else {
if (CP == 0) {
CP = 1;
cnt++;
} else {
DP = 1;
CP = 3;
cnt++;
}
}
}
}
}
cout << board[BProw][BPcol] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const bool online_judge = true;
const long long inf = 1LL << 60;
long long toInt(string s) {
long long res;
stringstream ss;
ss << s;
ss >> res;
return res;
}
string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
long long w, h;
vector<string> f;
typedef struct {
long long x;
long long y;
long long dp;
long long cp;
} state;
state memo[55][55][5][5];
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {-1, 0, 1, 0};
long long ex[4] = {-1, 0, 1, 0};
long long ey[4] = {0, -1, 0, 1};
state solve(long long y, long long x, long long dp, long long cp) {
if (memo[y][x][dp][cp].x != -1) return memo[y][x][dp][cp];
state ans;
long long nx = x + dx[dp];
long long ny = y + dy[dp];
long long mx = x + ex[(dp + (cp ? 2 : 0)) % 4];
long long my = y + ey[(dp + (cp ? 2 : 0)) % 4];
if (f[ny][nx] == f[y][x]) {
ans = solve(ny, nx, dp, cp);
} else if (f[my][mx] == f[y][x]) {
ans = solve(my, mx, dp, cp);
} else {
char c = f[ny][nx];
if (c == '0') {
if (cp == 0) {
ans.y = y;
ans.x = x;
ans.dp = dp;
ans.cp = 1;
} else {
ans.y = y;
ans.x = x;
ans.dp = (dp + 1) % 4;
ans.cp = 0;
}
} else {
ans.x = nx;
ans.y = ny;
ans.dp = dp;
ans.cp = cp;
}
}
memo[y][x][dp][cp] = ans;
return ans;
}
void run() {
long long m, n;
cin >> m >> n;
for (long long _ = 0; _ < m; _++) {
string tmp;
cin >> tmp;
f.push_back(tmp);
}
w = f[0].size();
h = m;
for (long long i = 0; i < m; i++) {
f[i] = "0" + f[i] + "0";
}
f.insert(f.begin(), string(w + 2, '0'));
f.push_back(string(w + 2, '0'));
for (long long i = 1; i <= h; i++) {
for (long long j = 1; j <= w; j++) {
for (long long dp = 0; dp <= 3; dp++) {
for (long long cp = 0; cp <= 1; cp++) {
memo[i][j][dp][cp].x = -1;
}
}
}
}
for (long long i = 1; i <= h; i++) {
for (long long j = 1; j <= w; j++) {
for (long long dp = 0; dp <= 3; dp++) {
for (long long cp = 0; cp <= 1; cp++) {
solve(i, j, dp, cp);
}
}
}
}
long long x = 1, y = 1, dp = 1, cp = 0;
while (n--) {
state next = memo[y][x][dp][cp];
x = next.x;
y = next.y;
dp = next.dp;
cp = next.cp;
}
cout << f[y][x] << endl;
}
int main(int argc, char *argv[]) {
run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 5;
const int MOD = 1000 * 1000 * 1000 + 7;
const long double EPS = 1e-9;
const int N = 55;
const int DX[] = {0, 1, 0, -1};
const int DY[] = {1, 0, -1, 0};
int n, m, k;
int a[N][N];
pair<pair<int, int>, pair<int, int> > f[N][N][4][2];
int g[N][N][4][2];
bool good(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
void next(int &x, int &y, int &u, int &v) {
int nx = f[x][y][u][v].first.first;
int ny = f[x][y][u][v].first.second;
int nu = f[x][y][u][v].second.first;
int nv = f[x][y][u][v].second.second;
x = nx, y = ny, u = nu, v = nv;
}
void calc(int &pre, int &len) {
int x = 0, y = 0, u = 0, v = 0;
memset(g, 255, sizeof g);
int c = 0;
while (g[x][y][u][v] == -1) {
g[x][y][u][v] = c++;
next(x, y, u, v);
}
pre = g[x][y][u][v];
len = c - g[x][y][u][v];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
m = (int)s.size();
for (int j = 0; j < m; ++j) {
a[i][j] = s[j] - '0';
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == 0) {
continue;
}
for (int u = 0; u < 4; ++u) {
for (int v = 0; v < 2; ++v) {
int x = i, y = j;
while (good(x + DX[u], y + DY[u]) &&
a[x + DX[u]][y + DY[u]] == a[i][j]) {
x += DX[u], y += DY[u];
}
int uu = (u + (v == 0 ? 3 : 1)) % 4;
while (good(x + DX[uu], y + DY[uu]) &&
a[x + DX[uu]][y + DY[uu]] == a[i][j]) {
x += DX[uu], y += DY[uu];
}
int p = u, q = v;
if (good(x + DX[u], y + DY[u]) && a[x + DX[u]][y + DY[u]] != 0) {
x += DX[u], y += DY[u];
} else {
if (v == 0) {
q = 1;
} else {
p = (u + 1) % 4;
q = 0;
}
}
f[i][j][u][v] = make_pair(make_pair(x, y), make_pair(p, q));
}
}
}
}
int pre, len;
calc(pre, len);
if (k < pre) {
int x = 0, y = 0, u = 0, v = 0;
for (int i = 0; i < k; ++i, next(x, y, u, v))
;
cout << a[x][y] << '\n';
} else {
int l = (k - pre) % len;
int x = 0, y = 0, u = 0, v = 0;
for (int i = 0; i < pre + l; ++i, next(x, y, u, v))
;
cout << a[x][y] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
enum Kierunki { GORA, PRAWO, DOL, LEWO };
const int SIZE = 55;
pair<int, int> grupa[SIZE * SIZE][10][10];
char plansza[SIZE][SIZE];
int odw[SIZE][SIZE];
int m_w, m_k;
void grupuj(int w, int k, int id) {
int max_w = m_w - 1, max_k = m_k - 1;
for (typeof(w) i = (w); i < (m_w); ++i) {
if (plansza[i][k] != plansza[w][k]) {
max_w = i - 1;
break;
}
for (typeof(k) j = (k); j < (m_k); ++j) {
if (plansza[i][j] != plansza[w][k]) {
max_k = j - 1;
break;
}
odw[i][j] = id;
}
}
grupa[id][GORA][LEWO].first = w - 1;
grupa[id][GORA][LEWO].second = k;
grupa[id][GORA][PRAWO].first = w - 1;
grupa[id][GORA][PRAWO].second = max_k;
grupa[id][DOL][LEWO].first = max_w + 1;
grupa[id][DOL][LEWO].second = max_k;
grupa[id][DOL][PRAWO].first = max_w + 1;
grupa[id][DOL][PRAWO].second = k;
grupa[id][LEWO][LEWO].first = max_w;
grupa[id][LEWO][LEWO].second = k - 1;
grupa[id][LEWO][PRAWO].first = w;
grupa[id][LEWO][PRAWO].second = k - 1;
grupa[id][PRAWO][LEWO].first = w;
grupa[id][PRAWO][LEWO].second = max_k + 1;
grupa[id][PRAWO][PRAWO].first = max_w;
grupa[id][PRAWO][PRAWO].second = max_k + 1;
}
void nastepny(int &bp, Kierunki &dp, Kierunki &cp) {
int w = grupa[bp][dp][cp].first;
int k = grupa[bp][dp][cp].second;
if (w < 0 || w >= m_w || k < 0 || k >= m_k || plansza[w][k] == '0') {
if (cp == LEWO)
cp = PRAWO;
else {
cp = LEWO;
switch (dp) {
case GORA:
dp = PRAWO;
break;
case PRAWO:
dp = DOL;
break;
case DOL:
dp = LEWO;
break;
case LEWO:
dp = GORA;
break;
}
}
} else {
bp = odw[w][k];
}
}
int main() {
int n;
scanf("%d %d", &m_w, &n);
for (typeof(0) i = (0); i < (m_w); ++i) scanf("%s", plansza[i]);
m_k = strlen(plansza[0]);
int id = 1;
for (typeof(0) i = (0); i < (m_w); ++i)
for (typeof(0) j = (0); j < (m_k); ++j)
if (!odw[i][j]) grupuj(i, j, id++);
int bp = 1;
Kierunki dp = PRAWO;
Kierunki cp = LEWO;
for (typeof(0) i = (0); i < (n); ++i) nastepny(bp, dp, cp);
int w = grupa[bp][GORA][LEWO].first + 1;
int k = grupa[bp][GORA][LEWO].second;
printf("%c\n", plansza[w][k]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int n, m, cp, dp, t;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
char c[55][55];
pair<int, int> d[55][55][4], cur = make_pair(0, 0);
void inc() {
if (cp == 0)
cp = 1;
else
dp = (dp + 1) % 4, cp = 0;
}
void go() {
cur = d[cur.first][cur.second][dp];
int to = (dp + 3) % 4;
if (cp) to = (dp + 1) % 4;
cur = d[cur.first][cur.second][to];
int tox = cur.first + dx[dp], toy = cur.second + dy[dp];
if (tox < n && tox >= 0 && toy < m && toy >= 0 && c[tox][toy] != '0')
cur = make_pair(tox, toy);
else
inc();
}
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++) scanf("%s", c[i]);
m = strlen(c[0]);
for (int i = 0; i < n; i++)
for (int j = m - 1; j >= 0; j--)
if (j != m - 1 && c[i][j] == c[i][j + 1])
d[i][j][0] = d[i][j + 1][0];
else
d[i][j][0] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (j && c[i][j] == c[i][j - 1])
d[i][j][2] = d[i][j - 1][2];
else
d[i][j][2] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i && c[i][j] == c[i - 1][j])
d[i][j][3] = d[i - 1][j][3];
else
d[i][j][3] = make_pair(i, j);
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < m; j++)
if (i != n - 1 && c[i][j] == c[i + 1][j])
d[i][j][1] = d[i + 1][j][1];
else
d[i][j][1] = make_pair(i, j);
for (int i = 0; i < t; i++) go();
printf("%c\n", c[cur.first][cur.second]);
return 0;
}
|
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::list;
using std::map;
using std::string;
using std::vector;
const int SIZE = 100;
int a[SIZE][SIZE];
int main() {
int n, m, x = 1, y = 1, dp = 1, cp = 0;
cin >> m >> n;
for (size_t k = 1; k <= m; k++) {
string s;
cin >> s;
for (size_t i = 1; i <= s.size(); i++) a[k][i] = s[i - 1] - 48;
}
for (size_t k = 0; k < n; k++) {
if (dp == 1 || dp == 2 && cp == 0 || dp == 0 && cp == 1)
while (a[y][x] == a[y][x + 1]) x++;
if (dp == 3 || dp == 2 && cp == 1 || dp == 0 && cp == 0)
while (a[y][x] == a[y][x - 1]) x--;
if (dp == 2 || dp == 1 && cp == 1 || dp == 3 && cp == 0)
while (a[y][x] == a[y + 1][x]) y++;
if (dp == 0 || dp == 1 && cp == 0 || dp == 3 && cp == 1)
while (a[y][x] == a[y - 1][x]) y--;
if (dp == 0) {
if (a[y - 1][x] != 0)
y--;
else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = 1;
}
}
} else if (dp == 1) {
if (a[y][x + 1] != 0)
x++;
else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = 2;
}
}
} else if (dp == 2) {
if (a[y + 1][x] != 0)
y++;
else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = 3;
}
}
} else if (dp == 3) {
if (a[y][x - 1] != 0)
x--;
else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = 0;
}
}
}
}
cout << a[y][x];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int col[50][50];
int mat[50][50][8];
void leggi() {
cin >> n >> p;
string s;
for (int i = 0; i < n; i += 1) {
cin >> s;
m = s.size();
for (int j = 0; j < m; j += 1) {
col[i][j] = s[j] - '0';
}
}
}
void elabora() {
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
if (col[i][j] != 0 && col[i][j] < 10) {
int h = 0;
for (int k = 0; i + k < n && col[i + k][j] == col[i][j]; k += 1) {
h += 1;
}
int w = 0;
for (int k = 0; j + k < m && col[i][j + k] == col[i][j]; k += 1) {
w += 1;
}
for (int a = i; a < i + h; a += 1) {
for (int b = j; b < j + w; b += 1) {
mat[a][b][0] = (((i) << 8) + (j + w));
mat[a][b][1] = (((i + h - 1) << 8) + (j + w));
mat[a][b][2] = (((i + h) << 8) + (j + w - 1));
mat[a][b][3] = (((i + h) << 8) + (j));
mat[a][b][4] = (((i + h - 1) << 8) + (j - 1));
mat[a][b][5] = (((i) << 8) + (j - 1));
mat[a][b][6] = (((i - 1) << 8) + (j));
mat[a][b][7] = (((i - 1) << 8) + (j + w - 1));
col[a][b] += 10;
}
}
}
}
}
int old_i = 0;
int old_j = 0;
int dir = 0;
for (int i = 0; i < p; i += 1) {
int new_i = ((mat[old_i][old_j][dir]) >> 8);
int new_j = ((mat[old_i][old_j][dir]) % 256);
if (0 <= new_i && new_i < n && 0 <= new_j && new_j < m &&
col[new_i][new_j] != 0) {
old_i = new_i;
old_j = new_j;
} else {
dir += 1;
dir %= 8;
}
}
cout << col[old_i][old_j] - 10 << endl;
}
int main() {
leggi();
elabora();
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxm = 100;
int m, n;
string s[maxm];
int get(int x, int y) {
if (x < 0 || y < 0 || x >= m || y >= s[0].length()) return 0;
return s[x][y] - '0';
}
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
int turn(int v, int t) {
if (t) return (v + 1) % 4;
return (v + 3) % 4;
}
int x, y, dp, cp;
int px[maxm][maxm][4][2];
int py[maxm][maxm][4][2];
void prepare(int x, int y, int dp, int cp) {
int c = get(x, y);
if (c == 0) {
px[x][y][dp][cp] = y;
py[x][y][dp][cp] = x;
return;
}
int ox = x, oy = y;
while (get(x + dx[dp], y + dy[dp]) == c) {
x += dx[dp];
y += dy[dp];
}
dp = turn(dp, cp);
while (get(x + dx[dp], y + dy[dp]) == c) {
x += dx[dp];
y += dy[dp];
}
px[ox][oy][dp][cp] = x;
py[ox][oy][dp][cp] = y;
}
void prepare() {
for (int i = 0; i < m; ++i)
for (int j = 0; j < s[0].length(); ++j)
for (int k = 0; k < 4; ++k) {
prepare(i, j, k, 0);
prepare(i, j, k, 1);
}
}
void move() {
if (get(px[x][y][dp][cp] + dx[dp], py[x][y][dp][cp] + dy[dp]) != 0) {
int ox = px[x][y][dp][cp] + dx[dp];
int oy = py[x][y][dp][cp] + dy[dp];
x = ox;
y = oy;
} else {
if (cp) {
cp = 0;
dp = turn(dp, 0);
} else
cp = 1;
}
}
int main() {
cin >> m >> n;
getline(cin, s[0]);
for (int i = 0; i < m; ++i) getline(cin, s[i]);
x = y = 0;
dp = 0;
cp = 0;
prepare();
for (int i = 0; i < n; ++i) {
move();
}
cout << get(x, y) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
class Solution {
int m, n;
int grid[52][52] = {0};
const int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
bool inside(int x, int y) {
return x >= 0 and y >= 0 and x < 50 and y < 50 and grid[x][y] > 0;
}
map<tuple<pair<int, int>, int, int>, int> vis;
vector<tuple<pair<int, int>, int, int>> st;
public:
void run() {
cin >> m >> n;
for (int i = 0; i < m; ++i) {
string s;
cin >> s;
for (int j = 0; j < s.size(); ++j) {
grid[i][j] = s[j] - '0';
}
}
pair<int, int> BP = {0, 0};
int DP = 0, CP = 1;
for (int i = 0; i < n; ++i) {
int x, y;
tie(x, y) = BP;
if (vis.count({{x, y}, DP, CP})) {
int len = i - vis[{{x, y}, DP, CP}];
int left = (n - i) % len;
int j = vis[{{x, y}, DP, CP}] + left;
tie(x, y) = get<0>(st[j]);
cout << grid[x][y] << '\n';
return;
}
vis[{{x, y}, DP, CP}] = i;
st.emplace_back(BP, DP, CP);
while (inside(x + dx[DP], y + dy[DP]) and
grid[x + dx[DP]][y + dy[DP]] == grid[x][y]) {
x += dx[DP], y += dy[DP];
}
while (inside(x + dx[CP], y + dy[CP]) and
grid[x + dx[CP]][y + dy[CP]] == grid[x][y]) {
x += dx[CP], y += dy[CP];
}
if (inside(x + dx[DP], y + dy[DP]) and grid[x + dx[DP]][y + dy[DP]]) {
x += dx[DP], y += dy[DP];
} else if (CP == ((1 + DP) & 3)) {
CP = (2 + CP) & 3;
} else {
DP = (3 + DP) & 3;
CP = (1 + DP) & 3;
}
BP = {x, y};
}
int x, y;
tie(x, y) = BP;
cout << grid[x][y] << '\n';
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Solution().run();
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, m, i, x, y, x2, y2, z, c, d, p;
int D[51][51][5][3];
char A[51][51];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) scanf("%s", A[i]);
m = strlen(A[0]);
x = y = 0;
d = 1;
c = 0;
memset(D, -1, sizeof D);
while (1) {
for (p = 0; p <= 1; p++)
while (1) {
x2 = x + dx[(d + p * (2 * c - 1) + 4) % 4];
y2 = y + dy[(d + p * (2 * c - 1) + 4) % 4];
if ((x2 >= 0 && x2 < n) && (y2 >= 0 && y2 < m) &&
A[x2][y2] == A[x][y]) {
x = x2;
y = y2;
} else
break;
}
if (D[x][y][d][c] != -1) {
k -= z;
k %= z - D[x][y][d][c];
z = 0;
memset(D, -1, sizeof D);
}
D[x][y][d][c] = z;
if (z == k) {
printf("%c\n", A[x][y]);
return 0;
}
x2 = x + dx[d];
y2 = y + dy[d];
if ((x2 >= 0 && x2 < n) && (y2 >= 0 && y2 < m) && A[x2][y2] != '0') {
x = x2;
y = y2;
} else {
if (c) d = (d + 1) % 4;
c = 1 - c;
}
z++;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char ady[55][55];
int CP[4][2] = {{3, 1}, {0, 2}, {3, 1}, {0, 2}};
struct Point {
Point() {}
int x, y;
Point(int xx, int yy) : x(xx), y(yy) {}
} memo[52][52][4][4];
int h, w;
void pre() {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
char color = ady[i][j];
for (int dp = 0; dp < 4; ++dp) {
int q = 1;
int nx, ny;
nx = i;
ny = j;
while (1) {
for (int cp = 0; cp < 4; ++cp) {
int k = 1, nxx, nyy;
nxx = nx;
nyy = ny;
while (1) {
nxx = k * dx[cp] + nx;
nyy = k * dy[cp] + ny;
if (nxx >= 0 && nyy >= 0 && nxx < h && nyy < w &&
color == ady[nxx][nyy] && ady[nxx][nyy] != '0') {
k++;
} else {
int xx = memo[i][j][dp][cp].x, yy = memo[i][j][dp][cp].y;
if (cp == 0 && xx >= (nxx - dx[cp])) {
memo[i][j][dp][cp].x = nxx - dx[cp];
memo[i][j][dp][cp].y = nyy - dy[cp];
}
if (cp == 2 && xx <= (nxx - dx[cp])) {
memo[i][j][dp][cp].x = nxx - dx[cp];
memo[i][j][dp][cp].y = nyy - dy[cp];
}
if (cp == 3 && yy >= (nyy - dy[cp])) {
memo[i][j][dp][cp].x = nxx - dx[cp];
memo[i][j][dp][cp].y = nyy - dy[cp];
}
if (cp == 1 && yy <= (nyy - dy[cp])) {
memo[i][j][dp][cp].x = nxx - dx[cp];
memo[i][j][dp][cp].y = nyy - dy[cp];
}
break;
}
}
}
nx = q * dx[dp] + i;
ny = q * dy[dp] + j;
if (nx >= 0 && ny >= 0 && nx < h && ny < w && color == ady[nx][ny] &&
ady[nx][ny] != '0') {
q++;
} else
break;
}
}
}
}
}
void init() {
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j)
for (int dp = 0; dp < 4; ++dp)
for (int cp = 0; cp < 4; ++cp)
memo[i][j][dp][cp].x = i, memo[i][j][dp][cp].y = j;
}
int main() {
int m, dp, cp, color;
char c;
cin >> h >> m;
for (int i = 0; i < h; ++i) cin >> ady[i];
w = strlen(ady[0]);
init();
pre();
dp = 1;
cp = 0;
c = ady[0][0];
int x, y, nx, ny, ax, ay, cp_fx, cp_fy;
x = y = 0;
int DP[55][55];
memset(DP, -1, sizeof(DP));
int i = 0;
for (; i < m; ++i) {
x = memo[x][y][dp][cp].x;
y = memo[x][y][dp][cp].y;
nx = x + dx[dp];
ny = y + dy[dp];
if (dp == 1 && (ny >= w || ady[nx][ny] == '0')) {
if (cp == 0)
cp = 2;
else {
dp = (dp + 1) % 4;
cp = 1;
}
} else if (dp == 2 && (nx >= h || ady[nx][ny] == '0')) {
if (cp == 1) {
cp = 3;
} else {
dp = (dp + 1) % 4;
cp = 2;
}
} else if (dp == 3 && (ny < 0 || ady[nx][ny] == '0')) {
if (cp == 2)
cp = 0;
else {
cp = 3;
dp = (dp + 1) % 4;
}
} else if (dp == 0 && (nx < 0 || ady[nx][ny] == '0')) {
if (cp == 3)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
} else {
x = nx;
y = ny;
c = ady[nx][ny];
}
}
cout << c << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char grid[60][60];
int conf[60][60][4][2];
map<int, pair<pair<int, int>, pair<int, int> > > mp;
int drow[] = {0, +1, 0, -1};
int dcol[] = {+1, 0, -1, 0};
int main() {
int m, n;
scanf("%d%d\n", &m, &n);
for (int i = 0; i < m; ++i) {
gets(grid[i]);
}
int nrow, ncol;
nrow = m;
ncol = strlen(grid[0]);
pair<pair<int, int>, pair<int, int> > head;
int looplen;
memset(conf, -1, sizeof conf);
int r = 0, c = 0, DP = 0, CP = 0;
int cnt = 0;
mp[cnt] = pair<pair<int, int>, pair<int, int> >(pair<int, int>(r, c),
pair<int, int>(DP, CP));
conf[r][c][DP][CP] = cnt++;
while (true) {
char color = grid[r][c];
while (true) {
int rr = r + drow[DP], cc = c + dcol[DP];
if (0 <= rr && rr < nrow && 0 <= cc && cc < ncol &&
grid[rr][cc] == color) {
r = rr;
c = cc;
} else {
break;
}
}
int x = DP;
if (CP == 0)
x = (x - 1 + 4) % 4;
else
x = (x + 1) % 4;
while (true) {
int rr = r + drow[x], cc = c + dcol[x];
if (0 <= rr && rr < nrow && 0 <= cc && cc < ncol &&
grid[rr][cc] == color) {
r = rr;
c = cc;
} else {
break;
}
}
int rr = r + drow[DP], cc = c + dcol[DP];
if (0 <= rr && rr < nrow && 0 <= cc && cc < ncol && grid[rr][cc] != '0') {
r = rr;
c = cc;
} else {
if (CP == 0) {
CP = 1 - CP;
} else {
DP = (DP + 1) % 4;
CP = 1 - CP;
}
}
if (conf[r][c][DP][CP] >= 0) {
head.first.first = r;
head.first.second = c;
head.second.first = DP;
head.second.second = CP;
looplen = cnt - conf[r][c][DP][CP];
break;
} else {
mp[cnt] = pair<pair<int, int>, pair<int, int> >(pair<int, int>(r, c),
pair<int, int>(DP, CP));
conf[r][c][DP][CP] = cnt++;
}
}
int idx = conf[head.first.first][head.first.second][head.second.first]
[head.second.second];
int index = (n - idx) % looplen + idx;
pair<pair<int, int>, pair<int, int> > last = mp[index];
int rlast = last.first.first, clast = last.first.second;
char ans[2] = {0};
ans[0] = grid[rlast][clast];
puts(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
bool calced[60][60][4][2];
bool possible[60][60][4][2];
pair<int, int> Go[60][60][4][2];
int field[60][60];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
memset(calced, 0, sizeof(calced));
memset(possible, 0, sizeof(possible));
int M, N;
string s;
scanf("%d%d", &M, &N);
for (int i = 0; i < M; i++) {
cin >> s;
field[i + 1][0] = 0;
field[i + 1][s.size() + 1] = 0;
for (int j = 0; j < s.size(); j++) field[i + 1][j + 1] = s[j] - '0';
}
for (int i = 0; i < 60; i++) field[0][i] = field[M + 1][i] = 0;
pair<int, int> current = make_pair(1, 1);
int dp = 0;
int cp = 0;
for (int z = 0; z < N; z++) {
pair<int, int> temp = current;
if (calced[current.first][current.second][dp][cp]) {
if (possible[current.first][current.second][dp][cp]) {
current = Go[current.first][current.second][dp][cp];
continue;
} else {
if (cp == 1) {
cp = 0;
dp = (dp + 1) % 4;
} else
cp = 1;
continue;
}
}
while (field[current.first + dx[dp]][current.second + dy[dp]] ==
field[temp.first][temp.second]) {
current.first += dx[dp];
current.second += dy[dp];
}
int ddx, ddy;
if (dp == 0)
ddx = -1, ddy = 0;
else if (dp == 1)
ddx = 0, ddy = 1;
else if (dp == 2)
ddx = 1, ddy = 0;
else
ddx = 0, ddy = -1;
if (cp == 1) {
ddx *= -1;
ddy *= -1;
}
while (field[current.first + ddx][current.second + ddy] ==
field[temp.first][temp.second]) {
current.first += ddx;
current.second += ddy;
}
calced[temp.first][temp.second][dp][cp] = true;
if (field[current.first + dx[dp]][current.second + dy[dp]] == 0) {
possible[temp.first][temp.second][dp][cp] = false;
if (cp == 1) {
cp = 0;
dp = (dp + 1) % 4;
} else
cp = 1;
continue;
} else {
possible[temp.first][temp.second][dp][cp] = true;
current.first += dx[dp];
current.second += dy[dp];
Go[temp.first][temp.second][dp][cp] = current;
continue;
}
}
printf("%d", field[current.first][current.second]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, baki, m;
string s[100];
int cnter, mni, mxi, mnj, mxj;
struct str {
int mni, mxi, mnj, mxj;
};
int mark[100][100];
str b[100100];
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
void dfs(int i, int j, int c) {
if (i >= n || j >= m || i < 0 || j < 0 || mark[i][j] || s[i][j] != c) return;
mark[i][j] = cnter;
mxi = max(mxi, i);
mni = min(mni, i);
mxj = max(mxj, j);
mnj = min(mnj, j);
for (int k = 0; k < 4; k++) {
int ni = i + di[k], nj = j + dj[k];
dfs(ni, nj, c);
}
}
void change(int curMark, pair<int, int> cur, int &i, int &j) {
if (cur.first == 0 && cur.second == 0 || cur.first == 3 && cur.second == 1) {
i = b[curMark].mni;
j = b[curMark].mxj;
} else if (cur.first == 0 && cur.second == 1 ||
cur.first == 1 && cur.second == 0) {
i = b[curMark].mxi;
j = b[curMark].mxj;
} else if (cur.first == 2 && cur.second == 0 ||
cur.first == 1 && cur.second == 1) {
i = b[curMark].mxi;
j = b[curMark].mnj;
} else {
i = b[curMark].mni;
j = b[curMark].mnj;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> baki;
for (int i = 0; i < n; i++) cin >> s[i];
m = s[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (!mark[i][j]) {
mni = 1e9;
mxi = 0;
mnj = 1e9;
mxj = 0;
cnter++;
dfs(i, j, s[i][j]);
b[cnter].mnj = mnj;
b[cnter].mxj = mxj;
b[cnter].mni = mni;
b[cnter].mxi = mxi;
}
}
int i = 0, j = 0;
pair<int, int> cur(0, 0);
int curMark = mark[0][0];
change(curMark, cur, i, j);
while (baki--) {
int ni = i + di[cur.first];
int nj = j + dj[cur.first];
if (ni >= n || nj >= m || ni < 0 || nj < 0 || s[ni][nj] == '0') {
cur.second++;
cur.second %= 2;
if (cur.second == 0) cur.first++;
(cur.first) %= 4;
int curMark = mark[i][j];
change(curMark, cur, i, j);
} else {
int curMark = mark[ni][nj];
change(curMark, cur, i, j);
}
}
cout << s[i][j] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, d[5][2][100][100];
char a[100][100];
bool was[100][100];
string s;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s;
m = s.length();
for (int j = 0; j < m; j++) {
a[i][j] = s[j];
if (a[i][j] == '0') was[i][j] = true;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!was[i][j]) {
int ii = i, jj = j;
while (ii < n && a[ii][j] == a[i][j]) ii++;
ii--;
while (jj < m && a[i][jj] == a[i][j]) jj++;
jj--;
for (int g = i; g <= ii; g++)
for (int h = j; h <= jj; h++) {
was[g][h] = true;
d[1][0][g][h] = ii * 100 + jj;
d[1][1][g][h] = i * 100 + jj;
d[2][0][g][h] = ii * 100 + j;
d[2][1][g][h] = ii * 100 + jj;
d[3][0][g][h] = i * 100 + j;
d[3][1][g][h] = ii * 100 + j;
d[4][0][g][h] = i * 100 + jj;
d[4][1][g][h] = i * 100 + j;
}
}
int in = 0, jn = 0, cp = 1, dp = 1, iin, jjn;
while (k) {
bool flag = false;
k--;
iin = in, jjn = jn;
in = d[dp][cp][iin][jjn] / 100;
jn = d[dp][cp][iin][jjn] % 100;
if (dp == 1 && jn < m - 1 && a[in][jn + 1] != '0')
jn++, flag = true;
else if (dp == 2 && in < n - 1 && a[in + 1][jn] != '0')
in++, flag = true;
else if (dp == 3 && jn > 0 && a[in][jn - 1] != '0')
jn--, flag = true;
else if (dp == 4 && in > 0 && a[in - 1][jn] != '0')
in--, flag = true;
if (!flag)
if (cp == 1)
cp = 0;
else
cp = 1, dp = dp % 4 + 1;
}
cout << a[in][jn];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
string mapa[55];
pair<int, int> pixelDir[55][55][5][3];
int movxDP[4] = {0, 1, 0, -1};
int movyDP[4] = {1, 0, -1, 0};
int visited[55][55][5][3];
int main() {
int m, n;
cin >> m >> n;
for (int i = 0; i < (int)(m); i++) {
cin >> mapa[i];
}
int mm = (mapa[0]).size();
for (int i = 0; i < (int)(m); i++)
for (int j = 0; j < (int)(mm); j++) {
for (int k = 0; k < (int)(4); k++) {
char color = mapa[i][j];
pair<int, int> punt = make_pair(i, j);
while (1) {
if (!(punt.first + movxDP[k] >= 0 && punt.first + movxDP[k] < m &&
punt.second + movyDP[k] >= 0 && punt.second + movyDP[k] < mm))
break;
if (mapa[punt.first + movxDP[k]][punt.second + movyDP[k]] != color)
break;
punt.first += movxDP[k];
punt.second += movyDP[k];
}
pair<int, int> puntIni = punt;
while (1) {
if (!(punt.first + movxDP[(k + 3) % 4] >= 0 &&
punt.first + movxDP[(k + 3) % 4] < m &&
punt.second + movyDP[(k + 3) % 4] >= 0 &&
punt.second + movyDP[(k + 3) % 4] < mm))
break;
if (mapa[punt.first + movxDP[(k + 3) % 4]]
[punt.second + movyDP[(k + 3) % 4]] != color)
break;
punt.first += movxDP[(k + 3) % 4];
punt.second += movyDP[(k + 3) % 4];
}
punt.first += movxDP[k];
punt.second += movyDP[k];
pixelDir[i][j][k][0] = punt;
punt = puntIni;
while (1) {
if (!(punt.first + movxDP[(k + 1) % 4] >= 0 &&
punt.first + movxDP[(k + 1) % 4] < m &&
punt.second + movyDP[(k + 1) % 4] >= 0 &&
punt.second + movyDP[(k + 1) % 4] < mm))
break;
if (mapa[punt.first + movxDP[(k + 1) % 4]]
[punt.second + movyDP[(k + 1) % 4]] != color)
break;
punt.first += movxDP[(k + 1) % 4];
punt.second += movyDP[(k + 1) % 4];
}
punt.first += movxDP[k];
punt.second += movyDP[k];
pixelDir[i][j][k][1] = punt;
}
}
pair<int, int> BP = make_pair(0, 0);
int DP = 0;
int CP = 0;
memset(visited, 0, sizeof(visited));
pair<int, int> tt, inicial(0, 0);
string lista = "";
int cont = 0;
for (int i = 0; i < (int)(n); i++) {
if (visited[BP.first][BP.second][DP][CP] == 1) {
lista += mapa[BP.first][BP.second];
} else if (visited[BP.first][BP.second][DP][CP] == 2) {
cont = i;
break;
}
visited[BP.first][BP.second][DP][CP]++;
tt = pixelDir[BP.first][BP.second][DP][CP];
bool cent = 1;
if (tt.first >= 0 && tt.first < m && tt.second >= 0 && tt.second < mm) {
if (mapa[tt.first][tt.second] != '0') {
cent = 0;
BP = tt;
}
}
if (cent) {
if (CP) DP = (DP + 1) % 4;
CP = 1 - CP;
}
}
if (cont == 0)
cout << mapa[BP.first][BP.second] << endl;
else
cout << lista[(n - cont) % (lista).size()] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int x, y;
string prog[50];
int iter[50 * 50][4][2];
int inci[4] = {0, -1, 0, 1};
int incj[4] = {1, 0, -1, 0};
void advance(int i, int j, int &ii, int &jj, int dir) {
char color = prog[i][j];
int iii = i, jjj = j;
while (true) {
iii += inci[dir];
jjj += incj[dir];
if (iii < 0 || iii >= y || jjj < 0 || jjj >= x || prog[iii][jjj] != color)
break;
i = iii;
j = jjj;
}
ii = i;
jj = j;
}
int main(int narg, char **arg) {
int n;
cin >> y >> n;
for (int i = 0; i < y; i++) cin >> prog[i];
x = prog[0].size();
memset(iter, 0xff, sizeof(iter));
int i = 0, j = 0;
int loop = 0, dp = 0, cp = 0, period = -1;
while (true) {
if (loop == n) {
cout << prog[i][j] << endl;
break;
}
int bp = i * x + j;
if (iter[bp][dp][cp] >= 0 && period < 0) {
period = loop - iter[bp][dp][cp];
n = loop + ((n - loop) % period);
if (loop == n) {
cout << prog[i][j] << endl;
break;
}
} else {
iter[bp][dp][cp] = loop;
}
int ii, jj;
advance(i, j, ii, jj, dp);
advance(ii, jj, ii, jj, cp ? (dp - 1) & 3 : (dp + 1) & 3);
i = ii;
j = jj;
ii = i + inci[dp];
jj = j + incj[dp];
if (ii < 0 || ii >= y || jj < 0 || jj >= x || prog[ii][jj] == '0') {
if (!cp)
cp = 1;
else {
cp = 0;
dp = (dp - 1) & 3;
}
} else {
i = ii;
j = jj;
}
loop++;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:268435456")
using namespace std;
template <class T>
T abs(T &x) {
return (x >= 0) ? (x) : (-x);
}
template <class T>
T sqr(T &x) {
return (x) * (x);
}
template <class T>
T min(T &a, T &b) {
return (a < b) ? (a) : (b);
}
template <class T>
T max(T &a, T &b) {
return (a > b) ? (a) : (b);
}
const int INF = 1000 * 1000 * 1000;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int leftCP = 0, rightCP = 1;
const int rightDP = 0, downDP = 1, leftDP = 2, upDP = 3;
int n, m, cnt;
int a[52][52];
int precDP[52][52];
int precCP[52][52];
int precX[52][52][4][2];
int precY[52][52][4][2];
inline bool good(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && a[x][y] > 0;
}
inline pair<int, int> get(int x, int y, int DP, int CP) {
return pair<int, int>(precX[x][y][DP][CP], precY[x][y][DP][CP]);
}
int main() {
cin >> n >> cnt;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
m = ((int)((s).size()));
for (int j = 0; j < m; j++) {
a[i + 1][j + 1] = s[j] - '0';
}
}
n += 2;
m += 2;
for (int i = 1; i < n - 1; i++)
for (int j = 1; j < m - 1; j++)
for (int DP = 0; DP < 4; DP++)
for (int CP = 0; CP < 2; CP++) {
int x = i;
int y = j;
int color = a[x][y];
while (a[x + dx[DP]][y + dy[DP]] == color) {
x += dx[DP];
y += dy[DP];
}
if (CP == leftCP) {
int newDP = (DP - 1 + 4) % 4;
while (a[x + dx[newDP]][y + dy[newDP]] == color) {
x += dx[newDP];
y += dy[newDP];
}
} else {
int newDP = (DP + 1) % 4;
while (a[x + dx[newDP]][y + dy[newDP]] == color) {
x += dx[newDP];
y += dy[newDP];
}
}
precX[i][j][DP][CP] = x;
precY[i][j][DP][CP] = y;
}
int x = 1, y = 1;
int DP = 0, CP = 0;
while (cnt--) {
pair<int, int> p = get(x, y, DP, CP);
int nx = p.first;
int ny = p.second;
if (good(nx + dx[DP], ny + dy[DP])) {
x = nx + dx[DP];
y = ny + dy[DP];
} else {
if (CP == leftCP) {
CP = rightCP;
} else {
CP = leftCP;
DP = (DP + 1) % 4;
}
}
}
cout << a[x][y] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, d[5][2][100][100];
char a[100][100];
bool was[100][100];
string s;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s;
m = s.length();
for (int j = 0; j < m; j++) a[i][j] = s[j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!was[i][j]) {
int ii = i, jj = j;
while (ii < n && a[ii][j] == a[i][j]) ii++;
ii--;
while (jj < m && a[i][jj] == a[i][j]) jj++;
jj--;
for (int g = i; g <= ii; g++)
for (int h = j; h <= jj; h++) {
was[g][h] = true;
d[1][0][g][h] = ii * 100 + jj;
d[1][1][g][h] = i * 100 + jj;
d[2][0][g][h] = ii * 100 + j;
d[2][1][g][h] = ii * 100 + jj;
d[3][0][g][h] = i * 100 + j;
d[3][1][g][h] = ii * 100 + j;
d[4][0][g][h] = i * 100 + jj;
d[4][1][g][h] = i * 100 + j;
}
}
int in = 0, jn = 0, cp = 1, dp = 1, iin, jjn;
while (k) {
bool flag = false;
k--;
iin = in, jjn = jn;
in = d[dp][cp][iin][jjn] / 100;
jn = d[dp][cp][iin][jjn] % 100;
if (dp == 1 && jn < m - 1 && a[in][jn + 1] != '0')
jn++, flag = true;
else if (dp == 2 && in < n - 1 && a[in + 1][jn] != '0')
in++, flag = true;
else if (dp == 3 && jn > 0 && a[in][jn - 1] != '0')
jn--, flag = true;
else if (dp == 4 && in > 0 && a[in - 1][jn] != '0')
in--, flag = true;
if (!flag)
if (cp == 1)
cp = 0;
else
cp = 1, dp = dp % 4 + 1;
}
cout << a[in][jn];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};
const int dxi[4][2] = {{0, 0}, {-1, 1}, {0, 0}, {1, -1}};
const int dxj[4][2] = {{-1, 1}, {0, 0}, {1, -1}, {0, 0}};
int Set[100][100], n, m, k, cc, u[3000][5][3];
string second[100];
void Dfs(int i, int j) {
if (Set[i][j]) return;
Set[i][j] = cc;
for (int k = 0; k < 4; ++k) {
int ii = i + di[k], jj = j + dj[k];
if (!(0 <= ii && ii < n && 0 <= jj && jj < m)) continue;
if (second[ii][jj] != second[i][j]) continue;
Dfs(ii, jj);
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> second[i];
}
m = second[0].size();
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (second[i][j] != '0' && !Set[i][j]) {
cc++;
Dfs(i, j);
}
int bp = Set[0][0];
int dp = 1;
int cp = 0;
int i = 0, j = 0;
int len = 0;
string path = "";
while (!u[bp][dp][cp]) {
u[bp][dp][cp] = ++len;
char cur = second[i][j];
while (true) {
int ii = i + di[dp];
int jj = j + dj[dp];
if (!(0 <= ii && ii < n && 0 <= jj && jj < m)) break;
if (second[ii][jj] != cur) break;
i = ii;
j = jj;
}
while (true) {
int ii = i + dxi[dp][cp];
int jj = j + dxj[dp][cp];
if (!(0 <= ii && ii < n && 0 <= jj && jj < m)) break;
if (second[ii][jj] != cur) break;
i = ii;
j = jj;
}
int ii = i + di[dp];
int jj = j + dj[dp];
if ((0 <= ii && ii < n && 0 <= jj && jj < m && second[ii][jj] != '0')) {
i = ii;
j = jj;
bp = Set[i][j];
} else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
}
path += second[i][j];
}
int cur = 0;
for (int i = 0; i < k - 1; ++i) {
cur++;
if (cur == path.size()) cur = u[bp][dp][cp] - 1;
}
cout << path[cur];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
class state {
public:
int x, y, dp, cp;
state() {
x = 0;
y = 0;
dp = 0;
cp = 3;
}
state(int _x, int _y, int _dp, int _cp) {
x = _x;
y = _y;
dp = _dp;
cp = _cp;
}
};
int n, m, k;
string a[60];
state z[60][60][4][4];
bool valid(int x, int y) {
if (x < 0 || y < 0 || x == n || y == m || a[x][y] == '0') return false;
return true;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
m = a[0].size();
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
for (int d = 0; d < 4; ++d) {
for (int it = 0; it < 2; ++it) {
int c = (d + 1 + it * 2) % 4;
int tekx = i, teky = j;
while (a[tekx][teky] == a[i][j]) {
tekx += dx[d];
teky += dy[d];
if (tekx == n || teky == m || tekx == -1 || teky == -1) break;
}
tekx -= dx[d];
teky -= dy[d];
while (a[tekx][teky] == a[i][j]) {
tekx += dx[c];
teky += dy[c];
if (tekx == n || teky == m || tekx == -1 || teky == -1) break;
}
tekx -= dx[c];
teky -= dy[c];
if (!valid(tekx + dx[d], teky + dy[d])) {
if (it == 1)
z[i][j][d][c] = state(i, j, d, (d + 1 + 0 * 2) % 4);
else
z[i][j][d][c] = state(i, j, (d + 1) % 4, ((d + 1) % 4 + 3) % 4);
} else {
z[i][j][d][c] = state(tekx + dx[d], teky + dy[d], d, c);
}
}
}
}
state tek;
for (int i = 0; i < k; ++i) {
tek = z[tek.x][tek.y][tek.dp][tek.cp];
}
cout << a[tek.x][tek.y] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int commands[55][55];
bool was[55][55];
int id[55][55];
int color[55 * 56];
pair<int, int> ul[55 * 56], dr[55 * 56];
int edge[55 * 56][8];
pair<int, int> calcUp(pair<int, int> a, pair<int, int> b) {
pair<int, int> c;
c.first = min(a.first, b.first);
c.second = min(a.second, b.second);
return c;
}
pair<int, int> calcDown(pair<int, int> a, pair<int, int> b) {
pair<int, int> c;
c.first = max(a.first, b.first);
c.second = max(a.second, b.second);
return c;
}
void dfs(const int i, const int j, const int counter, const int width,
const int height) {
if (was[i][j]) return;
if (commands[i][j] == 0) return;
was[i][j] = true;
id[i][j] = counter;
ul[counter] = calcUp(ul[counter], make_pair(i, j));
dr[counter] = calcDown(dr[counter], make_pair(i, j));
color[counter] = commands[i][j];
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy)
if (dx * dx + dy * dy == 1) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= height || y < 0 || y >= width) continue;
if (commands[i][j] != commands[x][y]) continue;
dfs(x, y, counter, width, height);
}
}
int main() {
int height, N, width;
cin >> height >> N;
memset((commands), (0), sizeof(commands));
for (int _n(height), i(0); i < _n; i++) {
string s;
cin >> s;
width = s.size();
for (int _n(width), j(0); j < _n; j++)
commands[i][j] = int(s[j]) - int('0');
}
memset((was), (0), sizeof(was));
memset((color), (0), sizeof(color));
for (int _n(55 * 56), i(0); i < _n; i++)
ul[i] = make_pair(1011111111, 1011111111);
for (int _n(55 * 56), i(0); i < _n; i++) dr[i] = make_pair(-1, -1);
int counter = 0;
memset((id), (0), sizeof(id));
for (int _n(height), i(0); i < _n; i++)
for (int _n(width), j(0); j < _n; j++)
if (!was[i][j] && commands[i][j] > 0) {
counter++;
dfs(i, j, counter, width, height);
}
for (int _n(55 * 56), i(0); i < _n; i++)
for (int _n(8), k(0); k < _n; k++) edge[i][k] = -1;
vector<int> mark(counter + 1, 0);
for (int _n(height), i(0); i < _n; i++)
for (int _n(width), j(0); j < _n; j++) {
if (commands[i][j] == 0) continue;
const int idx = id[i][j];
if (mark[idx]) continue;
mark[idx] = 1;
pair<int, int> links[] = {make_pair(ul[idx].first, dr[idx].second + 1),
make_pair(dr[idx].first, dr[idx].second + 1),
make_pair(dr[idx].first + 1, dr[idx].second),
make_pair(dr[idx].first + 1, ul[idx].second),
make_pair(dr[idx].first, ul[idx].second - 1),
make_pair(ul[idx].first, ul[idx].second - 1),
make_pair(ul[idx].first - 1, ul[idx].second),
make_pair(ul[idx].first - 1, dr[idx].second)};
for (int _n(8), k(0); k < _n; k++) {
int x = links[k].first, y = links[k].second;
if (x < 0 || x >= height || y < 0 || y >= width) continue;
if (commands[x][y] == 0) continue;
edge[idx][k] = id[x][y];
}
}
int bp = id[0][0];
int dir = 0;
for (int _n(N), iter(0); iter < _n; iter++) {
if (edge[bp][dir] != -1)
bp = edge[bp][dir];
else
dir = (dir + 1) % 8;
}
cout << color[bp] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string mp[52];
int c[52][52];
pair<int, int> p[52 * 52][4][2];
int vis[52][52][4][2];
vector<char> res;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
int n, nm;
cin >> n >> nm;
for (int i = 1; i <= n; i++) {
cin >> mp[i];
mp[i] = "0" + mp[i] + "0";
}
int sz = mp[1].size();
string s;
s.resize(sz, '0');
mp[0] = mp[n + 1] = s;
char col;
int k = 1, rs, cs;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sz; j++) {
if (!c[i][j] && mp[i][j] != '0') {
col = mp[i][j];
for (rs = j; rs <= sz; rs++) {
if (mp[i][rs] != col) break;
}
rs--;
p[k][2][1] = p[k][3][0] = make_pair(i, j);
p[k][0][0] = p[k][3][1] = make_pair(i, rs);
for (cs = i; cs <= n; cs++) {
if (mp[cs][j] == col) {
for (int jj = j; jj <= rs; jj++) {
c[cs][jj] = k;
}
} else
break;
}
cs--;
p[k][1][1] = p[k][2][0] = make_pair(cs, j);
p[k][0][1] = p[k][1][0] = make_pair(cs, rs);
k++;
}
}
}
int x, y, d, r, nx, ny;
x = y = 1;
d = r = 0;
k = 1;
while (!vis[x][y][d][r]) {
vis[x][y][d][r] = k++;
res.push_back(mp[x][y]);
x = p[c[x][y]][d][r].first;
y = p[c[x][y]][d][r].second;
nx = x + dx[d];
ny = y + dy[d];
if (mp[nx][ny] == '0') {
if (r == 0)
r = 1;
else {
r = 0;
d = (d + 1) % 4;
}
} else {
x = nx;
y = ny;
}
}
int nk = vis[x][y][d][r];
if (nm < nk - 1)
cout << res[nm] << endl;
else {
nm -= nk - 1;
cout << res[nm % (k - nk) + nk - 1] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct rectangle {
int r, c;
int w, h;
int color;
rectangle* rt;
rectangle* rd;
rectangle* lt;
rectangle* ld;
rectangle* tl;
rectangle* tr;
rectangle* dl;
rectangle* dr;
} rect;
int main() {
int i, j, k, l, m, n;
char pict[51][51];
int handled[51][51];
for (i = 0; i < 51; i++)
for (j = 0; j < 51; j++) handled[i][j] = -1;
scanf("%d %d", &m, &n);
for (i = 0; i < m; i++) scanf("%s", pict[i]);
int cols = strlen(pict[0]);
for (i = 0; i < m; i++)
for (j = 0; j < cols; j++) pict[i][j] -= '0';
vector<rect> rects;
int q = 0;
for (i = 0; i < m; i++)
for (j = 0; j < cols; j++) {
if (handled[i][j] == -1) {
int w = 1, h = 1;
for (int jj = j + 1; jj < cols && pict[i][j] == pict[i][jj]; w++, jj++)
;
for (int ii = i + 1; ii < m && pict[i][j] == pict[ii][j]; h++, ii++)
;
for (int ii = i; ii < i + h; ii++)
for (int jj = j; jj < j + w; jj++) handled[ii][jj] = q;
q++;
rect re;
re.r = i;
re.c = j;
re.w = w;
re.h = h;
re.color = pict[i][j];
re.tr = re.tl = re.dl = re.dr = re.lt = re.ld = re.rt = re.rd = NULL;
rects.push_back(re);
}
}
for (i = 0; i < rects.size(); i++) {
if (rects[i].r + rects[i].h < m) {
rects[i].dl = pict[rects[i].r + rects[i].h][rects[i].c] != 0
? &rects[handled[rects[i].r + rects[i].h][rects[i].c]]
: NULL;
rects[i].dr =
pict[rects[i].r + rects[i].h][rects[i].c + rects[i].w - 1] != 0
? &rects[handled[rects[i].r + rects[i].h]
[rects[i].c + rects[i].w - 1]]
: NULL;
}
if (rects[i].r - 1 >= 0) {
rects[i].tl = pict[rects[i].r - 1][rects[i].c] != 0
? &rects[handled[rects[i].r - 1][rects[i].c]]
: NULL;
rects[i].tr =
pict[rects[i].r - 1][rects[i].c + rects[i].w - 1] != 0
? &rects[handled[rects[i].r - 1][rects[i].c + rects[i].w - 1]]
: NULL;
}
if (rects[i].c + rects[i].w < cols) {
rects[i].rt = pict[rects[i].r][rects[i].c + rects[i].w] != 0
? &rects[handled[rects[i].r][rects[i].c + rects[i].w]]
: NULL;
rects[i].rd =
pict[rects[i].r + rects[i].h - 1][rects[i].c + rects[i].w] != 0
? &rects[handled[rects[i].r + rects[i].h - 1]
[rects[i].c + rects[i].w]]
: NULL;
}
if (rects[i].c - 1 >= 0) {
rects[i].lt = pict[rects[i].r][rects[i].c - 1] != 0
? &rects[handled[rects[i].r][rects[i].c - 1]]
: NULL;
rects[i].ld =
pict[rects[i].r + rects[i].h - 1][rects[i].c - 1] != 0
? &rects[handled[rects[i].r + rects[i].h - 1][rects[i].c - 1]]
: NULL;
}
}
rect* bp = &rects[0];
int dir = 0;
int cp = 0;
for (i = 0; i < n; i++) {
rect* next;
if (dir == 0) {
if (cp == 0) next = bp->rt;
if (cp == 1) next = bp->rd;
}
if (dir == 1) {
if (cp == 0) next = bp->dr;
if (cp == 1) next = bp->dl;
}
if (dir == 2) {
if (cp == 0) next = bp->ld;
if (cp == 1) next = bp->lt;
}
if (dir == 3) {
if (cp == 0) next = bp->tl;
if (cp == 1) next = bp->tr;
}
if (next != NULL)
bp = next;
else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dir = (dir + 1) % 4;
}
}
}
printf("%d\n", bp->color);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
char A[60][60];
int B[60][60];
bool was[60][60];
typedef struct {
pair<int, int> UL;
pair<int, int> UR;
pair<int, int> DL;
pair<int, int> DR;
} T1;
T1 V[2501];
typedef struct {
int BP;
int DP;
int CP;
} T2;
inline void doMagic(int c, int i, int j) {
if (i <= V[c].UL.first && j <= V[c].UL.second) V[c].UL = make_pair(i, j);
if (i <= V[c].UR.first && j >= V[c].UR.second) V[c].UR = make_pair(i, j);
if (i >= V[c].DL.first && j <= V[c].DL.second) V[c].DL = make_pair(i, j);
if (i >= V[c].DR.first && j >= V[c].DR.second) V[c].DR = make_pair(i, j);
}
int main() {
int m, n;
cin >> m >> n;
for (int i = 1; i <= m; i++) cin >> A[i] + 1;
int l = strlen(A[1] + 1);
for (int i0 = 1, c = 1; i0 <= m; i0++)
for (int j0 = 1; j0 <= l; j0++)
if (A[i0][j0] != '0' && !was[i0][j0]) {
queue<pair<int, int> > Q;
Q.push(make_pair(i0, j0));
was[i0][j0] = true;
B[i0][j0] = c;
T1 tmp;
tmp.UL = make_pair(i0, j0);
tmp.UR = make_pair(i0, j0);
tmp.DL = make_pair(i0, j0);
tmp.DR = make_pair(i0, j0);
V[c] = tmp;
while (!Q.empty()) {
int i = Q.front().first;
int j = Q.front().second;
doMagic(c, i, j);
for (int k = 0; k < 4; k++) {
int i1 = i + di[k];
int j1 = j + dj[k];
if (A[i1][j1] == A[i0][j0] && !was[i1][j1]) {
Q.push(make_pair(i1, j1));
was[i1][j1] = true;
B[i1][j1] = c;
}
}
Q.pop();
}
c++;
}
T2 P;
P.BP = B[1][1];
P.DP = 0;
P.CP = 0;
for (int k = 0; k < n; k++) {
int i, j;
if (P.DP == 3 && P.CP == 1 || P.DP == 0 && P.CP == 0) {
i = V[P.BP].UR.first;
j = V[P.BP].UR.second;
}
if (P.DP == 0 && P.CP == 1 || P.DP == 1 && P.CP == 0) {
i = V[P.BP].DR.first;
j = V[P.BP].DR.second;
}
if (P.DP == 1 && P.CP == 1 || P.DP == 2 && P.CP == 0) {
i = V[P.BP].DL.first;
j = V[P.BP].DL.second;
}
if (P.DP == 2 && P.CP == 1 || P.DP == 3 && P.CP == 0) {
i = V[P.BP].UL.first;
j = V[P.BP].UL.second;
}
i += di[P.DP];
j += dj[P.DP];
if (B[i][j] != 0) {
P.BP = B[i][j];
continue;
}
if (P.CP == 0) {
P.CP = 1;
continue;
}
P.DP = (P.DP + 1) % 4;
P.CP = 0;
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= l; j++)
if (B[i][j] == P.BP) {
cout << A[i][j];
goto exit;
}
exit:
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int x[100000], y[100000], DP[100000], CP[100000], R, C, used[50][50][4][4];
char s[64][64];
bool Out(int xx, int yy) { return xx < 0 || yy < 0 || xx >= R || yy >= C; }
void next(int x1, int y1, int DP1, int CP1, int &x2, int &y2, int &DP2,
int &CP2) {
int i;
for (i = 1;; i++) {
int nx = x1 + dx[DP1] * i;
int ny = y1 + dy[DP1] * i;
if (Out(nx, ny) || s[nx][ny] != s[x1][y1]) break;
}
i--;
x1 += dx[DP1] * i;
y1 += dy[DP1] * i;
for (i = 1;; i++) {
int nx = x1 + dx[(DP1 + CP1) & 3] * i;
int ny = y1 + dy[(DP1 + CP1) & 3] * i;
if (Out(nx, ny) || s[nx][ny] != s[x1][y1]) break;
}
i--;
x1 += dx[(DP1 + CP1) & 3] * i;
y1 += dy[(DP1 + CP1) & 3] * i;
int nx = x1 + dx[DP1];
int ny = y1 + dy[DP1];
if (Out(nx, ny) || !s[nx][ny]) {
x2 = x1;
y2 = y1;
if (CP1 == 1) {
CP2 = 3;
DP2 = DP1;
} else {
CP2 = 1;
DP2 = (DP1 + 3) & 3;
}
} else {
x2 = nx;
y2 = ny;
DP2 = DP1;
CP2 = CP1;
}
}
int main() {
int i, j, k, m;
scanf("%d%d", &R, &m);
for (i = 0; i < R; i++) scanf("%s", s[i]);
C = strlen(s[0]);
for (i = 0; i < R; i++)
for (j = 0; j < C; j++) s[i][j] -= '0';
CP[0] = 1;
memset(used, -1, sizeof(used));
used[0][0][0][1] = 0;
for (i = 1;; i++) {
next(x[i - 1], y[i - 1], DP[i - 1], CP[i - 1], x[i], y[i], DP[i], CP[i]);
if (used[x[i]][y[i]][DP[i]][CP[i]] >= 0) {
break;
}
used[x[i]][y[i]][DP[i]][CP[i]] = i;
}
int tmp = used[x[i]][y[i]][DP[i]][CP[i]];
if (m < i)
printf("%d\n", (int)s[x[m]][y[m]]);
else
printf(
"%d\n",
(int)s[x[tmp + (m - tmp) % (i - tmp)]][y[tmp + (m - tmp) % (i - tmp)]]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct pnt {
int x, y;
} ul[100][100], ur[100][100], dl[100][100], dr[100][100];
int n, m;
char a[100][100];
char s[][6] = {"up", "right", "down", "left"};
void init() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (j && a[i][j] == a[i][j - 1]) {
ul[i][j] = ul[i][j - 1];
} else if (i && a[i - 1][j] == a[i][j]) {
ul[i][j] = ul[i - 1][j];
} else {
ul[i][j].x = i;
ul[i][j].y = j;
}
}
for (int j = m - 1; j >= 0; --j) {
if (j + 1 < m && a[i][j] == a[i][j + 1]) {
ur[i][j] = ur[i][j + 1];
} else if (i && a[i - 1][j] == a[i][j]) {
ur[i][j] = ur[i - 1][j];
} else {
ur[i][j].x = i;
ur[i][j].y = j;
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j < m; ++j) {
if (j && a[i][j] == a[i][j - 1]) {
dl[i][j] = dl[i][j - 1];
} else if (i + 1 < n && a[i + 1][j] == a[i][j]) {
dl[i][j] = dl[i + 1][j];
} else {
dl[i][j].x = i;
dl[i][j].y = j;
}
}
for (int j = m - 1; j >= 0; --j) {
if (j + 1 < m && a[i][j] == a[i][j + 1]) {
dr[i][j] = dr[i][j + 1];
} else if (i + 1 < n && a[i + 1][j] == a[i][j]) {
dr[i][j] = dr[i + 1][j];
} else {
dr[i][j].x = i;
dr[i][j].y = j;
}
}
}
}
int main() {
int k;
scanf("%d%d\n", &n, &k);
for (int i = 0; i < n; ++i) gets(a[i]);
m = strlen(a[0]);
init();
int cp = 0, dp = 1, x = 0, y = 0, tx, ty, cc = a[0][0];
while (k--) {
if (dp == 1) {
if (cp == 0) {
tx = ur[x][y].x;
ty = ur[x][y].y + 1;
} else {
tx = dr[x][y].x;
ty = dr[x][y].y + 1;
}
} else if (dp == 2) {
if (cp == 1) {
tx = dr[x][y].x + 1;
ty = dr[x][y].y;
} else {
tx = dl[x][y].x + 1;
ty = dl[x][y].y;
}
} else if (dp == 3) {
if (cp == 2) {
tx = dl[x][y].x;
ty = dl[x][y].y - 1;
} else {
tx = ul[x][y].x;
ty = ul[x][y].y - 1;
}
} else {
if (cp == 3) {
tx = ul[x][y].x - 1;
ty = ul[x][y].y;
} else {
tx = ur[x][y].x - 1;
ty = ur[x][y].y;
}
}
if (tx < 0 || ty < 0 || tx >= n || ty >= m || a[tx][ty] == '0') {
if (dp == 1) {
if (cp == 0)
cp = 2;
else
dp = 2, cp = 1;
} else if (dp == 2) {
if (cp == 1)
cp = 3;
else
dp = 3, cp = 2;
} else if (dp == 3) {
if (cp == 2)
cp = 0;
else
dp = 0, cp = 3;
} else {
if (cp == 3)
cp = 1;
else
dp = 1, cp = 0;
}
} else
cc = a[tx][ty] - '0', x = tx, y = ty;
}
cout << a[x][y] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct state {
int i, j, dp, cp;
state() {}
state(int _i, int _j, int _dp, int _cp) : i(_i), j(_j), dp(_dp), cp(_cp) {}
};
state go[50][50][4][2];
pair<int, int> dirs[] = {make_pair(0, 1), make_pair(1, 0), make_pair(0, -1),
make_pair(-1, 0)};
int main(void) {
int m, n;
cin >> m >> n;
vector<string> s(m);
for (int i = 0; i < m; i++) cin >> s[i];
for (int i = 0; i < m; i++) {
for (int j = 0; j < s[i].size(); j++) {
for (int k = 0; k < 4; k++) {
pair<int, int> cpos(i, j);
pair<int, int> pcpos(cpos);
while (cpos.first >= 0 && cpos.first < m && cpos.second >= 0 &&
cpos.second < s[cpos.first].size() &&
s[i][j] == s[cpos.first][cpos.second]) {
pcpos = cpos;
cpos.first += dirs[k].first;
cpos.second += dirs[k].second;
}
for (int l = 0; l < 2; l++) {
int dir2 = (4 + k + (l ? -1 : 1)) % 4;
pair<int, int> dpos(pcpos);
pair<int, int> pdpos(dpos);
while (dpos.first >= 0 && dpos.first < m && dpos.second >= 0 &&
dpos.second < s[dpos.first].size() &&
s[i][j] == s[dpos.first][dpos.second]) {
pdpos = dpos;
dpos.first += dirs[dir2].first;
dpos.second += dirs[dir2].second;
}
dpos = make_pair(pdpos.first + dirs[k].first,
pdpos.second + dirs[k].second);
if (dpos.first >= 0 && dpos.first < m && dpos.second >= 0 &&
dpos.second < s[dpos.first].size() &&
s[dpos.first][dpos.second] != '0') {
go[i][j][k][l] = state(dpos.first, dpos.second, k, l);
} else {
if (l) {
go[i][j][k][l] = state(i, j, k, !l);
} else {
go[i][j][k][l] = state(i, j, (k + 1) % 4, !l);
}
}
}
}
}
}
state st(0, 0, 0, 1);
for (int step = 0; step < n; step++) {
st = go[st.i][st.j][st.dp][st.cp];
}
cout << s[st.i][st.j];
}
|
#include <bits/stdc++.h>
using namespace std;
struct state {
int x, y, dir;
bool left;
};
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};
struct solve {
int b[52][52];
int mem[52][52][4][2];
void adjust(state& s) {
bool incx = dx[s.dir] > 0;
if (dx[s.dir] == 0 &&
((dy[s.dir] > 0 && s.left) || (dy[s.dir] < 0 && !s.left)))
incx = true;
bool incy = dy[s.dir] > 0;
if (dy[s.dir] == 0 &&
((dx[s.dir] > 0 && !s.left) || (dx[s.dir] < 0 && s.left)))
incy = true;
int ix = incx ? 1 : -1;
int iy = incy ? 1 : -1;
while (b[s.y][s.x] == b[s.y][s.x + ix]) s.x += ix;
while (b[s.y][s.x] == b[s.y + iy][s.x]) s.y += iy;
bool moved = false;
if (dx[s.dir] != 0) {
if (b[s.y][s.x + ix] != 0) {
s.x += ix;
moved = true;
}
}
if (dy[s.dir] != 0) {
if (b[s.y + iy][s.x] != 0) {
s.y += iy;
moved = true;
}
}
if (!moved) {
if (s.left)
s.left = false;
else {
s.left = true;
s.dir = (s.dir + 1) % 4;
}
}
}
void doit() {
memset(b, 0, sizeof(b));
memset(mem, -1, sizeof(mem));
int m, N;
cin >> m >> N;
for (int i = 0; i < m; ++i) {
string s;
cin >> s;
for (int j = 0; j < s.size(); ++j) b[i + 1][j + 1] = s[j] - '0';
}
state s;
s.x = 1;
s.y = 1;
s.dir = 0;
s.left = true;
int t = 0;
while (t < N) {
adjust(s);
++t;
int& loc = mem[s.x][s.y][s.dir][s.left ? 1 : 0];
if (loc < 0)
loc = t;
else {
int diff = t - loc;
int rem = N - t;
if ((rem % diff) == 0) t = N;
}
}
int ret = b[s.y][s.x];
cout << ret << endl;
}
};
int main() {
solve s;
s.doit();
return 0;
}
|
#include <bits/stdc++.h>
int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct B {
bool u;
char x;
char y;
char dp;
char cp;
B(int _x = 0, int _y = 0, int _d = 0, int _c = 0, bool _u = false) {
u = _u;
x = _x;
y = _y;
dp = _d;
cp = _c;
}
};
B b[55][55][4][2];
char a[55][55], c;
int m, n, ans, x, y, DP, CP;
int dpmx[] = {1, 0, -1, 0};
int dpmy[] = {0, 1, 0, -1};
B move(int x, int y, int DP, int CP) {
if (b[y][x][DP][CP].u) {
return b[y][x][DP][CP];
}
if (a[y + dpmy[DP]][x + dpmx[DP]] == a[y][x]) {
return b[y][x][DP][CP] = move(x + dpmx[DP], y + dpmy[DP], DP, CP);
}
int z = CP ? 1 : -1;
if (a[y + dpmy[(4 + DP + z) % 4]][x + dpmx[(4 + DP + z) % 4]] == a[y][x]) {
return b[y][x][DP][CP] = move(x + dpmx[(4 + DP + z) % 4],
y + dpmy[(4 + DP + z) % 4], DP, CP);
}
if (a[y + dpmy[DP]][x + dpmx[DP]] != 0) {
return b[y][x][DP][CP] = B(x + dpmx[DP], y + dpmy[DP], DP, CP, 1);
} else {
if (CP == 0)
return b[y][x][DP][CP] = B(x, y, DP, 1, 1);
else
return b[y][x][DP][CP] = B(x, y, (DP + 1) % 4, 0, 1);
}
}
int main() {
scanf("%d%d\n", &m, &n);
for (int i = 0; i < m; i++) {
int j = 1;
while (1) {
scanf("%c", &c);
if (c < '0' || c > '9') break;
a[i + 1][j++] = c - '0';
}
scanf("\n");
}
x = y = 1;
DP = CP = 0;
for (int i = 0; i < n; i++) {
if (!b[y][x][DP][CP].u) move(x, y, DP, CP);
int tx = b[y][x][DP][CP].x;
int ty = b[y][x][DP][CP].y;
int tDP = b[y][x][DP][CP].dp;
int tCP = b[y][x][DP][CP].cp;
x = tx;
y = ty;
DP = tDP;
CP = tCP;
x = tx;
}
printf("%c", a[y][x] + '0');
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long x, y, c, n;
char s[51][51];
int b[2500][13];
int which(int px, int py) {
for (int i = 0; i < c; i++)
if (px >= b[i][9] && py >= b[i][10] && px <= b[i][11] && py <= b[i][12])
return i;
return -1;
}
void walk(int px, int py) {
int i, ex;
char cc = s[px][py];
b[c][8] = cc - '0';
b[c][9] = px;
b[c][10] = py;
for (i = px; i < x; i++)
if (s[i][py] != cc) break;
ex = b[c][11] = i - 1;
for (i = py; i < y; i++)
if (s[ex][i] != cc) break;
b[c][12] = i - 1;
c++;
}
void neighb(int bi) {
int bx = b[bi][9];
int by = b[bi][10];
int ex = b[bi][11];
int ey = b[bi][12];
if (ey == y - 1) {
b[bi][0] = -1;
b[bi][1] = -1;
} else {
b[bi][0] = which(bx, ey + 1);
b[bi][1] = which(ex, ey + 1);
}
if (ex == x - 1) {
b[bi][2] = -1;
b[bi][3] = -1;
} else {
b[bi][2] = which(ex + 1, ey);
b[bi][3] = which(ex + 1, by);
}
if (by == 0) {
b[bi][4] = -1;
b[bi][5] = -1;
} else {
b[bi][4] = which(ex, by - 1);
b[bi][5] = which(bx, by - 1);
}
if (bx == 0) {
b[bi][6] = -1;
b[bi][7] = -1;
} else {
b[bi][6] = which(bx - 1, by);
b[bi][7] = which(bx - 1, ey);
}
}
int main() {
cin >> x >> n;
cin.getline(s[0], 1);
for (int i = 0; i < x; i++) cin.getline(s[i], 51);
y = strlen(s[0]);
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++) {
if (s[i][j] == '0') continue;
if (i > 0 && (s[i - 1][j] == s[i][j]) ||
j > 0 && (s[i][j - 1] == s[i][j]))
continue;
walk(i, j);
}
for (int i = 0; i < c; i++) neighb(i);
int bp = 0, dp = 0, cp = 0;
for (int i = 0; i < n; i++) {
if (b[bp][dp + cp] < 0) {
if (cp) dp = (dp + 2) % 8;
cp = 1 - cp;
} else {
bp = b[bp][dp + cp];
}
}
cout << b[bp][8];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int n, m, cp, dp, t;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
char c[55][55];
pair<int, int> d[55][55][4], cur = make_pair(0, 0);
void inc() {
if (cp == 0)
cp = 1;
else
dp = (dp + 1) % 4, cp = 0;
}
void go() {
cur = d[cur.first][cur.second][dp];
int to = (dp + 3) % 4;
if (cp) to = (dp + 1) % 4;
cur = d[cur.first][cur.second][to];
int tox = cur.first + dx[dp], toy = cur.second + dy[dp];
if (tox < n && tox >= 0 && toy < m && toy >= 0 && c[tox][toy] != '0')
cur = make_pair(tox, toy);
else
inc();
}
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++) scanf("%s", c[i]);
m = strlen(c[0]);
for (int i = 0; i < n; i++)
for (int j = m - 1; j >= 0; j--)
if (j != m - 1 && c[i][j] == c[i][j + 1])
d[i][j][0] = d[i][j + 1][0];
else
d[i][j][0] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (j && c[i][j] == c[i][j - 1])
d[i][j][2] = d[i][j - 1][2];
else
d[i][j][2] = make_pair(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i && c[i][j] == c[i - 1][j])
d[i][j][3] = d[i - 1][j][3];
else
d[i][j][3] = make_pair(i, j);
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < m; j++)
if (i != n - 1 && c[i][j] == c[i + 1][j])
d[i][j][1] = d[i + 1][j][1];
else
d[i][j][1] = make_pair(i, j);
for (int i = 0; i < t; i++) go();
printf("%c\n", c[cur.first][cur.second]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char dc[4] = {'<', 'V', '>', '^'};
int main() {
int n, step;
char img[64][64];
int vis[64][64][4][2];
while (scanf("%d%d", &n, &step) != EOF) {
for (int i = 0; i < n; ++i) scanf("%s", img[i]);
int m = strlen(img[0]);
memset(vis, -1, sizeof(vis));
int x = 0, y = 0, dp = 2, cp = 0;
int base, cyc;
int i;
for (i = 0; i < step; ++i) {
if (vis[y][x][dp][cp] != -1) {
base = vis[y][x][dp][cp];
cyc = i - base;
break;
}
while (x - 1 >= 0 && img[y][x - 1] == img[y][x]) --x;
int minx = x;
while (y - 1 >= 0 && img[y - 1][x] == img[y][x]) --y;
int miny = y;
while (x + 1 < m && img[y][x + 1] == img[y][x]) ++x;
int maxx = x;
while (y + 1 < n && img[y + 1][x] == img[y][x]) ++y;
int maxy = y;
int edges[4] = {minx, maxy, maxx, miny};
int cpd = (cp ? (dp == 0 ? 3 : dp - 1) : (dp == 3 ? 0 : dp + 1));
for (x = minx; x <= maxx; ++x)
for (y = miny; y <= maxy; ++y) vis[y][x][dp][cp] = i;
if (dx[dp] == 0) {
x = edges[cpd];
y = edges[dp];
} else {
y = edges[cpd];
x = edges[dp];
}
int nx = x + dx[dp];
int ny = y + dy[dp];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && img[ny][nx] != '0') {
x = nx, y = ny;
} else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = (dp == 0 ? 3 : dp - 1);
}
}
}
if (i == step) {
printf("%c\n", img[y][x]);
} else {
step = base + (step - base) % cyc;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int d = 0; d < 4; ++d)
for (int lr = 0; lr < 2; ++lr)
if (vis[i][j][d][lr] == step) {
printf("%c\n", img[i][j]);
goto outer;
}
outer:
step = 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int UNDEF = -1;
const int INF = 1999999999;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int m, n, w;
inline bool chk(int y, int x) { return !(y < 0 || x < 0 || m <= y || w <= x); }
int main() {
while (cin >> m >> n) {
vector<string> map(m);
for (int i = 0; i < (m); ++i) {
cin >> map[i];
}
w = map[0].size();
int y = 0, x = 0;
int dp = 0, cp = 3;
int memo[m][w][4][4];
for (int y = 0; y < (m); ++y)
for (int x = 0; x < (w); ++x)
for (int i = 0; i < (4); ++i)
for (int j = 0; j < (4); ++j) {
memo[y][x][i][j] = -1;
}
bool cut = false;
for (int i = 0; i < (n); ++i) {
if (!cut && memo[y][x][dp][cp] != -1) {
int rest = n - i - 1;
int loop = i - memo[y][x][dp][cp];
i += (rest / loop) * loop;
cut = true;
} else {
memo[y][x][dp][cp] = i;
}
while (1) {
int ny = y + dy[dp], nx = x + dx[dp];
if (chk(ny, nx) && map[y][x] == map[ny][nx]) {
y = ny;
x = nx;
} else {
break;
}
}
int np = (dp + cp) % 4;
while (1) {
int ny = y + dy[np], nx = x + dx[np];
if (chk(ny, nx) && map[y][x] == map[ny][nx]) {
y = ny;
x = nx;
} else {
break;
}
}
int ny = y + dy[dp], nx = x + dx[dp];
if (chk(ny, nx) && map[ny][nx] != '0') {
y = ny;
x = nx;
} else if (cp == 3) {
cp = 1;
} else {
cp = 3;
dp = (dp + 1) % 4;
}
}
cout << map[y][x] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
template <class T>
inline T sqr(T x) {
return x * x;
}
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int n, m;
string s[50];
int BP, DP = 0, CP = 3;
struct point {
int x, y;
point(int X, int Y) {
x = X;
y = Y;
}
point() {}
};
struct rect {
point p[4];
int color;
rect(point p0, point p1, point p2, point p3, int cur_color) {
p[0] = p0;
p[1] = p1;
p[2] = p2;
p[3] = p3;
color = cur_color;
}
rect() {}
};
point curp(0, 0);
rect arr[50][50];
int dp[4][4];
bool used[50][50];
void init(int x, int y, rect r, int cur_color) {
if (x < 0 || y < 0 || x == n || y == s[0].length() || cur_color != s[x][y] ||
used[x][y])
return;
arr[x][y] = r;
used[x][y] = 1;
init(x + 1, y, r, cur_color);
init(x, y + 1, r, cur_color);
init(x - 1, y, r, cur_color);
init(x, y - 1, r, cur_color);
}
int main() {
dp[3][0] = 1;
dp[3][2] = 0;
dp[0][3] = 1;
dp[0][1] = 2;
dp[1][0] = 2;
dp[1][2] = 3;
dp[2][3] = 0;
dp[2][1] = 3;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) cin >> s[i];
memset(used, 0, sizeof used);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < s[0].length(); ++j) {
if (used[i][j]) continue;
int l = 0, h = 0;
while (j + l < s[0].length() && s[i][j] == s[i][j + l]) l++;
while (h + i < n && s[i][j] == s[h + i][j]) h++;
l--;
h--;
init(i, j,
rect(point(i, j), point(i, j + l), point(i + h, j + l),
point(i + h, j), s[i][j]),
s[i][j]);
}
}
BP = s[0][0];
for (int i = 0; i < m; ++i) {
curp = arr[curp.x][curp.y].p[dp[DP][CP]];
if (curp.x + dx[DP] < 0 || curp.y + dy[DP] < 0 || curp.x + dx[DP] >= n ||
curp.y + dy[DP] >= s[0].length() ||
s[curp.x + dx[DP]][curp.y + dy[DP]] == '0') {
if (CP + 1 != 4 && CP + 1 == DP || CP + 1 == 4 && DP == 0) {
CP += 2;
CP %= 4;
continue;
}
if (CP - 1 >= 0 && CP - 1 == DP || CP - 1 == -1 && DP == 3) {
CP--;
if (CP == -1) CP = 3;
DP++;
DP %= 4;
continue;
}
}
BP = s[curp.x + dx[DP]][curp.y + dy[DP]];
curp.x += dx[DP];
curp.y += dy[DP];
}
cout << BP - '0';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double inf = ~0u >> 2;
char s[55][55];
int a[55][55][4], n, k;
int judge(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= k) return 0;
if (s[x][y] == '0') return 0;
return 1;
}
int main() {
int i, j, m, g;
cin >> n >> m;
for (i = 0; i < n; i++) cin >> s[i];
k = strlen(s[1]);
for (i = 0; i < n; i++) {
for (j = 0; j < k; j++) {
for (g = j; g >= 0; g--)
if (s[i][g] != s[i][j]) break;
a[i][j][3] = g + 1;
for (g = i; g >= 0; g--)
if (s[g][j] != s[i][j]) break;
a[i][j][0] = g + 1;
for (g = j; g < k; g++)
if (s[i][g] != s[i][j]) break;
a[i][j][1] = g - 1;
for (g = i; g < n; g++)
if (s[g][j] != s[i][j]) break;
a[i][j][2] = g - 1;
}
}
int d1 = 1, d2 = 0, x = 0, y = 0;
char c = s[0][0];
int k1 = 1;
while (k1 <= m) {
if (d1 == 0 || d1 == 2)
x = a[x][y][d1];
else
y = a[x][y][d1];
if (d2 == 0 || d2 == 2)
x = a[x][y][d2];
else
y = a[x][y][d2];
int tx, ty;
if (d1 == 0) {
tx = x - 1;
ty = y;
} else if (d1 == 1) {
tx = x;
ty = y + 1;
} else if (d1 == 2) {
tx = x + 1;
ty = y;
} else {
tx = x;
ty = y - 1;
}
if (!judge(tx, ty)) {
if ((d2 + 1) % 4 == d1)
d2 = (d1 + 1) % 4;
else {
d1 = (d1 + 1) % 4;
d2 = (d1 - 1 + 4) % 4;
}
} else {
c = s[tx][ty];
x = tx, y = ty;
}
k1++;
}
cout << c << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, d[100][100][4], dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int mat[100][100] = {0}, len;
char st[1001];
int main() {
scanf("%d%d", &m, &n);
gets(st);
for (int i = 0; i < m; i++) {
gets(st);
len = strlen(st);
for (int j = 0; j < len; j++) {
mat[i + 1][j + 1] = st[j] - '0';
}
}
for (int i = (1); i < (m + 1); i++)
for (int j = (1); j < (len + 1); j++)
for (int k = (0); k < (4); k++) {
int l = 0, x = i, y = j;
while (mat[i][j] == mat[x][y]) x += dir[k][0], y += dir[k][1], l++;
d[i][j][k] = l;
}
pair<int, int> bp = pair<int, int>(1, 1);
int dp = 0, cp = 1;
for (int i = (0); i < (n); i++) {
int x = bp.first, y = bp.second, l = d[x][y][dp];
l--;
x += l * dir[dp][0];
y += l * dir[dp][1];
int l2 = d[x][y][cp];
l2--;
x += l2 * dir[cp][0];
y += l2 * dir[cp][1];
if (mat[x + dir[dp][0]][y + dir[dp][1]] == 0) {
if (cp == (dp + 1) % 4)
cp = ((dp - 1) % 4 + 4) % 4;
else {
int tmp = dp;
dp = cp;
cp = tmp;
}
} else {
x += dir[dp][0];
y += dir[dp][1];
}
bp.first = x;
bp.second = y;
}
printf("%d\n", mat[bp.first][bp.second]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct triple {
int first;
int second;
int third;
void set(int a, int b, int c) {
first = a;
second = b;
third = c;
}
};
triple t[53][53][8] = {0};
int table[53][53] = {0};
void f(int a, int b, int c) {
if (table[a][b] == 0 || b > 50 || a > 50 || a < 1 || b < 1) return;
int x = a;
int y = b;
if (c == 0) {
while (table[x - 1][y] == table[a][b]) x--;
while (table[x][y + 1] == table[a][b]) y++;
if (table[x][y + 1] != 0)
t[a][b][c].set(x, y + 1, c);
else
t[a][b][c].set(a, b, 1);
} else if (c == 1) {
while (table[x + 1][y] == table[a][b]) x++;
while (table[x][y + 1] == table[a][b]) y++;
if (table[x][y + 1] != 0)
t[a][b][c].set(x, y + 1, c);
else
t[a][b][c].set(a, b, 2);
} else if (c == 2) {
while (table[x + 1][y] == table[a][b]) x++;
while (table[x][y + 1] == table[a][b]) y++;
if (table[x + 1][y] != 0)
t[a][b][c].set(x + 1, y, c);
else
t[a][b][c].set(a, b, 3);
} else if (c == 3) {
while (table[x + 1][y] == table[a][b]) x++;
while (table[x][y - 1] == table[a][b]) y--;
if (table[x + 1][y] != 0)
t[a][b][c].set(x + 1, y, c);
else
t[a][b][c].set(a, b, 4);
} else if (c == 4) {
while (table[x + 1][y] == table[a][b]) x++;
while (table[x][y - 1] == table[a][b]) y--;
if (table[x][y - 1] != 0)
t[a][b][c].set(x, y - 1, c);
else
t[a][b][c].set(a, b, 5);
} else if (c == 5) {
while (table[x - 1][y] == table[a][b]) x--;
while (table[x][y - 1] == table[a][b]) y--;
if (table[x][y - 1] != 0)
t[a][b][c].set(x, y - 1, c);
else
t[a][b][c].set(a, b, 6);
} else if (c == 6) {
while (table[x - 1][y] == table[a][b]) x--;
while (table[x][y - 1] == table[a][b]) y--;
if (table[x - 1][y] != 0)
t[a][b][c].set(x - 1, y, c);
else
t[a][b][c].set(a, b, 7);
} else if (c == 7) {
while (table[x - 1][y] == table[a][b]) x--;
while (table[x][y + 1] == table[a][b]) y++;
if (table[x - 1][y] != 0)
t[a][b][c].set(x - 1, y, c);
else
t[a][b][c].set(a, b, 0);
}
}
int main() {
int n, m;
cin >> n >> m;
string s;
int h = 0;
cin.ignore();
for (int i = 0; i < n; i++) {
getline(cin, s);
for (int j = 0; j < s.size(); j++) {
table[i + 1][j + 1] = s[j] - '0';
}
}
h = s.size();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= h; j++) {
for (int k = 0; k < 8; k++) {
f(i, j, k);
}
}
}
int q1 = 1, q2 = 1, q3 = 0;
int w1, w2, w3;
for (int i = 0; i < m; i++) {
w1 = t[q1][q2][q3].first;
w2 = t[q1][q2][q3].second;
w3 = t[q1][q2][q3].third;
q1 = w1;
q2 = w2;
q3 = w3;
}
cout << table[q1][q2] << endl;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
int main() {
int map[50][50];
int border[4][50][50];
int m;
int rowSize;
int colSize;
int n;
int rowCurr = 0;
int colCurr = 0;
int iCol, iRow;
int iRun;
int DP = 0;
int CP = 0;
scanf("%d %d\n", &m, &n);
rowSize = m;
for (iRow = 0; iRow < m; ++iRow) {
char line[128] = {0};
scanf("%s\n", line);
colSize = 0;
for (iCol = 0; line[iCol] >= '0' && line[iCol] <= '9'; ++iCol) {
map[iRow][iCol] = (line[iCol] - '0');
}
colSize = iCol;
}
for (iRow = 0; iRow < rowSize; ++iRow) {
for (iCol = 0; iCol < colSize; ++iCol) {
int col = iCol;
int row = iRow;
while (col < colSize && map[iRow][col] == map[iRow][iCol]) ++col;
--col;
while (row < rowSize && map[row][col] == map[iRow][iCol]) ++row;
--row;
border[0][iRow][iCol] = (row << 8) | col;
row = iRow;
while (row >= 0 && map[row][col] == map[iRow][iCol]) --row;
++row;
border[3][iRow][iCol] = (row << 8) | col;
col = iCol;
while (col >= 0 && map[row][col] == map[iRow][iCol]) --col;
++col;
border[2][iRow][iCol] = (row << 8) | col;
row = iRow;
while (row < rowSize && map[row][col] == map[iRow][iCol]) ++row;
--row;
border[1][iRow][iCol] = (row << 8) | col;
}
}
for (iRun = 0; iRun < n; ++iRun) {
switch (DP) {
case 0: {
int col =
(CP) ? border[0][rowCurr][colCurr] : border[3][rowCurr][colCurr];
int row = col >> 8;
col = (col & 0xFF);
if (col < colSize - 1 && 0 != map[row][col + 1]) {
colCurr = col + 1;
rowCurr = row;
} else {
if (0 == CP)
CP = 1;
else {
CP = 0;
DP = 1;
}
}
} break;
case 1: {
int col =
(CP) ? border[1][rowCurr][colCurr] : border[0][rowCurr][colCurr];
int row = col >> 8;
col &= 0xFF;
if (row < rowSize - 1 && 0 != map[row + 1][col]) {
rowCurr = row + 1;
colCurr = col;
} else {
if (0 == CP)
CP = 1;
else {
CP = 0;
DP = 2;
}
}
} break;
case 2: {
int col =
(CP) ? border[2][rowCurr][colCurr] : border[1][rowCurr][colCurr];
int row = col >> 8;
col &= 0xFF;
if (col > 0 && 0 != map[row][col - 1]) {
colCurr = col - 1;
rowCurr = row;
} else {
if (0 == CP)
CP = 1;
else {
CP = 0;
DP = 3;
}
}
} break;
case 3: {
int col =
(CP) ? border[3][rowCurr][colCurr] : border[2][rowCurr][colCurr];
int row = col >> 8;
col &= 0xFF;
if (row > 0 && 0 != map[row - 1][col]) {
rowCurr = row - 1;
colCurr = col;
} else {
if (0 == CP)
CP = 1;
else {
CP = 0;
DP = 0;
}
}
} break;
}
}
printf("%d\n", map[rowCurr][colCurr]);
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int n;
char c;
bool neg;
n = 0;
neg = false;
for (c = getchar(); !(c >= '0' && c <= '9') && c != '-'; c = getchar())
;
if (c == '-') {
neg = true;
c = getchar();
}
while ((c >= '0' && c <= '9')) {
n = n * 10 + c - '0';
c = getchar();
}
return neg ? -n : n;
}
const int MAXN = 50;
const int MAXM = 50;
int n, m, col;
char str[MAXN][MAXM + 1];
int grupo[MAXN][MAXM];
int sig[MAXN * MAXM][4][2];
void crearGrupos() {
int i, j;
col = 0;
for (i = (0); i < (n); i++) {
for (j = (0); j < (m); j++) {
grupo[i][j] = -1;
if (str[i][j] != '0') {
if (i > 0 && str[i - 1][j] == str[i][j]) {
grupo[i][j] = grupo[i - 1][j];
}
if (j > 0 && str[i][j - 1] == str[i][j]) {
grupo[i][j] = grupo[i][j - 1];
}
if (grupo[i][j] == -1) {
grupo[i][j] = col++;
}
}
}
}
}
int color(int x, int y) {
return (x < 0 || x >= n || y < 0 || y >= m) ? -1 : grupo[x][y];
}
void crearSiguientes() {
int i, j, k, x1, x2, y1, y2;
for (i = (0); i < (col); i++) {
x1 = 0x3f3f3f3f;
x2 = -0x3f3f3f3f;
y1 = 0x3f3f3f3f;
y2 = -0x3f3f3f3f;
for (j = (0); j < (n); j++) {
for (k = (0); k < (m); k++) {
if (grupo[j][k] != i) {
continue;
}
x1 = min(x1, j);
x2 = max(x2, j);
y1 = min(y1, k);
y2 = max(y2, k);
}
}
sig[i][0][0] = color(x1, y2 + 1);
sig[i][0][1] = color(x2, y2 + 1);
sig[i][1][0] = color(x2 + 1, y2);
sig[i][1][1] = color(x2 + 1, y1);
sig[i][2][0] = color(x2, y1 - 1);
sig[i][2][1] = color(x1, y1 - 1);
sig[i][3][0] = color(x1 - 1, y1);
sig[i][3][1] = color(x1 - 1, y2);
}
}
int main() {
int i, j, k, c, dp, cp;
cin >> n >> k;
for (i = (0); i < (n); i++) {
cin >> str[i];
m = strlen(str[i]);
}
crearGrupos();
crearSiguientes();
c = grupo[0][0];
dp = 0;
cp = 0;
for (i = (0); i < (k); i++) {
if (sig[c][dp][cp] == -1) {
if (cp + 1 == 2) {
dp = (dp + 1) % 4;
cp = 0;
} else {
cp = 1;
}
} else {
c = sig[c][dp][cp];
}
}
for (i = (0); i < (n); i++) {
for (j = (0); j < (m); j++) {
if (grupo[i][j] == c) {
break;
}
}
if (j < m) {
break;
}
}
cout << str[i][j] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int m, n;
int step;
bool visit[151][151][4][2];
char a[151][151];
int f[151][151][4];
int fx, fy, dp, cp;
void movedp();
void movecp();
void movenext();
int main() {
cin >> m >> step;
for (int i = 1; i <= m; i++) {
string s;
cin >> s;
for (int j = 0; j < s.length(); j++) a[i][j + 1] = s[j];
n = s.length();
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
for (int k = 0; k < 4; k++) {
visit[i][j][k][0] = visit[i][j][k][1] = false;
f[i][j][k] = 0;
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j] == '0') continue;
if (i == 1)
f[i][j][0] = 0;
else if (a[i - 1][j] == a[i][j])
f[i][j][0] = f[i - 1][j][0] + 1;
else if (a[i - 1][j] != a[i][j])
f[i][j][0] = 0;
if (j == 1)
f[i][j][3] = 0;
else if (a[i][j - 1] == a[i][j])
f[i][j][3] = f[i][j - 1][3] + 1;
else if (a[i][j - 1] != a[i][j])
f[i][j][3] = 0;
}
for (int i = m; i >= 1; i--)
for (int j = n; j >= 1; j--) {
if (a[i][j] == '0') continue;
if (i == m)
f[i][j][2] = 0;
else if (a[i + 1][j] == a[i][j])
f[i][j][2] = f[i + 1][j][2] + 1;
else if (a[i + 1][j] != a[i][j])
f[i][j][2] = 0;
if (j == n)
f[i][j][1] = 0;
else if (a[i][j + 1] == a[i][j])
f[i][j][1] = f[i][j + 1][1] + 1;
else if (a[i][j + 1] != a[i][j])
f[i][j][1] = 0;
}
fx = fy = 1;
dp = 1;
cp = 0;
visit[fx][fy][dp][cp] = true;
for (int nstep = 1; nstep <= step; nstep++) {
movedp();
movecp();
movenext();
}
cout << a[fx][fy] << endl;
return 0;
}
void movedp() {
int t = f[fx][fy][dp];
if (dp == 0) {
fx = fx - t;
} else if (dp == 1) {
fy = fy + t;
} else if (dp == 2) {
fx = fx + t;
} else if (dp == 3) {
fy = fy - t;
}
}
int adjust() {
if (dp == 0) {
if (cp == 0)
return 3;
else
return 1;
} else if (dp == 1) {
if (cp == 0)
return 0;
else
return 2;
} else if (dp == 2) {
if (cp == 0)
return 1;
else
return 3;
} else if (dp == 3) {
if (cp == 0)
return 2;
else
return 0;
}
}
void movecp() {
int tt = adjust();
int t = f[fx][fy][tt];
if (tt == 0) {
fx = fx - t;
} else if (tt == 1) {
fy = fy + t;
} else if (tt == 2) {
fx = fx + t;
} else if (tt == 3) {
fy = fy - t;
}
}
void movenext() {
int t = 1;
int x = fx;
int y = fy;
if (dp == 0) {
x = x - t;
} else if (dp == 1) {
y = y + t;
} else if (dp == 2) {
x = x + t;
} else if (dp == 3) {
y = y - t;
}
if (x < 1 || y < 1 || x > m || y > n || (a[x][y] == '0')) {
if (cp == 0)
cp = 1;
else {
cp = 0;
dp = (dp + 1) % 4;
}
} else {
fx = x;
fy = y;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void file_open() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1},
dk[4][2] = {3, 1, 0, 2, 1, 3, 2, 0};
const int MAX = 60;
typedef char kieu[MAX][MAX];
struct point {
int x, y;
};
point tinh(kieu a, int i, int j, int dir) {
int t = a[i][j];
int u = i, v = j;
while (a[u][v] == t) {
u += di[dir];
v += dj[dir];
}
u -= di[dir];
v -= dj[dir];
point tmp = {u, v};
return tmp;
}
int main() {
int m, n, u, v, p;
kieu a = {};
int tr[MAX][MAX][8] = {};
point track[MAX * MAX * 8 + 1] = {};
scanf("%d%d\n", &m, &n);
u = v = 0;
for (int i = 1; i <= m; ++i) {
scanf("%s", a[i] + 1);
p = strlen(a[i] + 1);
for (int j = 1; j <= p; ++j) {
a[i][j] = a[i][j] - '0';
}
}
int bp, dp, cp, cost;
point tmp;
u = v = 1;
bp = a[u][v];
dp = 1;
cp = 0;
tr[u][v][dp * 2 + cp] = cost = 1;
track[1].x = 1;
track[1].y = 1;
while (n) {
int r1, r2, c1, c2, k;
k = dk[dp][cp];
tmp = tinh(a, u, v, dp);
r1 = tmp.x;
c1 = tmp.y;
tmp = tinh(a, r1, c1, k);
r2 = tmp.x;
c2 = tmp.y;
r1 = r2 + di[dp];
c1 = c2 + dj[dp];
if (a[r1][c1] == 0) {
r1 -= di[dp];
c1 -= dj[dp];
dp = (dp + cp) % 4;
cp = (cp + 1) % 2;
}
u = r1;
v = c1;
bp = a[u][v];
--n;
++cost;
if (tr[u][v][dp * 2 + cp]) {
cost = cost - tr[u][v][dp * 2 + cp];
n = n % cost;
tmp = track[tr[u][v][dp * 2 + cp] + n];
bp = a[tmp.x][tmp.y];
break;
} else {
tr[u][v][dp * 2 + cp] = cost;
tmp.x = u;
tmp.y = v;
track[cost] = tmp;
}
}
printf("%d", bp);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n, m;
vector<vector<int> > used, blocks, desk;
cin >> m >> n >> s;
int h = s.length();
used = vector<vector<int> >(m, vector<int>(h, 0));
desk = vector<vector<int> >(m, vector<int>(h));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < h; ++j) desk[i][j] = s[j] - '0';
cin >> s;
}
int curr = 1;
for (int i = 0; i < m; ++i)
for (int j = 0; j < h; ++j)
if (used[i][j] == 0 && desk[i][j] != 0) {
int st_j = j, fn_j = j, st_i = i, fn_i = i;
for (int g = j + 1; g < h && desk[i][g] == desk[i][j]; ++g, ++fn_j)
;
for (int g = i + 1; g < m && desk[g][j] == desk[i][j]; ++g, ++fn_i)
;
blocks.push_back(vector<int>(4));
blocks[curr - 1][0] = st_i;
blocks[curr - 1][1] = st_j;
blocks[curr - 1][2] = fn_i;
blocks[curr - 1][3] = fn_j;
for (int q = st_i; q <= fn_i; ++q)
for (int p = st_j; p <= fn_j; ++p) used[q][p] = curr;
++curr;
}
int d = 0, c = 0, pos = 1, x, y;
for (int q = 0; q < n; ++q) {
x = ((d == 1 || (d == 0 && c == 1) || (d == 2 && c == 0))
? blocks[pos - 1][2]
: blocks[pos - 1][0]) +
((d == 1) ? 1
: (d == 3) ? -1
: 0);
y = ((d == 0 || (d == 1 && c == 0) || (d == 3 && c == 1))
? blocks[pos - 1][3]
: blocks[pos - 1][1]) +
((d == 0) ? 1
: (d == 2) ? -1
: 0);
if ((x >= 0 && x < m) && (y >= 0 && y < h) && used[x][y] != 0) {
pos = used[x][y];
} else {
if (c == 0) {
c = 1;
} else {
c = 0;
d = (d + 1) % 4;
}
}
}
cout << desk[blocks[pos - 1][0]][blocks[pos - 1][1]];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int m, n, a[55][55];
int pos[55][55][4][2];
vector<int> state;
int up(int x, int y) {
while (x > 0 && a[x - 1][y] == a[x][y]) --x;
return x;
}
int left(int x, int y) {
while (y > 0 && a[x][y - 1] == a[x][y]) --y;
return y;
}
int main() {
cin >> m >> n;
int cols;
for (int i = 0; i < m; ++i) {
string s;
cin >> s;
cols = s.length();
for (size_t j = 0; j < s.length(); ++j) a[i][j] = s[j] - '0';
}
int p;
state.push_back(a[0][0]);
int x = 0, y = 0, dp = 0, cp = 0;
pos[x][y][dp][cp] = 1;
while (1) {
int x1 = x, y1 = y;
while (x1 + dx[dp] >= 0 && x1 + dx[dp] < m && y1 + dy[dp] >= 0 &&
y1 + dy[dp] < cols && a[x1][y1] == a[x1 + dx[dp]][y1 + dy[dp]])
x1 += dx[dp], y1 += dy[dp];
int k = (dp + 3) & 3;
if (cp == 1) k = (k + 2) & 3;
while (x1 + dx[k] >= 0 && x1 + dx[k] < m && y1 + dy[k] >= 0 &&
y1 + dy[k] < cols && a[x1][y1] == a[x1 + dx[k]][y1 + dy[k]])
x1 += dx[k], y1 += dy[k];
x1 += dx[dp];
y1 += dy[dp];
if (x1 >= 0 && x1 < m && y1 >= 0 && y1 < cols && a[x1][y1] > 0) {
x = up(x1, y1);
y = left(x1, y1);
} else if (cp == 0)
cp = 1;
else {
cp = 0;
dp = (dp + 1) & 3;
}
if (pos[x][y][dp][cp] == 0) {
state.push_back(a[x][y]);
pos[x][y][dp][cp] = state.size();
} else {
p = pos[x][y][dp][cp];
break;
}
}
if (p - 1 >= n)
cout << state[n - 1] << "\n";
else
cout << state[(n - p + 1) % (state.size() - p + 1) + p - 1] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int getint() {
unsigned int c;
int x = 0;
while (((c = getchar()) - '0') >= 10) {
if (c == '-') return -getint();
if (!~c) exit(0);
}
do {
x = (x << 3) + (x << 1) + (c - '0');
} while (((c = getchar()) - '0') < 10);
return x;
}
int getc_str(char str[]) {
unsigned int c, n = 0;
for (; (c = getchar()) <= ' ';)
if (!~c) exit(0);
do {
str[n++] = c;
} while ((c = getchar()) > ' ');
str[n] = '\0';
return n;
}
int wd, ht;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
inline bool inc(int x, int y) {
return 0 <= x and x < ht and 0 <= y and y < wd;
}
inline bool nget(int &nx, int &ny, int x, int y, int d) {
nx = x + dx[d], ny = y + dy[d];
return inc(nx, ny);
}
int n, m;
char fld[55][55];
int vis[55][55];
int next_pos[3333][4][2];
int next_dps[3333][4][2];
int next_cps[3333][4][2];
int minx[3333], maxx[3333], miny[3333], maxy[3333];
int color_table[3333];
void dfs(int x, int y, char color, int k) {
if (fld[x][y] != color) return;
color_table[k] = color;
vis[x][y] = k;
minx[k] = min(minx[k], x);
maxx[k] = max(maxx[k], x);
miny[k] = min(miny[k], y);
maxy[k] = max(maxy[k], y);
int nx, ny, d;
for (d = 0; d < 4; d++)
if (nget(nx, ny, x, y, d) and fld[nx][ny] == color and vis[nx][ny] == -1) {
dfs(nx, ny, color, k);
}
}
int main() {
int i, j, tcc, tc = 1;
for (tcc = 0; tcc < tc; tcc++) {
ht = getint(), n = getint();
for (i = 0; i < ht; i++) wd = getc_str(fld[i]);
memset(vis, ~0, sizeof(vis));
m = 0;
for (i = 0; i < ht; i++)
for (j = 0; j < wd; j++)
if (vis[i][j] == ~0) {
int color = fld[i][j];
if (color == '0') continue;
minx[m] = miny[m] = 1 << 28;
maxx[m] = maxy[m] = ~0;
dfs(i, j, color, m);
m++;
}
for (i = 0; i < ht; i++)
for (j = 0; j < wd; j++) {
if (fld[i][j] == '0') continue;
int k = vis[i][j];
int nx, ny;
nx = minx[k] - 1, ny = maxy[k];
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][0][0] = vis[nx][ny];
next_dps[k][0][0] = 0;
next_cps[k][0][0] = 0;
} else {
next_pos[k][0][0] = k;
next_dps[k][0][0] = 1;
next_cps[k][0][0] = 1;
}
nx = minx[k] - 1, ny = miny[k];
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][0][1] = vis[nx][ny];
next_dps[k][0][1] = 0;
next_cps[k][0][1] = 1;
} else {
next_pos[k][0][1] = k;
next_dps[k][0][1] = 0;
next_cps[k][0][1] = 0;
}
nx = maxx[k], ny = maxy[k] + 1;
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][1][0] = vis[nx][ny];
next_dps[k][1][0] = 1;
next_cps[k][1][0] = 0;
} else {
next_pos[k][1][0] = k;
next_dps[k][1][0] = 2;
next_cps[k][1][0] = 1;
}
nx = minx[k], ny = maxy[k] + 1;
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][1][1] = vis[nx][ny];
next_dps[k][1][1] = 1;
next_cps[k][1][1] = 1;
} else {
next_pos[k][1][1] = k;
next_dps[k][1][1] = 1;
next_cps[k][1][1] = 0;
}
nx = maxx[k] + 1, ny = miny[k];
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][2][0] = vis[nx][ny];
next_dps[k][2][0] = 2;
next_cps[k][2][0] = 0;
} else {
next_pos[k][2][0] = k;
next_dps[k][2][0] = 3;
next_cps[k][2][0] = 1;
}
nx = maxx[k] + 1, ny = maxy[k];
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][2][1] = vis[nx][ny];
next_dps[k][2][1] = 2;
next_cps[k][2][1] = 1;
} else {
next_pos[k][2][1] = k;
next_dps[k][2][1] = 2;
next_cps[k][2][1] = 0;
}
nx = minx[k], ny = miny[k] - 1;
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][3][0] = vis[nx][ny];
next_dps[k][3][0] = 3;
next_cps[k][3][0] = 0;
} else {
next_pos[k][3][0] = k;
next_dps[k][3][0] = 0;
next_cps[k][3][0] = 1;
}
nx = maxx[k], ny = miny[k] - 1;
if (inc(nx, ny) and fld[nx][ny] != '0') {
next_pos[k][3][1] = vis[nx][ny];
next_dps[k][3][1] = 3;
next_cps[k][3][1] = 1;
} else {
next_pos[k][3][1] = k;
next_dps[k][3][1] = 3;
next_cps[k][3][1] = 0;
}
}
int pos = 0, dp = 1, cp = 1;
for (i = 0; i < n; i++) {
int npos = next_pos[pos][dp][cp];
int ndp = next_dps[pos][dp][cp];
int ncp = next_cps[pos][dp][cp];
pos = npos, dp = ndp, cp = ncp;
}
putchar(color_table[pos]);
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
int n, m, num;
char tab[100][100];
int xx[10000][4];
int yy[10000][4];
int ch[100][100];
int id[100][100];
int getdir(int cd, int dd) {
if (cd == 1)
return (dd + 1) % 4;
else
return dd;
}
int main() {
scanf("%d %d", &n, &num);
for (int i = 0; i < n; i++) {
scanf("%s", tab[i]);
m = strlen(tab[i]);
for (int j = 0; j < m; j++) {
tab[i][j] -= '0';
}
}
int count = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (tab[i][j] == 0) {
id[i][j] = 0;
continue;
}
if (i - 1 >= 0 && tab[i - 1][j] == tab[i][j]) {
id[i][j] = id[i - 1][j];
} else if (j - 1 >= 0 && tab[i][j - 1] == tab[i][j]) {
id[i][j] = id[i][j - 1];
} else {
id[i][j] = count++;
xx[id[i][j]][0] = i;
yy[id[i][j]][0] = j;
xx[id[i][j]][1] = i;
yy[id[i][j]][3] = j;
}
if ((i + 1 >= n || tab[i][j] != tab[i + 1][j]) &&
(j + 1 >= m || tab[i][j] != tab[i][j + 1])) {
yy[id[i][j]][1] = j;
xx[id[i][j]][3] = i;
xx[id[i][j]][2] = i;
yy[id[i][j]][2] = j;
}
}
}
int tx, ty, cd = 0, dd = 1;
int pos = id[tx][ty];
for (int p = 0; p < num; p++) {
int dir = getdir(cd, dd);
tx = xx[pos][dir];
ty = yy[pos][dir];
int dx, dy;
if (dd == 0) {
dx = -1;
dy = 0;
}
if (dd == 1) {
dx = 0;
dy = 1;
}
if (dd == 2) {
dx = 1;
dy = 0;
}
if (dd == 3) {
dx = 0;
dy = -1;
}
if (tx + dx < 0 || tx + dx >= n || ty + dy < 0 || ty + dy >= m ||
id[tx + dx][ty + dy] == 0) {
if (cd == 0) {
cd = 1;
} else {
cd = 0;
dd += 1;
dd %= 4;
}
} else {
pos = id[tx + dx][ty + dy];
tx += dx;
ty += dy;
}
}
printf("%d\n", tab[tx][ty]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 55;
const int MAXB = 4000;
const int INF = (1 << 27);
int n, rows, columns, DP, BP, CP;
int m[MAX][MAX], label[MAX][MAX], border[MAXB][5], cor[MAXB];
int dl[] = {1, -1, 0, 0};
int dc[] = {0, 0, 1, -1};
char aux[MAX];
string s;
int ok(int i, int j) {
if (i < 0 || j < 0 || i >= rows || j >= columns) return 0;
if (m[i][j] == 0) return 0;
return 1;
}
void ret(int i, int j, int id, int color) {
label[i][j] = id;
cor[id] = color;
border[id][2] = min(border[id][2], j - 1);
border[id][0] = max(border[id][0], j + 1);
border[id][3] = min(border[id][3], i - 1);
border[id][1] = max(border[id][1], i + 1);
for (int k = 0; k < 4; k++) {
int u = i + dl[k], v = j + dc[k];
if (ok(u, v) && m[u][v] == color && label[u][v] == -1) ret(u, v, id, color);
}
}
void limpa() {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
label[i][j] = -1;
}
}
for (int i = 0; i < MAXB; i++) {
border[i][2] = border[i][3] = INF;
border[i][0] = border[i][1] = -INF;
}
BP = 1;
DP = 0;
CP = 3;
}
int main() {
scanf(" %d %d ", &rows, &n);
for (int i = 0; i < rows; i++) {
scanf(" %s", aux);
s = aux;
columns = (int)s.size();
for (int j = 0; j < columns; j++) m[i][j] = (int)(s[j] - '0');
}
limpa();
int cnt = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (m[i][j] > 0 && label[i][j] == -1) {
ret(i, j, cnt, m[i][j]);
cnt++;
}
}
}
int nl, nc;
for (int i = 0; i < n; i++) {
if (DP == 2 || DP == 0) {
nc = border[BP][DP];
if (CP == 3)
nl = border[BP][3] + 1;
else
nl = border[BP][1] - 1;
} else {
nl = border[BP][DP];
if (CP == 2)
nc = border[BP][2] + 1;
else
nc = border[BP][0] - 1;
}
if (ok(nl, nc))
BP = label[nl][nc];
else {
if ((CP + 1) % 4 == DP)
CP = (CP + 2) % 4;
else {
DP++;
DP %= 4;
CP = (DP + 3) % 4;
}
}
}
printf("%d\n", cor[BP]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char a[55][55];
int u[55][55][4][2];
int d[] = {0, 1, 1, 0, 0, -1, -1, 0}, dp, cp = -1;
int ls[50010000];
int lsk;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
m = strlen(a[1] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) a[i][j] -= '0';
int x = 1, y = 1;
for (int K = 0; K <= k - 1; K++) {
if (u[x][y][dp][cp == 1]) {
int pl = K - u[x][y][dp][cp == 1] + 1;
while (K < k) K += pl;
K -= pl;
}
u[x][y][dp][cp == 1] = K + 1;
int dx = x, dy = y;
while (a[dx][dy] == a[x][y]) dx += d[dp * 2], dy += d[dp * 2 + 1];
dx -= d[dp * 2], dy -= d[dp * 2 + 1];
int dcp = (dp + cp + 4) % 4;
while (a[dx][dy] == a[x][y]) dx += d[dcp * 2], dy += d[dcp * 2 + 1];
dx -= d[dcp * 2], dy -= d[dcp * 2 + 1];
if (a[dx + d[dp * 2]][dy + d[dp * 2 + 1]] > 0)
x = dx + d[dp * 2], y = dy + d[dp * 2 + 1];
else if (cp == -1)
cp = 1;
else
cp = -1, dp = (dp + 1) % 4;
}
printf("%d\n", a[x][y]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int togo[2500][8];
int comp[50][50], field[51][51];
int maxi[2500], maxj[2500], mini[2500], minj[2500];
int m, n, k;
int curcomp;
char rev(char x) {
char res = 0;
for (int i = 0; i < 8; i++) {
res = res * 2 + (x % 2);
x /= 2;
}
return res;
}
void dfs(int i, int j, int c, int ind) {
if (field[i][j] != ind || comp[i][j] || field[i][j] == 0) return;
comp[i][j] = c;
if (i > 0) dfs(i - 1, j, c, ind);
if (j > 0) dfs(i, j - 1, c, ind);
if (i < n - 1) dfs(i + 1, j, c, ind);
if (j < m - 1) dfs(i, j + 1, c, ind);
if (i > maxi[c]) maxi[c] = i;
if (i < mini[c]) mini[c] = i;
if (j > maxj[c]) maxj[c] = j;
if (j < minj[c]) minj[c] = j;
}
inline int _search(int curcomp) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (comp[i][j] == curcomp) return field[i][j];
}
inline void check(int steps) {
if (steps == k) {
cout << _search(curcomp);
exit(0);
}
}
int main() {
for (int c = 0; c < 2500; c++) {
maxi[c] = maxj[c] = -1;
mini[c] = minj[c] = 2147483647;
}
scanf("%d%d\n", &n, &k);
for (int i = 0; i < n; i++) {
m = 0;
char c;
while (scanf("%c", &c) == 1 && c != '\n') field[i][m++] = c - '0';
}
memset(comp, 0, sizeof(comp));
curcomp = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!comp[i][j] && field[i][j]) dfs(i, j, curcomp++, field[i][j]);
for (int c = 1; c < curcomp; c++) {
int i = maxi[c];
if (i < n - 1) {
for (int j = 0; j < m; j++)
if (comp[i][j] == c) togo[c][2] = comp[i + 1][j];
for (int j = 0; j < m; j++)
if (comp[i][j] == c) {
togo[c][3] = comp[i + 1][j];
break;
}
}
i = mini[c];
if (i > 0) {
for (int j = 0; j < m; j++)
if (comp[i][j] == c) togo[c][7] = comp[i - 1][j];
for (int j = 0; j < m; j++)
if (comp[i][j] == c) {
togo[c][6] = comp[i - 1][j];
break;
}
}
int j = maxj[c];
if (j < m - 1) {
for (i = 0; i < n; i++)
if (comp[i][j] == c) togo[c][1] = comp[i][j + 1];
for (i = 0; i < n; i++)
if (comp[i][j] == c) {
togo[c][0] = comp[i][j + 1];
break;
}
}
j = minj[c];
if (j > 0) {
for (i = 0; i < n; i++)
if (comp[i][j] == c) togo[c][4] = comp[i][j - 1];
for (i = 0; i < n; i++)
if (comp[i][j] == c) {
togo[c][5] = comp[i][j - 1];
break;
}
}
}
int curdir = 0;
curcomp = comp[0][0];
int startcomp = curcomp, steps = 0;
while (togo[curcomp][curdir] == 0 && steps < k)
curdir = (curdir + 1) % 8, steps++;
check(steps);
if (togo[curcomp][curdir]) {
curcomp = togo[curcomp][curdir];
steps++;
check(steps);
}
while (1) {
while (togo[curcomp][curdir] == 0 && steps < k)
curdir = (curdir + 1) % 8, steps++;
check(steps);
curcomp = togo[curcomp][curdir];
steps++;
check(steps);
}
while (curdir != 0 && steps < k) curdir = (curdir + 1) % 8, steps++;
check(k);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int m;
int n;
string board[50];
int blockn;
int cellBlock[50][50];
int blockCornerX[2500][2][2];
int blockCornerY[2500][2][2];
char solve() {
memset(cellBlock, -1, sizeof(cellBlock));
blockn = 0;
int w = m;
int h = board[0].size();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if ((board[i][j] != '0') && (cellBlock[i][j] == -1)) {
int a = i;
while ((a < w) && (board[a][j] == board[i][j])) {
a++;
}
int b = j;
while ((b < h) && (board[i][b] == board[i][j])) {
b++;
}
for (int x = i; x < a; x++) {
for (int y = j; y < b; y++) {
cellBlock[x][y] = blockn;
}
}
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
blockCornerX[blockn][x][y] = i + (a - 1 - i) * x;
blockCornerY[blockn][x][y] = j + (b - 1 - j) * y;
}
}
blockn++;
}
}
}
int BP = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int DP = 0;
int CP = -1;
for (int t = 0; t < n; t++) {
int nx = dx[DP] + dx[(DP + CP + 4) % 4];
int ny = dy[DP] + dy[(DP + CP + 4) % 4];
if (nx == -1) {
nx = 0;
} else {
nx = 1;
}
if (ny == -1) {
ny = 0;
} else {
ny = 1;
}
int cx = blockCornerX[BP][nx][ny];
int cy = blockCornerY[BP][nx][ny];
nx = cx + dx[DP];
ny = cy + dy[DP];
if ((nx < 0) || (nx >= w) || (ny < 0) || (ny >= h) ||
(board[nx][ny] == '0')) {
if (CP == -1) {
CP = 1;
} else {
DP = (DP + 1) % 4;
CP = -1;
}
} else {
BP = cellBlock[nx][ny];
}
}
return board[blockCornerX[BP][0][0]][blockCornerY[BP][0][0]];
}
inline void init() {}
int main() {
while (cin >> m >> n) {
for (int i = 0; i < m; i++) {
cin >> board[i];
}
cout << solve() << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[100][100];
int di[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int v[100][100];
int fst[4] = {1, 0, 1, 0};
int xi[4][2] = {1, 0, 0, 1, 2, 3, 3, 2};
int m, r;
struct zb {
int z[2];
} po[100][100][4][2];
void gx(int x, int y, int &a, int &b, int xi) {
if (xi == 0) {
if (x > a || (x == a && y > b)) {
a = x;
b = y;
}
}
if (xi == 1) {
if (x > a || (x == a && y < b)) {
a = x;
b = y;
}
}
if (xi == 2) {
if (x < a || (x == a && y > b)) {
a = x;
b = y;
}
}
if (xi == 3) {
if (x < a || (x == a && y < b)) {
a = x;
b = y;
}
}
}
void dfs(int x, int y, int cx, int cy) {
v[cx][cy] = 1;
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++) {
if (fst[i] == 1)
gx(cy, cx, po[x][y][i][j].z[1], po[x][y][i][j].z[0], xi[i][j]);
else
gx(cx, cy, po[x][y][i][j].z[0], po[x][y][i][j].z[1], xi[i][j]);
}
for (i = 0; i < 4; i++) {
int nx, ny;
nx = cx + di[i][0];
ny = cy + di[i][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < r && v[nx][ny] == 0 &&
s[nx][ny] == s[x][y])
dfs(x, y, nx, ny);
}
}
int main() {
int n, i, j, k, l, bp, cp, dp, cx, cy;
while (scanf("%d%d", &m, &n) == 2) {
for (i = 0; i < m; i++) scanf("%s", s[i]);
r = strlen(s[0]);
for (i = 0; i < m; i++)
for (j = 0; j < r; j++) {
for (k = 0; k < 4; k++)
for (l = 0; l < 2; l++) {
po[i][j][k][l].z[0] = i;
po[i][j][k][l].z[1] = j;
}
memset(v, 0, sizeof(v));
if (s[i][j] != '0') dfs(i, j, i, j);
}
cx = 0;
cy = 0;
cp = 0;
dp = 0;
while (n--) {
int nx, ny;
nx = po[cx][cy][cp][dp].z[0];
ny = po[cx][cy][cp][dp].z[1];
cx = nx;
cy = ny;
i = cp;
nx = cx + di[i][0];
ny = cy + di[i][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < r && s[nx][ny] != '0') {
cx = nx;
cy = ny;
} else {
if (dp == 0)
dp = 1;
else {
dp = 0;
cp = (cp + 1) % 4;
}
}
}
printf("%c\n", s[cx][cy]);
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int bpX, bpY, dp, cp;
void findEnd() {
int curX = bpX;
int curY = bpY;
int pointer = dp;
if (pointer == 0) {
while (v[curX][curY] == v[bpX][bpY]) {
curY++;
}
} else if (pointer == 1) {
while (v[curX][curY] == v[bpX][bpY]) {
curX++;
}
} else if (pointer == 2) {
while (v[curX][curY] == v[bpX][bpY]) {
curY--;
}
} else {
while (v[curX][curY] == v[bpX][bpY]) {
curX--;
}
}
if (pointer == 0) {
curY--;
} else if (pointer == 1) {
curX--;
} else if (pointer == 2) {
curY++;
} else {
curX++;
}
pointer += cp;
if (pointer == -1) {
pointer = 3;
} else if (pointer == 4) {
pointer = 0;
}
if (pointer == 0) {
while (v[curX][curY] == v[bpX][bpY]) {
curY++;
}
} else if (pointer == 1) {
while (v[curX][curY] == v[bpX][bpY]) {
curX++;
}
} else if (pointer == 2) {
while (v[curX][curY] == v[bpX][bpY]) {
curY--;
}
} else {
while (v[curX][curY] == v[bpX][bpY]) {
curX--;
}
}
if (pointer == 0) {
curY--;
} else if (pointer == 1) {
curX--;
} else if (pointer == 2) {
curY++;
} else {
curX++;
}
bpX = curX;
bpY = curY;
return;
}
int main() {
int m, n;
scanf("%d %d\n", &m, &n);
v.resize(m + 2);
char c = getchar();
v[1].push_back(0);
while (c != '\n') {
v[1].push_back(c - '0');
c = getchar();
}
v[1].push_back(0);
v[0].resize(v[1].size(), 0);
v.back().resize(v[1].size(), 0);
for (int i = 2; i <= m; i++) {
v[i].push_back(0);
c = getchar();
while (c != '\n') {
v[i].push_back(c - '0');
c = getchar();
}
v[i].push_back(0);
}
bpX = 1;
bpY = 1;
dp = 0;
cp = -1;
for (int i = 0; i < n; i++) {
findEnd();
int X = bpX, Y = bpY;
if (dp == 0) {
Y++;
} else if (dp == 1) {
X++;
} else if (dp == 2) {
Y--;
} else {
X--;
}
if (v[X][Y] == 0) {
if (cp == -1) {
cp = 1;
} else {
cp = -1;
dp++;
if (dp == 4) {
dp = 0;
}
}
} else {
bpX = X;
bpY = Y;
}
}
printf("%d", v[bpX][bpY]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Block {
int color;
int right_up;
int right_down;
int down_right;
int down_left;
int left_down;
int left_up;
int up_left;
int up_right;
int top;
int bottom;
int left;
int right;
Block(int c, int l, int r, int t, int b)
: color(c), left(l), right(r), top(t), bottom(b) {}
Block() {}
};
void block_map_fill(int, int, vector<string>&, vector<Block>&,
vector<vector<short> >&);
void blocks_charact_fill(int, int, Block&, vector<vector<short> >&);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k;
int h;
int m;
cin >> h;
cin >> k;
vector<string> vec(h);
cin >> vec[0];
m = vec[0].length();
vector<vector<short> > block_map(h, vector<short>(m, 0));
for (int i = 1; i < h; ++i) {
cin >> vec[i];
}
vector<Block> blocks(1);
block_map_fill(h, m, vec, blocks, block_map);
for (int i = 1; i < blocks.size(); i++) {
blocks_charact_fill(h, m, blocks[i], block_map);
}
int status = 0;
int cur = 1;
for (int i = 0; i < k; ++i) {
if (status == 0 && blocks[cur].right_up != 0) {
cur = blocks[cur].right_up;
} else if (status == 1 && blocks[cur].right_down != 0) {
cur = blocks[cur].right_down;
} else if (status == 2 && blocks[cur].down_right != 0) {
cur = blocks[cur].down_right;
} else if (status == 3 && blocks[cur].down_left != 0) {
cur = blocks[cur].down_left;
} else if (status == 4 && blocks[cur].left_down != 0) {
cur = blocks[cur].left_down;
} else if (status == 5 && blocks[cur].left_up != 0) {
cur = blocks[cur].left_up;
} else if (status == 6 && blocks[cur].up_left != 0) {
cur = blocks[cur].up_left;
} else if (status == 7 && blocks[cur].up_right != 0) {
cur = blocks[cur].up_right;
} else {
++status;
status %= 8;
}
}
cout << blocks[cur].color - '0';
}
void block_map_fill(int h, int m, vector<string>& vec, vector<Block>& blocks,
vector<vector<short> >& block_map) {
short num = 1;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < m; ++j) {
if (block_map[i][j] == 0 && vec[i][j] != '0') {
int fin_x = i;
int fin_y = j;
while (fin_x + 1 < h && vec[fin_x + 1][j] == vec[i][j]) {
++fin_x;
}
while (fin_y + 1 < m && vec[i][fin_y + 1] == vec[i][j]) {
++fin_y;
}
for (int x = i; x <= fin_x; ++x) {
for (int y = j; y <= fin_y; ++y) {
block_map[x][y] = num;
}
}
blocks.push_back(Block(vec[i][j], j, fin_y, i, fin_x));
++num;
}
}
}
}
void blocks_charact_fill(int h, int m, Block& thisBlock,
vector<vector<short> >& block_map) {
if (thisBlock.right + 1 < m) {
thisBlock.right_down = block_map[thisBlock.bottom][thisBlock.right + 1];
thisBlock.right_up = block_map[thisBlock.top][thisBlock.right + 1];
} else {
thisBlock.right_down = 0;
thisBlock.right_up = 0;
}
if (thisBlock.left - 1 >= 0) {
thisBlock.left_down = block_map[thisBlock.bottom][thisBlock.left - 1];
thisBlock.left_up = block_map[thisBlock.top][thisBlock.left - 1];
} else {
thisBlock.left_down = 0;
thisBlock.left_up = 0;
}
if (thisBlock.bottom + 1 < h) {
thisBlock.down_right = block_map[thisBlock.bottom + 1][thisBlock.right];
thisBlock.down_left = block_map[thisBlock.bottom + 1][thisBlock.left];
} else {
thisBlock.down_right = 0;
thisBlock.down_left = 0;
}
if (thisBlock.top - 1 >= 0) {
thisBlock.up_right = block_map[thisBlock.top - 1][thisBlock.right];
thisBlock.up_left = block_map[thisBlock.top - 1][thisBlock.left];
} else {
thisBlock.up_right = 0;
thisBlock.up_left = 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
short INF = 51;
struct block {
short xl, yl, xr, yr;
short col;
block(short a, short b, short c, short d, short f) {
xl = a;
yl = b;
xr = c;
yr = d;
col = f;
}
block() {
xl = -1;
yl = -1;
xr = INF;
yr = INF;
col = -1;
}
};
int main() {
short n, n2;
int m;
cin >> n >> m;
vector<string> tab(n);
for (short i = 0; i < n; i++) {
cin >> tab[i];
}
n2 = tab[0].size();
vector<vector<short> > was(n + 2, vector<short>(n2 + 2, -1));
vector<block> vec;
short coun = -1;
for (short i = 0; i < n; i++) {
for (short j = 0; j < n2; j++) {
if (was[i + 1][j + 1] == -1) {
coun++;
short maxx = 0, maxy = 0;
queue<pair<short, short> > q;
q.push({i, j});
while (!q.empty()) {
short x = q.front().first;
short y = q.front().second;
q.pop();
if (was[x + 1][y + 1] != -1) {
continue;
}
was[x + 1][y + 1] = coun;
if (y + 1 < n2) {
if (tab[x][y] == tab[x][y + 1]) {
q.push({x, y + 1});
} else {
maxy = y;
}
} else {
maxy = y;
}
if (x + 1 < n) {
if (tab[x + 1][y] == tab[x][y]) {
q.push({x + 1, y});
} else {
maxx = x;
}
} else {
maxx = x;
}
}
vec.push_back(block(i + 1, j + 1, maxx + 1, maxy + 1, tab[i][j] - '0'));
}
}
}
vec.push_back(block(-1, -1, -1, -1, 0));
coun++;
for (short i = 0; i <= n + 1; i++) {
was[i][0] = coun;
was[i][n2 + 1] = coun;
}
for (short i = 0; i <= n2 + 1; i++) {
was[n + 1][i] = coun;
was[0][i] = coun;
}
short dp = 0, cp = 1, bp = 0;
for (int i = 0; i < m; i++) {
if (dp == 4 && cp == 1) {
if (vec[was[vec[bp].xl - 1][vec[bp].yl]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xl - 1][vec[bp].yl];
}
continue;
}
if (dp == 4 && cp == 0) {
if (vec[was[vec[bp].xl - 1][vec[bp].yr]].col == 0) {
dp = 0;
cp = 1;
} else {
bp = was[vec[bp].xl - 1][vec[bp].yr];
}
continue;
}
if (dp == 0 && cp == 1) {
if (vec[was[vec[bp].xl][vec[bp].yr + 1]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xl][vec[bp].yr + 1];
}
continue;
}
if (dp == 0 && cp == 0) {
if (vec[was[vec[bp].xr][vec[bp].yr + 1]].col == 0) {
dp = 1;
cp = 1;
} else {
bp = was[vec[bp].xr][vec[bp].yr + 1];
}
continue;
}
if (dp == 1 && cp == 1) {
if (vec[was[vec[bp].xr + 1][vec[bp].yr]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xr + 1][vec[bp].yr];
}
continue;
}
if (dp == 1 && cp == 0) {
if (vec[was[vec[bp].xr + 1][vec[bp].yl]].col == 0) {
dp = 2;
cp = 1;
} else {
bp = was[vec[bp].xr + 1][vec[bp].yl];
}
continue;
}
if (dp == 2 && cp == 1) {
if (vec[was[vec[bp].xr][vec[bp].yl - 1]].col == 0) {
cp = 0;
} else {
bp = was[vec[bp].xr][vec[bp].yl - 1];
}
continue;
}
if (dp == 2 && cp == 0) {
if (vec[was[vec[bp].xl][vec[bp].yl - 1]].col == 0) {
dp = 4;
cp = 1;
} else {
bp = was[vec[bp].xl][vec[bp].yl - 1];
}
continue;
}
}
cout << vec[bp].col;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int a[100][100];
int step[100][100][4][2];
int n, k;
int ok(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; }
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (size_t j = 0; j < s.size(); j++) a[i][j] = s[j] - '0';
}
n = 50;
int x = 0, y = 0;
int dir = 0, cp = 0;
int cnt = 0;
while (1) {
cnt++;
if (step[x][y][dir][cp]) break;
step[x][y][dir][cp] = cnt;
while (ok(x + dx[dir], y + dy[dir]) &&
a[x + dx[dir]][y + dy[dir]] == a[x][y]) {
x += dx[dir];
y += dy[dir];
}
if (cp == 0)
dir = dir - 1;
else
dir = dir + 1;
dir = dir + 8;
dir %= 4;
while (ok(x + dx[dir], y + dy[dir]) &&
a[x + dx[dir]][y + dy[dir]] == a[x][y]) {
x += dx[dir];
y += dy[dir];
}
if (cp == 0)
dir = dir + 1;
else
dir = dir - 1;
dir = dir + 8;
dir %= 4;
if (ok(x + dx[dir], y + dy[dir]) && a[x + dx[dir]][y + dy[dir]] != 0) {
x += dx[dir];
y += dy[dir];
} else {
if (cp == 0)
cp = 1;
else {
cp = 0;
dir = dir + 1;
dir %= 4;
}
}
}
int loop = cnt - step[x][y][dir][cp];
cnt--;
while (k >= cnt) {
k -= loop;
}
int K = k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 2; l++) {
if (step[i][j][k][l] - 1 == K) {
cout << a[i][j] << endl;
return 0;
}
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, d[5][2][100][100];
char a[100][100];
bool was[100][100];
string s;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s;
m = s.length();
for (int j = 0; j < m; j++) {
a[i][j] = s[j];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!was[i][j]) {
int ii = i, jj = j;
while (ii < n && a[ii][j] == a[i][j]) ii++;
ii--;
while (jj < m && a[i][jj] == a[i][j]) jj++;
jj--;
for (int g = i; g <= ii; g++)
for (int h = j; h <= jj; h++) {
was[g][h] = true;
d[1][0][g][h] = ii * 100 + jj;
d[1][1][g][h] = i * 100 + jj;
d[2][0][g][h] = ii * 100 + j;
d[2][1][g][h] = ii * 100 + jj;
d[3][0][g][h] = i * 100 + j;
d[3][1][g][h] = ii * 100 + j;
d[4][0][g][h] = i * 100 + jj;
d[4][1][g][h] = i * 100 + j;
}
}
int in = 0, jn = 0, cp = 1, dp = 1, iin, jjn;
while (k) {
bool flag = false;
k--;
iin = in, jjn = jn;
in = d[dp][cp][iin][jjn] / 100;
jn = d[dp][cp][iin][jjn] % 100;
if (dp == 1 && jn < m - 1 && a[in][jn + 1] != '0')
jn++, flag = true;
else if (dp == 2 && in < n - 1 && a[in + 1][jn] != '0')
in++, flag = true;
else if (dp == 3 && jn > 0 && a[in][jn - 1] != '0')
jn--, flag = true;
else if (dp == 4 && in > 0 && a[in - 1][jn] != '0')
in--, flag = true;
if (!flag)
if (cp == 1)
cp = 0;
else
cp = 1, dp = dp % 4 + 1;
}
cout << a[in][jn];
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.