-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1048FollowMyLogic.cpp
More file actions
134 lines (132 loc) · 2.92 KB
/
Copy path1048FollowMyLogic.cpp
File metadata and controls
134 lines (132 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include<stdio.h>
#include<vector>
#include<limits.h>
#include<string.h>
#include<utility>
using namespace std;
typedef pair<int,int> PII;
int v[26];
struct Gate{
bool inv;
bool linv,rinv;
char type;
Gate *l ,* r;
Gate(){
l = r = NULL;
linv = rinv = inv = false;
}
bool eval(){
bool ret;
if(type>='A' && type<='Z') ret = v[type-'A']==1?true:false;
else{
bool lv = l->eval();
bool rv = r->eval();
if(linv) lv = !lv;
if(rinv) rv = !rv;
if(type==')') ret = lv&&rv;
else ret = lv||rv;
}
if(inv) ret = !ret;
return ret;
}
};
char pic[100][120];
int vis[100][120];
char s[120];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
inline bool check(char c){
if(c>='A' && c<='Z') return true;
if(c=='o') return true;
if(c==')') return true;
if(c=='>') return true;
return false;
}
Gate *build(int i,int j){
vis[i][j] = 1;
int x,y,nx,ny;
while(true){
x=i,y=j;
if(check(pic[x][y])) break;
for(int k=0;k<4;k++){
nx = x+dx[k],ny = y+dy[k];
if(pic[nx][ny]!=' ' && !vis[nx][ny]){
i=nx,j=ny;
vis[i][j] = 1;
break;
}
}
}
char g = pic[x][y];
Gate *root = new Gate();
if(g=='o'){
root->inv = true;
y--;
vis[x][y] = 1;
g=pic[x][y];
}
if(g>='A' && g<='Z'){
root->type = g;
return root;
}
root->type = g;
for(int i=-1;i<=1;i++)
for(int j=1;j<=2;j++)
vis[x+i][y-j] = 1;
int li,lj;
int ri,rj;
li = x-1, lj = y-3;
ri = x+1, rj = y-3;
if(pic[li][lj]=='o'){
vis[li][lj] = 1;
lj--;
root->linv = true;
}
if(pic[ri][rj]=='o'){
vis[ri][rj]=1;
rj--;
root->rinv = true;
}
root->l = build(li,lj);
root->r = build(ri,rj);
return root;
}
int main()
{
bool begin = true;
while(gets(s)){
memset(pic,' ',sizeof pic);
memset(vis,0,sizeof vis);
int len = strlen(s);
int ri,rj;
for(int j=0;j<len;j++){
pic[1][1+j] = s[j];
if(s[j]=='?') ri = 1, rj=1+j;
}
//pic[0][len] = 0;
int n=1;
while(gets(s),s[0]!='*'){
++n;
len = strlen(s);
for(int j=0;j<len;j++){
pic[n][1+j] = s[j];
if(s[j]=='?') ri = n, rj = 1+j;
}
//pic[n][len] = 0;
}
//bfs until a A, ) o >
if(begin)begin=false;
else
printf("\n");
vis[ri][rj] = 1;
Gate *root = build(ri,rj);
while(gets(s),s[0]!='*'){
for(int i=0;i<26;i++)
v[i] = s[i]-'0';
if(root->eval())printf("1\n");
else printf("0\n");
}
}
return 0;
}