forked from pse1202/Programming-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor2.c
143 lines (130 loc) · 2.09 KB
/
xor2.c
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
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
short grid[502][502];
short point[501][501];
typedef struct _rect{
int x1;
int x2;
int y1;
int y2;
} rect;
int n;
int q,p;
int k;
int area;
int iswhole(){
int i,j;
for(i=q;i<=n;i++){
for(j=0;j<=n;j++){
if(point[i][j] == 1){
return 0;
}
}
}
return 1;
}
int flip(rect a){
point[a.y1-1][a.x1-1] ^= 1;
point[a.y1-1][a.x2] ^= 1;
point[a.y2][a.x1-1] ^= 1;
point[a.y2][a.x2] ^= 1;
area += (a.y2-a.y1+1)*(a.x2-a.x1+1);
return 0;
}
int ticks(int n){
if(n<=300){
return 2*n;
}
if(n>=450){
return 4;
}
}
rect find(){
int i,j; int check=0;
rect ans; int tick=0;
int xodd[501];
int yodd[501];
for(i=q;i<=n;i++){
for((check==0)?(j=p):(j=0);j<=n;j++){
check=1;
if(point[i][j]==1){
ans.x1 = j+1;
ans.y1 = i+1;
q = i;
p = j;
i = ans.y1 ;
j = ans.x1-1;
while(point[i][j] == 0){
i++;
}
ans.y2 = i;
i = ans.y1-1;
j = ans.x1;
while(point[i][j] == 0){
j++;
}
ans.x2 = j;
if(point[ans.y2][ans.x2] == 1){
return ans;
}
while(1){
if(point[ans.y2][ans.x2] == 1){
return ans;
}
if(ans.y2>=n || ans.x2>=n){
return ans;
}
if(tick >= ticks(n)){
return ans;
}
if((tick%2 == 0) ){
i = ans.y2+1;
j = ans.x1-1;
while(point[i][j] == 0){
i++;
if(i>=n){
return ans;
}
}
ans.y2 = i;
}else{
i = ans.y1-1;
j = ans.x2+1;
while(point[i][j]==0){
j++;
if(j>=n){
return ans;
}
}
ans.x2=j;
}
tick++;
}
}
}
}
}
int main(){
rect output[50000];
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%hd",&grid[i][j]);
}
}
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
point[i][j] = ((grid[i][j] + grid[i+1][j] + grid[i][j+1] + grid[i+1][j+1]) %2);
}
}
while(iswhole() == 0){
output[k] = find();
flip(output[k]);
k++;
}
printf("%d\n",k);
for(i=0;i<k;i++){
printf("%d %d %d %d\n",output[i].y1,output[i].x1,output[i].y2,output[i].x2);
}
return 0;
}