// 32351ZN #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef vector ivec_t; typedef vector imat_t; //ifstream fin("in"); //#define cin fin #define FOR_EACH(p,q,r) for(p=q;p!=r;p++) #define FOR_EACH_C(p,c) for(p=(c).begin();p!=(c).end();p++) const int FORK = 26; const int BLANK = 27; string g_exp; imat_t g_mat; int g_n; ivec_t g_reg(26, 0); int Calc(int a,int b,string op) { if(op == "AND")return a&b; return a|b; } int Priority(string op) { if(op == "AND")return 0; return 1; } bool ParsingSub(stack& num,stack& ost) { string o = ost.top(); ost.pop(); int tmp = num.top(); num.pop(); int t2 = num.top(); num.pop(); num.push(Calc(t2,tmp,o)); return !ost.empty(); } int Parsing(istream & istm) { stack num; stack ost; while(istm.good()) { string str(""); istm >> str; if(!str.size())break; if(str == "(") { num.push(Parsing(istm)); } else if(str == "NOT") { istm >> str; if(str == "(")num.push(Parsing(istm)^1); else if(str == "TRUE") { num.push(0); } else if(str == "FALSE") { num.push(1); } else if(str.size() == (size_t)1) { num.push(g_reg[str[0]-'A']^1); } else abort(); continue; } else if(str == ")")break; else if(str == "AND" || str == "OR") { if(!ost.empty()) { while(Priority(ost.top()) <= Priority(str)) { if(!ParsingSub(num,ost))break; } } ost.push(str); } else if(str == "TRUE") { num.push(1); } else if(str == "FALSE") { num.push(0); } else { num.push(g_reg[str[0]-'A']); } } while(num.size() > 1) { ParsingSub(num,ost); } return num.top(); } int Exp() { istringstream istm(g_exp); int result = Parsing(istm); return result; } void Solve() { int rcol = g_n, rrow = g_n; int way = 0; int xway[] = {1, 0, -1, 0}; int yway[] = {0, 1, 0, -1}; while(rcol < g_n * 2 + 1 && rrow < g_n * 2 + 1 && rcol >= 0 && rrow >= 0) { cout << rcol - g_n << " " << rrow - g_n << endl; if(g_mat[rrow][rcol] == FORK) { if(Exp()) way--; else way++; if(way >= 4) way = 0; else if(way < 0) way = 3; } else { if(g_mat[rrow][rcol] != BLANK) { g_reg[g_mat[rrow][rcol]] ^= 1; } } rrow += yway[way]; rcol += xway[way]; } } string InsertSpace(string exp) { string result; for(int i=0;i<(int)exp.size();i++) { if(exp[i] == '(' || exp[i] == ')') result += ' '; result += exp[i]; if(exp[i] == '(' || exp[i] == ')') result += ' '; } return result; } int main() { getline(cin, g_exp); g_exp = InsertSpace(g_exp); int n, m, k; cin >> n >> m >> k; g_n = n; g_mat.assign(n*2+1, ivec_t(n*2+1, BLANK)); for(int i=0;i> col >> row; g_mat[row+n][col+n] = FORK; } for(int i=0;i> col >> row >> reg; g_mat[row+n][col+n] = reg - 'A'; } Solve(); return 0; }