## Editorial for VOI 12 Bài 5 - Robocon

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

#### Code mẫu của flashmt

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

char a[505][505],f[2][2][505][1010];
int n,ans=2000;

int main()
{
int k,x,y,z=0;
cin >> n >> k;
while (k--) scanf("%d%d",&x,&y), a[x][y]=1;

for (int i=1;i<=n && !a[1][i];i++) f[0][0][i][i-1]=1;
for (int i=n;i && !a[1][i];i--) f[1][0][i][n-i]=1;

for (int i=2;i<=n && i-1<ans;i++)
{
z^=1;
for (int j=1;j<=n;j++)
for (int k=0;k<=i*2;k++)
f[0][z][j][k]=f[1][z][j][k]=0;

for (int j=1;j<=n;j++)
if (!a[i][j])
for (int k=i-1;k<ans && k<=i+j-2;k++)
f[0][z][j][k]=f[0][1-z][j-1][k-1] | f[0][1-z][j][k-1] | f[0][z][j-1][k-1];

for (int j=n;j;j--)
if (!a[i][j])
for (int k=i-1;k<ans && k<=i-1+n-j;k++)
{
f[1][z][j][k]=f[1][1-z][j+1][k-1] | f[1][1-z][j][k-1] | f[1][z][j+1][k-1];
if (f[1][z][j][k] && f[0][z][j][k]) ans=k;
}
}

cout << ans << endl;
}


#### Code mẫu của ladpro98

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, a, b) for(int i = (a); i <=(b); i++)
#define FORD(i, a, b) for(int i = (a); i > (b); i--)
#define REPD(i, a, b) for(int i = (a); i >=(b); i--)
#define SZ(a) (int((a).size()))
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define LL long long
#define LD long double
#define II pair<int, int>
#define X first
#define Y second
#define VI vector<int>
const int N = 505;
const int NN = 15 * N * N;
const int dx1[] = {0, 1, 1};
const int dy1[] = {1, 1, 0};
const int dx2[] = {0, 1, 1};
const int dy2[] = {-1, -1, 0};

using namespace std;
II Q1[NN], Q2[NN];
bool block[N][N], was1[N][N], was2[N][N];
int n, k, l1, r1, l2, r2;

bool out(II u)
{return u.X < 1 || u.Y < 1 || u.X > n || u.Y > n || block[u.X][u.Y];}

void bfs1() {
int tmp = r1;
while (l1 <= r1) {
II u = Q1[l1++];
FOR(i, 0, 3) {
II v (u.X + dx1[i], u.Y + dy1[i]);
if (out(v)) continue;
if (!was1[v.X][v.Y]) {
was1[v.X][v.Y] = 1;
Q1[++tmp] = v;
}
}
}
r1 = tmp;
}

bool bfs2() {
int tmp = r2;
while (l2 <= r2) {
II u = Q2[l2++];
FOR(i, 0, 3) {
II v (u.X + dx2[i], u.Y + dy2[i]);
if (out(v)) continue;
if (was1[v.X][v.Y]) return true;
if (!was2[v.X][v.Y]) {
was2[v.X][v.Y] = 1;
Q2[++tmp] = v;
}
}
}
r2 = tmp;
return false;
}

int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
int u, v;
FOR(i, 0, k) {
cin >> u >> v;
block[u][v] = 1;
}
l1 = r1 = 1; Q1[1] = II(1, 1);
l2 = r2 = 1; Q2[1] = II(1, n);
FOR(step, 1, n * n) {
bfs1();
if (bfs2()) {cout << step; break;}
REP(i, l1, r1) was1[Q1[i].X][Q1[i].Y] = 0;
REP(i, l2, r2) was2[Q2[i].X][Q2[i].Y] = 0;
}
return 0;
}


#### Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
if(!*inp) { \
if (REACHEOF) return 0;\
memset(BUF,0,sizeof BUF);\
int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
if (inpzzz != BUFSIZE) REACHEOF = true;\
inp=BUF; \
} \
INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
AM=0;\
GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
if (INP=='-') {AM=1;GETCHAR(INP);} \
j=INP-'0'; GETCHAR(INP); \
while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

int n, a[511][511];
pair<int,int> qu[2][2][511*511];
int top[2][2];
bool mark[2][2][511][511];

const int di[2][3] = {{0,1,1}, {0,1,1}};
const int dj[2][3] = {{1,0,1}, {-1,0,-1}};

bool ok(pair<int,int> v) {
return v.F > 0 && v.F <= n && v.S > 0 && v.S <= n && a[v.F][v.S] == 0;
}

int main() {
int k;
scanf("%d%d", &n, &k);
while (k--) {
int u, v; scanf("%d%d", &u, &v);
a[u][v] = 1;
}
if (n == 1) {
puts("0");
return 0;
}
// queue(turn, robot, id)
top[0][0] = 1; qu[0][0][1] = MP(1,1);
top[0][1] = 1; qu[0][1][1] = MP(1,n);
int cur = 0;

FOR(time,1,n+n) {
cur = 1 - cur;
memset(mark[cur], 0, sizeof mark[cur]);
REP(robot,2) {
top[cur][robot] = 0;
pair<int,int> u, v;

FOR(i,1,top[1-cur][robot]) {
u = qu[1-cur][robot][i];
REP(dir,3) {
v = MP(u.F + di[robot][dir], u.S + dj[robot][dir]);
if (!ok(v)) continue;
if (!mark[cur][robot][v.F][v.S]) {
mark[cur][robot][v.F][v.S] = true;
qu[cur][robot][++top[cur][robot]] = v;
}
}
}
}

bool ok = false;
FOR(i,1,top[cur][0]) {
pair<int,int> u = qu[cur][0][i];
if (mark[cur][0][u.F][u.S] && mark[cur][1][u.F][u.S]) {
ok = true;
break;
}
}
if (ok) {
printf("%d\n", time);
return 0;
}
}
return 0;
}


#### Code mẫu của hieult

#include<cstdio>
#include<cmath>
#include<math.h>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#define ep 0.00001
#define maxn 1030
#define oo 2000000001
#define modunlo 111539786
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define fi first
#define se second
//#define g 9.81
double const PI=4*atan(1.0);

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;

bool a[505][505] = {0},f[2][505][500] = {0},g[2][505][505] = {0};
int n,k,u,v;

int main(){

//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
scanf("%d %d",&n,&k);

for(int ik = 0; ik < k; ik++){
scanf("%d %d",&u,&v);
a[u][v] = 1;
}

if(n == 1){
printf("0");
return 0;
}

g[0][1][0] = 1;
g[1][n][0] = 1;
int MIN = oo;

for(int i = 1; i <= n; i++){
memset(g,0,sizeof(g));
for(int j = 1; j <= n; j++){
if(i == 1 && j == 1) g[0][1][0] = 1;
else if(!a[i][j]){
for(int k = 0; k < j; k++) g[0][j][k] = f[0][j][k] | g[0][j-1][k-1] | f[0][j-1][k];
}
}
for(int j = n; j >=1; j--){
if(i == 1 && j == n) g[1][n][0] = 1;
else if(!a[i][j]){
for(int k = 0; k < j; k++) g[1][j][k] = f[1][j][k] | g[1][j+1][k-1] | f[1][j+1][k];
}
}
for(int j = 1; j <=n; j++){
for(int k = 0; k < j; k++){
f[0][j][k] = g[0][j][k];
f[1][j][k] = g[1][j][k];
if(f[0][j][k] && f[1][j][k]){
// printf("%d %d %d\n",i,j,k);
MIN = min(MIN,i + k - 1);
}
}
}
}
printf("%d\n",MIN);
}


#### Code mẫu của ll931110

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define BLOCK 60
using namespace std;

long long olda[1005][17],newa[1005][17],oldb[1005][17],newb[1005][17],wall[1005][17];
int n,k;

int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) wall[i][j/BLOCK] |= (1LL << (j % BLOCK));
for (int i = 0; i < k; i++) {
int x,y;
scanf("%d %d", &x, &y);
x--;  y--;
wall[x][y/BLOCK] ^= (1LL << (y % BLOCK));
}

olda[0][0] = 1;
oldb[0][(n - 1)/BLOCK] = 1LL << ((n - 1) % BLOCK);
for (int ret = 1; ret <= n + n; ret++) {
memset(newa,0,sizeof(newa));
for (int i = 0; i < n; i++)
for (int j = 0; j < 17; j++) {
newa[i][j] |= olda[i][j] << 1;
if (olda[i][j] & (1LL << (BLOCK - 1))) newa[i][j + 1] |= 1;
newa[i][j] %= (1LL << BLOCK);
if (i >= n - 1) continue;
newa[i + 1][j] |= olda[i][j];
newa[i + 1][j] |= olda[i][j] << 1;
if (olda[i][j] & (1LL << (BLOCK - 1))) newa[i + 1][j + 1] |= 1;
newa[i + 1][j] |= (1LL << BLOCK);
}

for (int i = 0; i < n; i++)
for (int j = 0; j < 17; j++) {
newb[i][j] |= oldb[i][j] >> 1;
if (j && (oldb[i][j] & 1)) newb[i][j - 1] |= (1LL << (BLOCK - 1));
if (i >= n - 1) continue;
newb[i + 1][j] |= oldb[i][j];
newb[i + 1][j] |= oldb[i][j] >> 1;
if (j && (oldb[i][j] & 1)) newb[i + 1][j - 1] |= (1LL << (BLOCK - 1));
}

for (int i = 0; i < n; i++)
for (int j = 0; j < 17; j++) {
olda[i][j] = newa[i][j] & wall[i][j];
oldb[i][j] = newb[i][j] & wall[i][j];
if (olda[i][j] & oldb[i][j]) {
printf("%d\n", ret);
return 0;
}
}
}
printf("-1\n");
}


#### Code mẫu của skyvn97

#include<cstdio>
#include<queue>
#define MAX   505
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define fi   first
#define se   second
using namespace std;
bool allowCell[MAX][MAX];
bool inQueue[2][2][MAX][MAX];
int n;
int dx[][3]={{0,1,1},{0,1,1}};
int dy[][3]={{1,0,1},{-1,0,-1}};
void init(void) {
int k;
scanf("%d%d",&n,&k);
FOR(i,1,n) FOR(j,1,n) allowCell[i][j]=true;
REP(zz,k) {
int x,y;
scanf("%d%d",&x,&y);
allowCell[x][y]=false;
}
}
bool canMove(int x,int y) {
if (x<1 || x>n || y<1 || y>n) return (false);
return (allowCell[x][y]);
}
void process(void) {
queue<pair<int,int> > q[2][2];
q[0][0].push(make_pair(1,1));
q[0][1].push(make_pair(1,n));
inQueue[0][0][1][1]=true;
inQueue[0][1][1][n]=true;
REP(t,MAX*MAX) {
int curPos=t%2;
int nextPos=curPos^1;
REP(i,2) while (!q[curPos][i].empty()) {
int x=q[curPos][i].front().fi;
int y=q[curPos][i].front().se;
q[curPos][i].pop();
inQueue[curPos][i][x][y]=false;
if (inQueue[curPos][i^1][x][y]) {
printf("%d\n",t);
return;
}
REP(j,3) if (canMove(x+dx[i][j],y+dy[i][j])) {
int tx=x+dx[i][j];
int ty=y+dy[i][j];
if (!inQueue[nextPos][i][tx][ty]) {
inQueue[nextPos][i][tx][ty]=true;
q[nextPos][i].push(make_pair(tx,ty));
}
}
}
}
}
int main(void) {
init();
process();
return 0;
}


## Comments

Please read the guidelines before commenting.

There are no comments at the moment.