#include "vision.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int dis(int i1, int j1, int i2, int j2) {
return abs(i1-i2) + abs(j1-j2);
}
void case1(int H, int W) {
int S = H*W;
for (int i=0; i<H*W; i++) {
if (i+1>=H*W) continue;
vector<int> Ns = {i, i+1};
add_and(Ns);
S++;
}
vector<int> res;
for (int i=H*W; i<S; i++) {
res.push_back(i);
}
add_or(res);
}
void construct_network(int H, int W, int k) {
int S = H*W;
//Columnas solitarias
if (min(H, W) == 1) {
case1(H, W);
return;
}
vector<int> rows(H, -1);
vector<int> col(W, -1);
for (int i=0; i<H; i++) {
vector<int> Ns;
for (int j=0; j<W; j++) {
Ns.push_back(i*W+j);
}
add_xor(Ns);
add_not(S);
add_or(Ns);
vector<int> aux = {S+1, S+2};
add_and(aux);
rows[i] = S;
S += 4;
}
for (int j=0; j<W; j++) {
vector<int> Ns;
for (int i=0; i<H; i++) {
Ns.push_back(i*W+j);
}
add_xor(Ns);
add_not(S);
add_or(Ns);
vector<int> aux = {S+1, S+2};
add_and(aux);
col[j] = S;
S += 4;
}
//Columnas adyacentes
vector<int> rows2(H, -1);
vector<int> col2(W, -1);
for (int i=0; i<H-1; i++) {
vector<int> Ns;
for (int j=0; j<W; j++) {
Ns.push_back(i*W+j);
Ns.push_back((i+1)*W+j);
}
//Ns = {rows[i], rows[i+1]};
add_xor(Ns);
add_not(S);
//Ns = {rows[i]+2, rows[i+1]+2};
add_or(Ns);
vector<int> aux = {S+1, S+2};
add_and(aux);
rows2[i] = S;
S += 4;
}
for (int j=0; j<W-1; j++) {
vector<int> Ns;
for (int i=0; i<H; i++) {
Ns.push_back(i*W+j);
Ns.push_back(i*W+j+1);
}
//x = a1^a2..
//y = b1^b2..
//a1^a2...^b1^b2... = x^y;
//Ns = {col[j], col[j+1]};
add_xor(Ns);
add_not(S);
//Ns = {col[j]+2, col[j+1]+2};
add_or(Ns);
vector<int> aux = {S+1, S+2};
add_and(aux);
col2[j] = S;
S += 4;
}
vector<int> Ns;
for (int i=0; i<H; i++) {
Ns.push_back(rows2[i]+3);
}
add_or(Ns); //S
Ns.clear();
for (int j=0; j<W; j++) {
Ns.push_back(col2[j]+3);
}
add_or(Ns); //S+1
int beg = S+2;
vector<int> res;
for (int i=0; i<H; i++) {
res.push_back(beg++);
Ns.clear();
Ns = {rows[i]+3, S+1};
add_and(Ns);
}
for (int i=0; i<W; i++) {
res.push_back(beg++);
Ns.clear();
Ns = {col[i]+3, S};
add_and(Ns);
}
add_or(res);
}