#include "Bruno.h"
#include <vector>
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
namespace {
PII GetAnchor(PII A, PII B) {
VPII vecs{{0, 1}, {1, -1}, {1, 0}, {1, 1}};
for (auto v : vecs) {
if ((A.st + v.st - B.st) % 3 == 0 && (A.nd + v.nd - B.nd) % 3 == 0) {
return A;
}
}
return B;
}
} // namespace
int Go(int sr, int sc, int tr, int tc) {
if (tr < sr) {
return 3;
} else if (tr > sr) {
return 2;
} else if (tc > sc) {
return 0;
} else if (tc < sc) {
return 1;
}
return 4;
}
std::vector<int> Bruno(int K, std::vector<int> value) {
vector<int> res(K);
vector<VI> flags(3, VI(3));
int nxt = 0;
REP (i, 3) {
REP (j, 3) {
flags[i][j] = value[nxt++];
}
}
int fr, fc;
int r3, c3;
VPII nons;
REP (i, 3) {
REP (j, 3) {
if (flags[i][j] == 12) {
nons.PB({i, j});
}
}
}
assert(SZ(nons) == 2);
if (GetAnchor(nons[0], nons[1]) != nons[0]) {
swap(nons[0], nons[1]);
}
VPII wh;
REP (i, 3) {
REP (j, 3) {
if (i == 0 && j == 0) { continue; }
int ni = (nons[0].st + i) % 3;
int nj = (nons[0].nd + j) % 3;
if (ni == nons[1].st && nj == nons[1].nd) { continue; }
wh.PB({ni, nj});
}
}
debug(wh);
//vector<PII> heh{{0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
REP (goal, K) {
int r = wh[goal].st;
int c = wh[goal].nd;
int val = flags[r][c];
if (val == 8) { res[goal] = 2; continue; }
if (val == 9) { res[goal] = 3; continue; }
if (val == 10) { res[goal] = 0; continue; }
if (val == 11) { res[goal] = 1; continue; }
//int tr = -1, tc = -1;
int nxt = 1;
FOR (dr, -1, 1) {
FOR (dc, -1, 1) {
int nr = (r + dr + 3) % 3;
int nc = (c + dc + 3) % 3;
int fail = 0;
REP (tr, 2) {
if (nr == nons[tr].st && nc == nons[tr].nd) { fail = 1; }
}
if (fail) { continue; }
if (val == nxt) {
res[goal] = Go(1, 1, r + dr, c + dc);
// res[goal] = 4;
// if (dr == 1) { res[goal] = 2; }
// if (dr == -1) { res[goal] = 3; }
// if (dc == 1) { res[goal] = 0; }
// if (dc == -1) { res[goal] = 1; }
}
nxt++;
}
}
}
debug(value);
debug(res);
return res;
}
I2luY2x1ZGUgIkJydW5vLmgiCiNpbmNsdWRlIDx2ZWN0b3I+CgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBNUCBtYWtlX3BhaXIKI2RlZmluZSBQQiBwdXNoX2JhY2sKI2RlZmluZSBzdCBmaXJzdAojZGVmaW5lIG5kIHNlY29uZAojZGVmaW5lIHJkIHRoaXJkCiNkZWZpbmUgRk9SKGksIGEsIGIpIGZvcihpbnQgaSA9KGEpOyBpIDw9KGIpOyArK2kpCiNkZWZpbmUgUkUoaSwgbikgRk9SKGksIDEsIG4pCiNkZWZpbmUgRk9SRChpLCBhLCBiKSBmb3IoaW50IGkgPSAoYSk7IGkgPj0gKGIpOyAtLWkpCiNkZWZpbmUgUkVQKGksIG4pIGZvcihpbnQgaSA9IDA7aSA8KG4pOyArK2kpCiNkZWZpbmUgVkFSKHYsIGkpIF9fdHlwZW9mKGkpIHY9KGkpCiNkZWZpbmUgRk9SRShpLCBjKSBmb3IoVkFSKGksIChjKS5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9ucykgPT0gMik7CiAgaWYgKEdldEFuY2hvcihub25zWzBdLCBub25zWzFdKSAhPSBub25zWzBdKSB7CiAgICBzd2FwKG5vbnNbMF0sIG5vbnNbMV0pOwogIH0KICBWUElJIHdoOwogIFJFUCAoaSwgMykgewogICAgUkVQIChqLCAzKSB7CiAgICAgIGlmIChpID09IDAgJiYgaiA9PSAwKSB7IGNvbnRpbnVlOyB9CiAgICAgIGludCBuaSA9IChub25zWzBdLnN0ICsgaSkgJSAzOwogICAgICBpbnQgbmogPSAobm9uc1swXS5uZCArIGopICUgMzsKICAgICAgaWYgKG5pID09IG5vbnNbMV0uc3QgJiYgbmogPT0gbm9uc1sxXS5uZCkgeyBjb250aW51ZTsgfQogICAgICB3aC5QQih7bmksIG5qfSk7CiAgICB9CiAgfQogIGRlYnVnKHdoKTsKICAvL3ZlY3RvcjxQSUk+IGhlaHt7MCwgMn0sIHsxLCAwfSwgezEsIDF9LCB7MSwgMn0sIHsyLCAwfSwgezIsIDF9LCB7MiwgMn19OwogIFJFUCAoZ29hbCwgSykgewogICAgaW50IHIgPSB3aFtnb2FsXS5zdDsKICAgIGludCBjID0gd2hbZ29hbF0ubmQ7CiAgICBpbnQgdmFsID0gZmxhZ3Nbcl1bY107CiAgICBpZiAodmFsID09IDgpIHsgcmVzW2dvYWxdID0gMjsgY29udGludWU7IH0KICAgIGlmICh2YWwgPT0gOSkgeyByZXNbZ29hbF0gPSAzOyBjb250aW51ZTsgfQogICAgaWYgKHZhbCA9PSAxMCkgeyByZXNbZ29hbF0gPSAwOyBjb250aW51ZTsgfQogICAgaWYgKHZhbCA9PSAxMSkgeyByZXNbZ29hbF0gPSAxOyBjb250aW51ZTsgfQogICAgLy9pbnQgdHIgPSAtMSwgdGMgPSAtMTsKICAgIGludCBueHQgPSAxOwogICAgRk9SIChkciwgLTEsIDEpIHsKICAgICAgRk9SIChkYywgLTEsIDEpIHsKICAgICAgICBpbnQgbnIgPSAociArIGRyICsgMykgJSAzOwogICAgICAgIGludCBuYyA9IChjICsgZGMgKyAzKSAlIDM7CiAgICAgICAgaW50IGZhaWwgPSAwOwogICAgICAgIFJFUCAodHIsIDIpIHsKICAgICAgICAgIGlmIChuciA9PSBub25zW3RyXS5zdCAmJiBuYyA9PSBub25zW3RyXS5uZCkgeyBmYWlsID0gMTsgfQogICAgICAgIH0KICAgICAgICBpZiAoZmFpbCkgeyBjb250aW51ZTsgfQogICAgICAgIGlmICh2YWwgPT0gbnh0KSB7CiAgICAgICAgICByZXNbZ29hbF0gPSBHbygxLCAxLCByICsgZHIsIGMgKyBkYyk7Ci8vICAgICAgICAgICByZXNbZ29hbF0gPSA0OwovLyAgICAgICAgICAgaWYgKGRyID09IDEpIHsgcmVzW2dvYWxdID0gMjsgfQovLyAgICAgICAgICAgaWYgKGRyID09IC0xKSB7IHJlc1tnb2FsXSA9IDM7IH0KLy8gICAgICAgICAgIGlmIChkYyA9PSAxKSB7IHJlc1tnb2FsXSA9IDA7IH0KLy8gICAgICAgICAgIGlmIChkYyA9PSAtMSkgeyByZXNbZ29hbF0gPSAxOyB9CiAgICAgICAgfQogICAgICAgIG54dCsrOwogICAgICB9CiAgICB9CiAgfQogIGRlYnVnKHZhbHVlKTsKICBkZWJ1ZyhyZXMpOwogIHJldHVybiByZXM7Cn0K