Problem:
Problem D:
Dungeon Wall
In 2337, people are bored at daily life and have crazy desire of having extraordinary experience. These days,“Dungeon Adventure” is one of the hottest attractions, where brave adventurers risk their lives to kill the evil monsters and save the world.
You are a manager of one of such dungeons. Recently you have been receiving a lot of complaints from soldiers who frequently enter your dungeon; they say your dungeon is too easy and they do not want to enter again. You are considering making your dungeon more difficult by increasing the distance between the entrance and the exit of your dungeon so that more monsters can approach the adventurers.
The shape of your dungeon is a rectangle whose width is
W
and whose height is
H
. The dungeon is a grid which consists of
W
×
H
square rooms of identical size 1 × 1. The southwest corner of the dungeon
has the coordinate (0; 0), and the northeast corner has (
W
,
H
). The outside of the dungeon is surrounded by walls, and there may be some more walls inside the dungeon in order to prevent adventurers from going directly to the exit. Each wall in the dungeon is parallel to either
x
-axis or
y
-axis, and both ends of each wall are at integer coordinates. An adventurer can move from one room to another room if they are adjacent vertically or horizontally and have no wall between.
You would like to add some walls to make the shortest path between the entrance and the exit longer. However, severe financial situation only allows you to build at most only one wall of a unit length. Note that a new wall must also follow the constraints stated above. Furthermore, you need to guarantee that there is at least one path from the entrance to the exit.
You are wondering where you should put a new wall to maximize the minimum number of steps between the entrance and the exit. Can you figure it out?
Input
W H N
sx
0
sy
0
dx
0
dy
0
sx
1
sy
1
dx
1
dy
1
.
.
.
ix iy
ox oy
The first line contains three integers:
W
,
H
and
N
(1 ≤
W
,
H
≤ 50, 0 ≤
N
≤ 1000). They are separated with a space.
The next
N
lines contain the specifications of
N
walls inside the dungeon. Each wall specification is a line containing four integers. The
i
-th specification contains
sx
i
,
sy
i
,
dx
i
and
dy
i
, separated with a space. These integers give the position of the
i
-th wall: it spans from (
sx
i
,
sy
i
) to (
dx
i
,
dy
i
).
The last two lines contain information about the locations of the entrance and the exit. The first line contains two integers
ix
and
iy
, and the second line contains two integers
ox
and
oy
. The entrance is located at the room whose south-west corner is (
ix
,
iy
), and the exit is located at the room whose southwest corner is (
ox
,
oy
).
Output
Print the maximum possible increase in the minimum number of required steps between the entrance and the exit just by adding one wall. If there is no good place to build a new wall, print zero.
Sample Input 1
3 4 4
1 0 1 1
1 3 1 4
1 2 2 2
2 1 2 3
0 0
1 0
Output 
...(truncated)

Submitted solutions:

code_snippet: #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxw 55
#define maxn 1010

int n;
int w;
int h;
bool wall[maxw][maxw][4];
int mp[maxw][maxw];
int ix,iy;
int ox,oy;
int que[maxw*maxw][2];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int bfs()
{
    memset(mp,-1,sizeof(mp));
    mp[ix][iy] = 0;
    int head = 0, tail = 0;
    tail++;
    que[tail][0] = ix;
    que[tail][1] = iy;
    while (head < tail)
    {
        ++head;
        int x = que[head][0];
        int y = que[head][1];
        //printf("**********%d %d\n",x,y);
        for (int i=0;i<4;i++){
            if (!wall[x][y][i]){
                int xx = x + dir[i][0];
                int yy = y + dir[i][1];
                if (mp[xx][yy]==-1){
                 //   printf("%d %d\n",xx,yy);
                    mp[xx][yy] = mp[x][y] + 1;
                    if (xx == ox && yy == oy)
                        return mp[xx][yy];
                    tail++;
                    que[tail][0] = xx;
                    que[tail][1] = yy;
                }
            }
        }
    }
    return mp[ox][oy];
}
void init()
{
    memset(wall,0,sizeof(wall));
    for (int i=1;i<=n;i++)
    {
        int sx,sy,dx,dy;
        scanf("%d%d%d%d",&sx,&sy,&dx,&dy);
        if (sx == dx && sy == dy)
            continue ;
        if (sx == dx){
            if (sy > dy)
                swap(sy,dy);
            for (int j = max(0,sy);j<min(h,dy);j++){
                if (sx-1>=0){
                    wall[sx-1][j][1] = 1;
                }
                if (sx<w){
                    wall[sx][j][3] = 1;
                }
            }
        }
        if (sy == dy){
            if (sx > dx)
                swap(sx,dx);
            for (int j=max(sx,0);j<min(dx,w);j++){
                if (sy-1>=0){
                    wall[j][sy - 1][0] = 1;
                }
                if (sy<h){
                    wall[j][sy][2] = 1;
                }
            }
        }
    }
    for (int j=0;j<h;j++){
        wall[0][j][3] = 1;
        wall[w-1][j][1] = 1;
    }
    for (int i=0;i<w;i++){
        wall[i][0][2] = 1;
        wall[i][h-1][0] = 1;
    }
    scanf("%d%d",&ix,&iy);
    scanf("%d%d",&ox,&oy);
/*
    for (int i=0;i<=w;i++)
        for (int j=0;j<=h;j++)
            for (int k=0;k<4;k++){
                printf("%d %d %d %d\n",i,j,k,wall[i][j][k]);
            }
*/
    return ;
}
bool check(int x,int y)
{
    return (x>=0 && x<w && y>=0 && y<h);
}
int main()
{
    while (scanf("%d%d%d",&w,&h,&n)!=EOF)
    {
        init();
        if (ix==ox && iy==oy){
            puts("0");
            continue;
        }
        int tmp = bfs();
       // printf("%d\n",tmp);
        int ans = 0;
        for (int i=0;i<w;i++)
            for (int j=0;j<h;j++)
                for (int k=0;k<4;k++)
                    if (!wall[i][j][k]){
                        wall[i][j][k] = 1;
                        int ii = i + dir[k][0];
                        int jj = j + dir[k][1];
                        int kk = (k+2)%4;
                        if (check(ii,jj))
                            wall[ii][jj][kk] = 1;
                        int ttt = bfs();
                        if (ttt>=0){
                            if(ttt - tmp > ans)
                                ans = ttt - tmp;
                        }
                        wall[i][j][k] = 0;
                        if (check(ii,jj))
                            wall[ii][jj][kk] = 0;
                    }
        printf("%d\n",ans);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3036
final_rank: 6

code_snippet: // AOJ #2225 Dungeon Wall
// 2025.4.30

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int w, h, n;
bool hwall[50][51], vwall[51][50];

int bfs(int sx, int sy, int tx, int ty) {
    static int d[50][50];
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++) d[i][j] = -1;

    queue<pair<int,int>> q;
    d[sy][sx] = 0;
    q.push({sx, sy});

    while (!q.empty()) {
        auto [x, y] = q.front(); q.pop();
        if (x == tx && y == ty) return d[y][x];
        if (x + 1 < w && !vwall[x+1][y] && d[y][x+1] < 0) {
            d[y][x+1] = d[y][x] + 1;
            q.push({x+1, y});
        }
        if (x - 1 >= 0 && !vwall[x][y] && d[y][x-1] < 0) {
            d[y][x-1] = d[y][x] + 1;
            q.push({x-1, y});
        }
        if (y + 1 < h && !hwall[x][y+1] && d[y+1][x] < 0) {
            d[y+1][x] = d[y][x] + 1;
            q.push({x, y+1});
        }
        if (y - 1 >= 0 && !hwall[x][y] && d[y-1][x] < 0) {
            d[y-1][x] = d[y][x] + 1;
            q.push({x, y-1});
        }
    }
    return INT_MAX;
}

int main() {
    w = Cin(), h = Cin(), n = Cin();
    for (int x = 0; x < w; x++) hwall[x][0] = hwall[x][h] = true;
    for (int y = 0; y < h; y++) vwall[0][y] = vwall[w][y] = true;

    for (int i = 0; i < n; i++) {
        int sx = Cin(), sy = Cin(), dx = Cin(), dy = Cin();
        if (sx == dx) {
            if (sy > dy) swap(sy, dy);
            for (int y = sy; y < dy; y++) vwall[sx][y] = true;
        } else {
            if (sx > dx) swap(sx, dx);
            for (int x = sx; x < dx; x++) hwall[x][sy] = true;
        }
    }

    int sx = Cin(), sy = Cin(), tx = Cin(), ty = Cin();
    int d0 = bfs(sx, sy, tx, ty);
    int ans = 0;

    for (int x = 1; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (!vwall[x][y]) {
                vwall[x][y] = true;
                int d1 = bfs(sx, sy, tx, ty);
                if (d1 < INT_MAX) ans = max(ans, d1 - d0);
                vwall[x][y] = false;
            }
        }
    }
    for (int x = 0; x < w; x++) {
        for (int y = 1; y < h; y++) {
            if (!hwall[x][y]) {
                hwall[x][y] = true;
                int d1 = bfs(sx, sy, tx, ty);
                if (d1 < INT_MAX) ans = max(ans, d1 - d0);
                hwall[x][y] = false;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3460
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define f first;
#define ss second
#define ll long long

int h,w,n;
int wallv[51][51][51];///x,y1,y2;
int wallh[51][51][51];///y x1,x2
int iy,ix,ox,oy;

int dist[55][55];
int valid(int x,int y){
    return (x<w && x>=0 && y>=0 && y<h);
}
void clean(){
    for(int i=0;i<50;i++){
        for(int j=0;j<50;j++)
            dist[i][j]=1e9;
    }
}
int bfs(){
    clean();
    queue<pair<int,int>>q;
    q.push({ix,iy});
    dist[ix][iy]=0;
    while(!q.empty()){
        int x=q.front().f;
        int y=q.front().ss;
        int d=dist[x][y];
        q.pop();
        if(x==ox && y==oy)return d;

        if(valid(x,y+1)   && !wallh[x][y][y+1] && dist[x][y+1]>d+1){


            dist[x][y+1]=d+1;
            q.push({x,y+1});
        }

        if(valid(x,y-1) && !wallh[x][y-1][y] && dist[x][y-1]>d+1){
            dist[x][y-1]=d+1;
            q.push({x,y-1});
        }

        if(valid(x+1,y) && !wallv[y][x][x+1] && dist[x+1][y]>d+1){
            dist[x+1][y]=d+1;
            q.push({x+1,y});
        }

        if(valid(x-1,y) && !wallv[y][x-1][x] && dist[x-1][y]>d+1){
            dist[x-1][y]=d+1;
            q.push({x-1,y});
        }

    }
    return dist[ox][oy];


}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin>>w>>h>>n;
    for(int i=0;i<n;i++){
        int x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2;

        if(x1==x2){
            if(y1>y2)swap(y1,y2);
            for(int y=y1;y<y2;y++){
                if(x1)wallv[y][x1-1][x1]=1;
            }
        }
        else{
            if(x1>x2)swap(x1,x2);
            for(int x=x1;x<x2;x++){
                if(y1)wallh[x][y1-1][y1]=1;
            }

        }
    }
    cin>>ix>>iy>>ox>>oy;


    int min_dist=bfs();
    ////cout<<min_dist<<endl;

    int ans=0;
    for(int i=0;i<w;i++){
        for(int j=0;j<h;j++){
            if(valid(i-1,j) && !wallv[j][i-1][i]){
                wallv[j][i-1][i]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallv[j][i-1][i]=0;


            }
            if(valid(i+1,j) && !wallv[j][i][i+1]){
                wallv[j][i][i+1]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallv[j][i][i+1]=0;

            }
            if(valid(i,j-1) && !wallh[i][j-1][j]){
                wallh[i][j-1][j]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallh[i][j-1][j]=0;
            }
            if(valid(i,j+1) && !wallh[i][j][j+1]){
                wallh[i][j][j+1]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallh[i][j][j+1]=0;
            }
        }
    }
    cout<<ans<<endl;


    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4508
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define f first;
#define ss second
#define ll long long

int h,w,n;
int wallv[51][51][51];///x,y1,y2;
int wallh[51][51][51];///y x1,x2
int iy,ix,ox,oy;

int dist[55][55];
int valid(int x,int y){
    return (x<=w && x>=0 && y>=0 && y<=h);
}
void clean(){
    for(int i=0;i<50;i++){
        for(int j=0;j<50;j++)
            dist[i][j]=1e9;
    }
}
int bfs(){
    clean();
    queue<pair<int,int>>q;
    q.push({ix,iy});
    dist[ix][iy]=0;
    while(!q.empty()){
        int x=q.front().f;
        int y=q.front().ss;
        int d=dist[x][y];
        q.pop();
        if(x==ox && y==oy)return d;

        if(valid(x,y+1)   && !wallh[x][y][y+1] && dist[x][y+1]>d+1){


            dist[x][y+1]=d+1;
            q.push({x,y+1});
        }

        if(valid(x,y-1) && !wallh[x][y-1][y] && dist[x][y-1]>d+1){
            dist[x][y-1]=d+1;
            q.push({x,y-1});
        }

        if(valid(x+1,y) && !wallv[y][x][x+1] && dist[x+1][y]>d+1){
            dist[x+1][y]=d+1;
            q.push({x+1,y});
        }

        if(valid(x-1,y) && !wallv[y][x-1][x] && dist[x-1][y]>d+1){
            dist[x-1][y]=d+1;
            q.push({x-1,y});
        }

    }
    return dist[ox][oy];


}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin>>w>>h>>n;
    for(int i=0;i<n;i++){
        int x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2;

        if(x1==x2){
            if(y1>y2)swap(y1,y2);
            for(int y=y1;y<y2;y++){
                if(x1)wallv[y][x1-1][x1]=1;
            }
        }
        else{
            if(x1>x2)swap(x1,x2);
            for(int x=x1;x<x2;x++){
                if(y1)wallh[x][y1-1][y1]=1;
            }

        }
    }
    cin>>ix>>iy>>ox>>oy;


    int min_dist=bfs();
    ////cout<<min_dist<<endl;

    int ans=0;
    for(int i=0;i<=w;i++){
        for(int j=0;j<=h;j++){
            if(valid(i-1,j) && !wallv[j][i-1][i]){
                wallv[j][i-1][i]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallv[j][i-1][i]=0;


            }
            if(valid(i+1,j) && !wallv[j][i][i+1]){
                wallv[j][i][i+1]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallv[j][i][i+1]=0;

            }
            if(valid(i,j-1) && !wallh[i][j-1][j]){
                wallh[i][j-1][j]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallh[i][j-1][j]=0;
            }
            if(valid(i,j+1) && !wallh[i][j][j+1]){
                wallh[i][j][j+1]=1;
                int cost=bfs();
                if(cost<1e9)
                    ans=max(ans,(cost-min_dist));
                wallh[i][j][j+1]=0;
            }
        }
    }
    cout<<ans<<endl;


    return 0;
}
accuracy: 0.48717948717948717
time_ms: 40
memory_kb: 4436
final_rank: 20

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 55

struct Info{
	Info(int arg_node_id,int arg_sum_cost){
		node_id = arg_node_id;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{

		return sum_cost > arg.sum_cost;
	}
	int node_id,sum_cost;
};

int W,H,N;
int num_nodes,start,goal;
int index_table[SIZE][SIZE];
int min_dist[SIZE*SIZE],MIN_DIST[SIZE*SIZE],rev_min_dist[SIZE*SIZE];
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};
bool wall[SIZE*SIZE][SIZE*SIZE];
vector<int> G[SIZE*SIZE];


bool rangeCheck(int row,int col){

	return row >= 0 && row <= H-1 && col >= 0 && col <= W-1;
}

void calc_rev(){

	for(int i = 0; i < num_nodes; i++){

		rev_min_dist[i] = BIG_NUM;
	}
	rev_min_dist[goal] = 0;

	priority_queue<Info> Q;
	Q.push(Info(goal,0));

	while(!Q.empty()){

		if(Q.top().sum_cost > rev_min_dist[Q.top().node_id]){

			Q.pop();
		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){

				int next = G[Q.top().node_id][i];
				if(wall[Q.top().node_id][next])continue;

				int next_cost = Q.top().sum_cost+1;

				if(rev_min_dist[next] > next_cost){
					rev_min_dist[next] = next_cost;
					Q.push(Info(next,next_cost));
				}
			}
			Q.pop();
		}
	}
}

int func(){

	for(int i = 0; i < num_nodes; i++){

		min_dist[i] = BIG_NUM;
	}
	min_dist[start] = 0;

	priority_queue<Info> Q;
	Q.push(Info(start,0));

	while(!Q.empty()){

		if(Q.top().sum_cost > min_dist[Q.top().node_id]){

			Q.pop();
		}else if(Q.top().node_id == goal){

			return Q.top().sum_cost;
		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){

				int next = G[Q.top().node_id][i];
				if(wall[Q.top().node_id][next])continue;

				int next_cost = Q.top().sum_cost+1;

				if(min_dist[next] > next_cost){
					min_dist[next] = next_cost;
					Q.push(Info(next,next_cost));
				}
			}
			Q.pop();
		}
	}

	return BIG_NUM;
}


int main(){

	scanf("%d %d %d",&W,&H,&N);

	int index = 0;
	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			index_table[row][col] = index++;
		}
	}

	num_nodes = H*W;
	for(int i = 0; i < num_nodes; i++){
		for(int k = 0; k < num_nodes; k++){

			wall[i][k] = false;
		}
	}

	int s_col,s_row,d_col,d_row;
	int from_index,to_index;

	for(int loop = 0; loop < N; loop++){

		scanf("%d %d %d %d",&s_col,&s_row,&d_col,&d_row);

		if(s_col == d_col){ //縦

			if(s_col == 0){
				continue;
			}

			for(int i = min(s_row,d_row); i < max(s_row,d_row); i++){

				from_index = index_table[i][s_col-1];
				to_index = index_table[i][s_col];

				wall[from_index][to_index] = true;
				wall[to_index][from_index] = true;
			}

		}else{ //横

			if(s_row == 0){
				continue;
			}

			for(int i = min(s_col,d_col); i < max(s_col,d_col); i++){

				from_index = index_table[s_row-1][i];
				to_index = index_table[s_row][i];

				wall[from_index][to_index] = true;
				wall[to_index][from_index] = true;
			}
		}
	}

	int start_col,start_row;
	int goal_col,goal_row;

	scanf("%d %d",&start_col,&start_row);
	start = index_table[start_row][start_col];

	scanf("%d %d",&goal_col,&goal_row);
	goal = index_table[goal_row][goal_col];

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){

			from_index = index_table[row][col];

			for(int i = 0; i < 4; i++){
				int adj_row = row+diff_row[i];
				int adj_col = col+diff_col[i];
				if(!rangeCheck(adj_row,adj_col))continue;

				to_index = index_table[adj_row][adj_col];
				if(wall[from_index][to_index])continue;

				G[from_index].push_back(to_index);
			}
		}
	}

	calc_rev();
	int first_ans = func();
	for(int i = 0; i < num_nodes; i++){

		MIN_DIST[i] = min_dist[i];
	}

	int ans = 0;

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){

			from_index = index_table[row][col];
			if(MIN_DIST[from_index]+rev_min_dist[from_index] != first_ans)continue; //最短経路上でないならSKIP

			for(int i = 0; i < 4; i++){

				int adj_row = row+diff_row[i];
				int adj_col = col+diff_col[i];

				if(!rangeCheck(adj_row,adj_col))continue;
				to_index = index_table[adj_row][adj_col];

				if(wall[from_index][to_index])continue;
				wall[from_index][to_index] = true;

				int tmp_ans = func();
				if(tmp_ans != BIG_NUM){

					ans = max(ans,tmp_ans-first_ans);
				}

				wall[from_index][to_index] = false;
			}
		}
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 10812
final_rank: 15

code_snippet: #include<bits/stdc++.h>
#define MAX 110
#define inf 1<<28
#define f first
#define s second
#define mp make_pair
using namespace std;
 
int w,h,n,si,sj,gi,gj;
bool grid[MAX][MAX]={};
int Y[4]={1,0,-1,0};
int X[4]={0,1,0,-1};
int d[MAX][MAX];
 
int bfs(){
  for(int i=0;i<MAX;i++){
    for(int j=0;j<MAX;j++)d[i][j]=inf;
  }
  d[si][sj]=0;
  queue<pair<int,int> > q;
  q.push(mp(si,sj));
 
  while(q.size()){
    pair<int,int> u=q.front();
    q.pop();
    if(u.f==gi && u.s==gj)return d[gi][gj];
 
    for(int k=0;k<4;k++){
      int a=u.f+Y[k],b=u.s+X[k];
      if(grid[a][b])continue;
      a+=Y[k];
      b+=X[k];
      if(d[a][b]==inf){
    d[a][b]=d[u.f][u.s]+1;
    q.push(mp(a,b));
      }
    }
  }
  return -1;
}
 
int main()
{
  int a,b,c,d;
  
  cin>>w>>h>>n;
 
  for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;
  for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;
 
  for(int i=0;i<n;i++){
    cin>>a>>b>>c>>d;
    for(int j=min(a,c)*2;j<=max(a,c)*2;j++){
      for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;
    }
  }
  cin>>sj>>si>>gj>>gi;
  si=si*2+1;
  sj=sj*2+1;
  gi=gi*2+1;
  gj=gj*2+1;
 
  int dis=bfs(),ans=0;
 
  for(int i=2;i<=h*2-2;i+=2){
    for(int j=1;j<=w*2-1;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  for(int i=1;i<=h*2-1;i+=2){
    for(int j=2;j<=w*2-2;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  cout<<ans<<endl;  
 
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3160
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long

struct P {
  int x, y;
  P(){}
  P(int x, int y):x(x), y(y){}
  bool operator == (const P &p) const {
    return x == p.x && y == p.y;
  }
  bool operator < (const P &p) const {
    return x != p.x ? x < p.x : y < p.y;
  }
};

P min(P p, P q) {
  return p < q ? p : q;
}

int W, H, N;
P ip, op;

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

bool wall[55][55][55][55];

bool in(int y, int x) {
  return 0<=y&&y<H&&0<=x&&x<W;
}

int bfs() {
  queue<P> que;
  int dist[55][55];
  memset(dist, -1, sizeof(dist));
  que.emplace(ip);
  dist[ip.y][ip.x] = 0;
  while(!que.empty()) {
    P p = que.front(); que.pop();
    //cout << p.x << " " << p.y << ":"<< dist[p.y][p.x]<<endl; 
    if(p == op) break;   
    for(int i = 0; i < 4; i++) {
      int ny = p.y+dy[i], nx = p.x+dx[i];
      if(!in(ny, nx)) continue;
      if(dist[ny][nx] != -1) continue;
      if((i == 0 && !wall[ny][nx][ny][nx+1]) ||
	 (i == 1 && !wall[ny][nx][ny+1][nx]) ||
	 (i == 2 && !wall[p.y][p.x][p.y][p.x+1]) ||
	 (i == 3 && !wall[p.y][p.x][p.y+1][p.x])) {
	que.emplace(nx, ny);
	dist[ny][nx] = dist[p.y][p.x]+1;
	//cout<<ny<<" "<<nx<<" "<<dist[ny][nx]<<endl;
	//cout<<"          "<<dist[p.y][p.x]+1<<endl;
      }
    }
  }
  //cout<<dist[op.y][op.x]<<endl;
  return dist[op.y][op.x];
}

void print(){
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cout<<wall[i][j][i][j+1]<<" "<<wall[i][j][i+1][j]<<endl;
    }
  }
  cout<<endl;
}

signed main(){
  cin >> W >> H >> N;
  for(int i = 0; i < N; i++) {
    P s, d;
    cin >> s.x >> s.y >> d.x >> d.y;
    P p = min(s, d);
    P q = p == s ? d : s;
    if(p.y == q.y) {
      for(int x = p.x; x < q.x; x++) wall[p.y][x][p.y][x+1] = true;
    } else if(p.x == q.x) {
      for(int y = p.y; y < q.y; y++) wall[y][p.x][y+1][p.x] = true;      
    } else {
      assert(false);
    }
  }

  cin >> ip.x >> ip.y >> op.x >> op.y;

  int ini = bfs(), ans = ini;
  
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      if(in(i+1, j) && !wall[i][j][i+1][j]) {
	wall[i][j][i+1][j] = true;
	//print();
	ans = max(ans, bfs());
	wall[i][j][i+1][j] = false;
      }
      if(in(i, j+1) && !wall[i][j][i][j+1]) {
	wall[i][j][i][j+1] = true;
	//print();
	ans = max(ans, bfs());
	wall[i][j][i][j+1] = false;
      }
    }
  }
  
  //cout << ans << " " << ini << endl;
  cout << ans - ini << endl;
  
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 10904
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>  P;
int w,h,n,ix,iy,ox,oy;

bool kab[60][60][4];

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int cal(){
  queue<P> q;

  int cost[60][60];

  for(int i=0;i<60;i++)
    for(int j=0;j<60;j++)cost[i][j]=1e9;
  cost[iy][ix]=0;
  q.push(P(ix,iy));
  
  while(!q.empty()){
    int x=q.front().first,y=q.front().second;
    q.pop();
    if(x==ox&&y==oy)return cost[y][x];
    
    for(int i=0;i<4;i++){
      int nx=x+dx[i],ny=y+dy[i];
      if(nx<0||w<=nx||ny<0||h<=ny||kab[y][x][i]||cost[ny][nx]!=1e9)continue;
      q.push(P(nx,ny));
      cost[ny][nx]=cost[y][x]+1;
    }
      
  }
  
  return 0;
}


int main(){
  cin>>w>>h>>n;
  for(int i=0;i<n;i++){
    int sx,sy,ddx,ddy;
    cin>>sx>>sy>>ddx>>ddy;

    if(sx==ddx)
      for(int i=min(sy,ddy);i<max(ddy,sy);i++){
	if(0<=sx-1)kab[i][sx-1][0]=1;
	if(sx<w)kab[i][sx][2]=1;
      }
    else
      for(int i=min(sx,ddx);i<max(ddx,sx);i++){
	if(0<=sy-1)kab[sy-1][i][1]=1;
	if(sy<h)kab[sy][i][3]=1;
      }
  }
  cin>>ix>>iy>>ox>>oy;
  
    for(int i=0;i<w;i++){
      kab[0][i][3]=1;
      kab[h-1][i][1]=1;
    }
    
    for(int i=0;i<h;i++){
      kab[i][0][2]=1;
      kab[i][w-1][0]=1;
    }		  

    int d=cal(),ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	for(int k=0;k<4;k++){
	  if(kab[i][j][k])continue;
	  kab[i][j][k]=1;
	  ans=max(ans,cal()-d);
	  kab[i][j][k]=0;
      }    
  
  cout<<ans<<endl;

  return 0;  
}
accuracy: 1
time_ms: 150
memory_kb: 3124
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define MAX 110
#define inf 1<<28
#define f first
#define s second
#define mp make_pair
using namespace std;

int w,h,n,si,sj,gi,gj;
bool grid[MAX][MAX]={};
int Y[4]={1,0,-1,0};
int X[4]={0,1,0,-1};
int d[MAX][MAX];

int bfs(){
  for(int i=0;i<MAX;i++){
    for(int j=0;j<MAX;j++)d[i][j]=inf;
  }
  d[si][sj]=0;
  queue<pair<int,int> > q;
  q.push(mp(si,sj));

  while(q.size()){
    pair<int,int> u=q.front();
    q.pop();
    if(u.f==gi && u.s==gj)return d[gi][gj];

    for(int k=0;k<4;k++){
      int a=u.f+Y[k],b=u.s+X[k];
      if(grid[a][b])continue;
      a+=Y[k];
      b+=X[k];
      if(d[a][b]==inf){
	d[a][b]=d[u.f][u.s]+1;
	q.push(mp(a,b));
      }
    }
  }
  return -1;
}

int main()
{
  int a,b,c,d;
 
  cin>>w>>h>>n;

  for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;
  for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;

  for(int i=0;i<n;i++){
    cin>>a>>b>>c>>d;
    for(int j=min(a,c)*2;j<=max(a,c)*2;j++){
      for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;
    }
  }
  cin>>sj>>si>>gj>>gi;
  si=si*2+1;
  sj=sj*2+1;
  gi=gi*2+1;
  gj=gj*2+1;

  int dis=bfs(),ans=0;

  for(int i=2;i<=h*2-2;i+=2){
    for(int j=1;j<=w*2-1;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  for(int i=1;i<=h*2-1;i+=2){
    for(int j=2;j<=w*2-2;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  cout<<ans<<endl;  

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1276
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define MAX 110
#define inf 1<<28
#define f first
#define s second
#define mp make_pair
using namespace std;

int w,h,n,si,sj,gi,gj;
bool grid[MAX][MAX]={};
int Y[4]={1,0,-1,0};
int X[4]={0,1,0,-1};

int bfs(){
  int d[MAX][MAX];
  for(int i=0;i<MAX;i++){
    for(int j=0;j<MAX;j++)d[i][j]=inf;
  }
  d[si][sj]=0;
  queue<pair<int,int> > q;
  q.push(mp(si,sj));

  while(q.size()){
    pair<int,int> u=q.front();
    q.pop();
    if(u.f==gi && u.s==gj)return d[gi][gj];

    for(int k=0;k<4;k++){
      int a=u.f+Y[k],b=u.s+X[k];
      if(grid[a][b])continue;
      a+=Y[k];
      b+=X[k];
      if(d[u.f][u.s]+1<d[a][b]){
	d[a][b]=d[u.f][u.s]+1;
	q.push(mp(a,b));
      }
    }
  }
  return -1;
}

int main()
{
  int a,b,c,d;
 
  cin>>w>>h>>n;

  for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;
  for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;

  for(int i=0;i<n;i++){
    cin>>a>>b>>c>>d;
    for(int j=min(a,c)*2;j<=max(a,c)*2;j++){
      for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;
    }
  }
  cin>>sj>>si>>gj>>gi;
  si=si*2+1;
  sj=sj*2+1;
  gi=gi*2+1;
  gj=gj*2+1;

  int dis=bfs(),ans=0;

  for(int i=2;i<=h*2-2;i+=2){
    for(int j=1;j<=w*2-1;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  for(int i=1;i<=h*2-1;i+=2){
    for(int j=2;j<=w*2-2;j+=2){
      if(grid[i][j])continue;
      grid[i][j]=true;
      ans=max(bfs()-dis,ans);
      grid[i][j]=false;
    }
  }
  cout<<ans<<endl;  

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1272
final_rank: 4

code_snippet: #include <bits/stdc++.h>
  
using namespace std;
  
#define MAX_H 60
#define MAX_W 60
#define INF 1e9
  
int H,W,N,ix,iy,ox,oy;
bool wall[MAX_H][MAX_W][4],a[MAX_H][MAX_W][4];
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,-1,0,1};
  
struct P{
    int x,y;
    P(int x,int y) : x(x),y(y) {}
};
  
inline bool inField(int x,int y){
    return (0 <= x && x < W && 0 <= y && y < H);
}
  
int bfs(){
    int d[MAX_H][MAX_W];
    for(int i = 0 ; i < H ; i++){
	for(int j = 0 ; j < W ; j++){
	    d[i][j] = INF;
	}
    }
    d[iy][ix] = 0;
    queue<P> Q;
    Q.push(P(ix,iy));
    queue<int> step;
    step.push(0);
  
    while(!Q.empty()){
	P p = Q.front(); Q.pop();
	int s = step.front(); step.pop();
	int x = p.x, y = p.y;
	if(x == ox && y == oy) return d[oy][ox];
	for(int i = 0 ; i < 4 ; i++){
	    int nx = x + dx[i], ny = y + dy[i];
	    if(wall[y][x][i]) continue;
	    if(!inField(nx,ny)) continue;
	    if(d[y][x]+1 < d[ny][nx]){
		d[ny][nx] = d[y][x]+1;
		Q.push(P(nx,ny));
		step.push(s+1);
	    }
	}
    }
    return -1;
}
  
int main(){
    cin >> W >> H >> N;
    for(int i = 0 ; i < N ; i++){
	int sx,sy,dx,dy;
	cin >> sx >> sy >> dx >> dy;
	if(sx > dx) swap(sx,dx);
	if(sy > dy) swap(sy,dy);
	bool t = false,y = false;
	if(sx != dx) y = true;
	if(sy != dy) t = true;
	if(y){
	    for(int j = sx ; j < dx ; j++){
		for(int k = sy ; k <= dy ; k++){
		    if(k-1 >= 0){
			wall[k-1][j][3] = true;
			a[k-1][j][3] = true;
		    }
		    wall[k][j][1] = true;
		    a[k][j][1] = true;
		}
	    }
	}
	if(t){
	    for(int j = sx ; j <= dx ; j++){
		for(int k = sy ; k < dy ; k++){
		    if(j-1 >= 0){
			wall[k][j-1][2] = true;
			a[k][j-1][2] = true;
		    }
		    wall[k][j][0] = true;
		    a[k][j][0] = true;
		}
	    }
	}
    }
    int res = 0;
    cin >> ix >> iy >> ox >> oy;
    int dist = bfs();
    for(int x = 0 ; x <= W+1 ; x++){
	for(int y = 0 ; y <= H+1 ; y++){
	    int xx = x-1,yy = y;
	    if(xx >= 0) wall[yy][xx][2] = wall[y][x][0] = true;
	    res = max(res,bfs()-dist);
	    if(xx >= 0){
		wall[yy][xx][2] = a[yy][xx][2];
		wall[y][x][0] = a[y][x][0];
	    }
	}
    }
    for(int x = 0 ; x <= W+1 ; x++){
	for(int y = 0 ; y <= H+1 ; y++){
	    int xx = x,yy = y-1;
	    if(yy >= 0) wall[yy][xx][3] = wall[y][x][1] = true;
	    res = max(res,bfs()-dist);
	    if(yy >= 0){
		wall[yy][xx][3] = a[yy][xx][3];
		wall[y][x][1] = a[y][x][1];
	    }
	}
    }
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3172
final_rank: 11

code_snippet: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef pair<int,int> P;
int w,h,n;
int fie[101][101];
int dist[101][101];
int ix,iy,ox,oy;
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};

int bfs(){
	queue<P> que;
	memset(dist,-1,sizeof(dist));
	que.push(P(ix,iy));
	dist[iy][ix]=0;
	while(que.size()){
		P q=que.front();
		que.pop();
		for(int i=0;i<4;i++){
			int nx=q.first+dx[i],ny=q.second+dy[i];
			if(fie[ny][nx]==0){
				nx+=dx[i];
				ny+=dy[i];
				if(dist[ny][nx]!=-1)continue;
				dist[ny][nx]=dist[q.second][q.first]+1;
				que.push(P(nx,ny));
			}
		}
	}
	return dist[oy][ox];
}

int main(void){
	scanf("%d %d %d",&w,&h,&n);
	w*=2;
	h*=2;
	for(int i=0;i<=h;i++){
		fie[i][0]=1;
		fie[i][w]=1;
	}
	for(int i=0;i<=w;i++){
		fie[0][i]=1;
		fie[h][i]=1;
	}
	for(int i=0;i<n;i++){
		int sx,sy,ddx,ddy;
		scanf("%d %d %d %d",&sx,&sy,&ddx,&ddy);
		if(sx>ddx)swap(sx,ddx);
		if(sy>ddy)swap(sy,ddy);
		sx*=2;
		sy*=2;
		ddx*=2;
		ddy*=2;
		for(int j=sx;j<=ddx;j++){
			fie[ddy][j]=1;
			fie[sy][j]=1;
		}
		for(int j=sy;j<=ddy;j++){
			fie[j][ddx]=1;
			fie[j][sx]=1;
		}
	}
	scanf("%d%d",&ix,&iy);
	scanf("%d%d",&ox,&oy);
	ix=ix*2+1;
	iy=iy*2+1;
	ox=ox*2+1;
	oy=oy*2+1;
	int prev=bfs();
	int res=prev;
	for(int i=1;i<h;i+=2){
		for(int j=0;j<w;j+=2){
			if(fie[i][j]==0){
				fie[i][j]=1;
				int di=bfs();
				if(di!=-1)res=max(di,res);
				fie[i][j]=0;
			}
		}
	}
	for(int i=0;i<h;i+=2){
		for(int j=1;j<w;j+=2){
			if(fie[i][j]==0){
				fie[i][j]=1;
				int di=bfs();
				if(di!=-1)res=max(di,res);
				fie[i][j]=0;
			}
		}
	}
	printf("%d\n",res-prev);
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1136
final_rank: 1

code_snippet: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef pair<int,int> P;
int w,h,n;
int fie[101][101];
int dist[101][101];
int ix,iy,ox,oy;
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};

int bfs(){
	queue<P> que;
	memset(dist,-1,sizeof(dist));
	que.push(P(ix,iy));
	dist[iy][ix]=0;
	while(que.size()){
		P q=que.front();
		que.pop();
		for(int i=0;i<4;i++){
			int nx=q.first+dx[i],ny=q.second+dy[i];
			if(fie[ny][nx]==0){
				nx+=dx[i];
				ny+=dy[i];
				if(dist[ny][nx]!=-1)continue;
				dist[ny][nx]=dist[q.second][q.first]+1;
				que.push(P(nx,ny));
			}
		}
	}
	return dist[oy][ox];
}

int main(void){
	scanf("%d %d %d",&w,&h,&n);
	w*=2;
	h*=2;
	for(int i=0;i<=h;i++){
		fie[i][0]=1;
		fie[i][w]=1;
	}
	for(int i=0;i<=w;i++){
		fie[0][i]=1;
		fie[h][i]=1;
	}
	for(int i=0;i<n;i++){
		int sx,sy,ddx,ddy;
		scanf("%d %d %d %d",&sx,&sy,&ddx,&ddy);
		if(sx>ddx)swap(sx,ddx);
		if(sy>ddy)swap(sy,ddy);
		sx*=2;
		sy*=2;
		ddx*=2;
		ddy*=2;
		for(int j=sx;j<=ddx;j++){
			fie[ddy][j]=1;
			fie[sy][j]=1;
		}
		for(int j=sy;j<=ddy;j++){
			fie[j][ddx]=1;
			fie[j][sx]=1;
		}
	}
	scanf("%d%d",&ix,&iy);
	scanf("%d%d",&ox,&oy);
	ix=ix*2+1;
	iy=iy*2+1;
	ox=ox*2+1;
	oy=oy*2+1;
	int prev=bfs();
	int res=0;
	for(int i=1;i<h;i+=2){
		for(int j=0;j<w;j+=2){
			if(fie[i][j]==0){
				fie[i][j]=1;
				int di=bfs();
				if(di!=-1)res=max(di,res);
				fie[i][j]=0;
			}
		}
	}
	for(int i=0;i<h;i+=2){
		for(int j=1;j<w;j+=2){
			if(fie[i][j]==0){
				fie[i][j]=1;
				int di=bfs();
				if(di!=-1)res=max(di,res);
				fie[i][j]=0;
			}
		}
	}
	printf("%d\n",res-prev);
	return 0;
}
accuracy: 0.8461538461538461
time_ms: 60
memory_kb: 1136
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;

int walls[55][55][4];

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };

struct aa {
	int x;
	int y;
	int time;
	vector<int>xs;
	vector<int>ys;
};
class Compare {
public:
	//aaが昇順に並ぶ
	bool operator()(const aa&l, const aa&r) {
		return l.time> r.time;
	}
};
aa getway(const int fx, const int fy, const int tx, const int ty) {
	priority_queue<aa, vector<aa>, Compare>que;
	que.push(aa{ fx,fy,0,{},{} });
	int memo[100][100];
	for (int i = 0; i < 100; ++i) {
		for (int j = 0; j < 100; ++j) {
			memo[i][j] = 99999999;
		}
	}
	memo[fy][fx] = 0;
	while (!que.empty()) {
		aa atop(que.top());
		que.pop();
		if (atop.x == tx&&atop.y == ty) {
			return atop;
		}
		else {
			atop.xs.push_back(atop.x);
			atop.ys.push_back(atop.y);
		}
		vector<int> axs(atop.xs);
		vector<int> ays(atop.ys);
		for (int i = 0; i < 4; ++i) {
			int newx = atop.x + dx[i];
			int newy = atop.y + dy[i];
			if (!walls[atop.y][atop.x][i]) {
				if (memo[newy][newx]>atop.time + 1) {
					memo[newy][newx] = atop.time + 1;
					que.push(aa{ newx,newy,atop.time + 1,atop.xs,atop.ys });
				}
			}
		}
	}
	return aa{ -10000,0,0,{},{} };
}

int main() {
	int W, H,N; cin >> W >> H >> N;
	for (int i = 0; i < N; ++i) {
		int sx, sy, dx, dy; cin >> sx >> sy >> dx >> dy;
		sy = H - sy;
		dy = H - dy;
		if (sx == dx) {
			if (sy > dy)swap(sy, dy);
			for (int y = sy; y < dy; ++y) {
				if(sx-1>=0)walls[y][sx - 1][2] = true;
				if (sx < W)walls[y][sx][0] = true;
			}
		}
		else {
			if (sx > dx)swap(sx, dx);
			for (int x = sx; x < dx; ++x) {
				if(sy-1>=0)walls[sy-1][x][1] = true;
				if(sy<H)walls[sy][x][3] = true;
			}
		}
	}
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			for (int k = 0; k < 4; ++k) {
				if (i + dy[k] < 0 || i + dy[k] >= H) {
					walls[i][j][k] = true;
				}
				if (j + dx[k] < 0 || j + dx[k] >= W) {
					walls[i][j][k] = true;

				}
			}
		}
	}
	int startx, starty, goalx, goaly; cin >> startx >> starty >> goalx >> goaly;
	starty = H - starty - 1;
	goaly = H - goaly - 1;

	aa fstway =getway(startx, starty, goalx, goaly);
	fstway.xs.push_back(goalx);
	fstway.ys.push_back(goaly);
	int fsttime = fstway.time;
	int ans = 0;
	if (!fstway.xs.empty()) {
		for (int i = 0; i < fstway.xs.size() - 1; ++i) {
			for (int j = 0; j < 4; ++j) {
				if (fstway.xs[i] + dx[j] == fstway.xs[i + 1] && fstway.ys[i] + dy[j] == fstway.ys[i + 1]) {
					walls[fstway.ys[i]][fstway.xs[i]][j] = true;
					walls[fstway.ys[i + 1]][fstway.xs[i + 1]][(2 + j) % 4] = true;
					aa nowway(getway(startx, starty, goalx, goaly));
					if (nowway.time) {
						ans = max(ans, nowway.time - fsttime);
					}
					walls[fstway.ys[i]][fstway.xs[i]][j] = false;
					walls[fstway.ys[i + 1]][fstway.xs[i + 1]][(2 + j) % 4] = false;
				}
			}
		}
	}
	
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 2510
memory_kb: 3244
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
	EACH(x,vec) is >> x;
	return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
	for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
		if (i) os << " ";
		os << get<0>(t);
	}
	return os;
}
*/
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
	REP(i,vec.size()) {
		if (i) os << " ";
		os << vec[i];
	}
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
	REP(i,vec.size()) {
		if (i) os << endl;
		os << vec[i];
	}
	return os;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int wall[51][51][51][51] = {0};
int W, H;
bool inRange(int x, int y) {
	return 0 <= x && x < W && 0 <= y && y < H;
}
bool inRange(P p) {
	int x, y; tie(x, y) = p;
	return inRange(x, y);
}
ll bfs(P s, P t) {
	ll dist[51][51]; fill(dist[0], dist[51], linf); dist[s.second][s.first] = 0;
	queue<P> Q; Q.push(s);
	while ( !Q.empty() ) {
		P p = Q.front(); Q.pop();
		int x, y; tie(x, y) = p;
		REP(d, 4) {
			int nx = x + dx[d], ny = y + dy[d];
			if ( !inRange(nx, ny) ) continue;
			if ( wall[x][y][nx][ny] ) continue;
			if ( dist[y][x]+1 < dist[ny][nx] ) {
				dist[ny][nx] = dist[y][x]+1;
				Q.push(P(nx, ny));
			}
		}
	}
	return dist[t.second][t.first];
}
P operator-(const P& p1, const P& p2) {
	return P(p1.first-p2.first, p1.second-p2.second);
}
P operator+(const P& p1, const P& p2) {
	return P(p1.first+p2.first, p1.second+p2.second);
}
P operator/(const P& p, const int& d) {
	return P(p.first/d, p.second/d);
}
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N; cin >> W >> H >> N;
	REP(i, N) {
		int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
		P p1(x1, y1), p2(x2, y2);
		P d = p2-p1;
		d = d / (abs(d.first) + abs(d.second));
		assert( abs(d.first) + abs(d.second) == 1 );
		P p = p1;
		while (1) {
			map<P, int> m;
			FOR(x, -1, 1) FOR(y, -1, 1) {
				P p1(p.first+x, p.second+y);
				P p2(p.first+d.first+x, p.second+d.second+y);
				m[p1]++;
				m[p2]++;
			}
			vector<P> v;
			EACH(p, m) if (p.second == 2) v.pb(p.first);
			assert( v.size() == 2 );
			if (inRange(v[0]) && inRange(v[1])) {
				wall[v[0].first][v[0].second][v[1].first][v[1].second]++;
				wall[v[1].first][v[1].second][v[0].first][v[0].second]++;
			}
			p = p + d;
			if (p == p2) break;
		}
	}
	int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
	ll base = bfs(P(sx, sy), P(gx, gy));
	ll ans = 0;
	REP(y, H) REP(x, W) REP(d, 4) {
		int nx = x+dx[d], ny = y+dy[d];
		if ( !inRange(nx, ny) ) continue;
		++wall[x][y][nx][ny];
		++wall[nx][ny][x][y];
		ll step = bfs(P(sx, sy), P(gx, gy));
//		cout << x << " " << y << " " << nx << " " << ny << " " << step << endl;
		if (step != linf) {
			ans = max(ans, step-base);
		}
		--wall[x][y][nx][ny];
		--wall[nx][ny][x][y];
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 340
memory_kb: 14164
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int W, H, N, ix, iy, ox, oy;
bool movable[55][55][4];

const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};

bool in_range(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

struct State { int x, y; };

int step[55][55];

int bfs() {
	memset(step, -1, sizeof(step));
	
	queue< State > que;
	que.push(State{ix, iy});
	
	while (!que.empty()) {
		State s = que.front(); que.pop();
		
		for_(d,0,4) {
			if (!movable[s.y][s.x][d]) continue;
			
			int nx = s.x + dx[d], ny = s.y + dy[d];
			
			if (!in_range(nx, ny)) continue;
			
			if (step[ny][nx] == -1) {
				step[ny][nx] = step[s.y][s.x] + 1;
				que.push(State{nx, ny});
			}
		}
	}
	
	return step[oy][ox];
}

void solve() {
	int ini = bfs(), ans = 0;
	
	for_(y,0,H) for_(x,0,W) {
		if (x > 0 && movable[y][x][3]) {
			movable[y][x][3] = movable[y][x - 1][1] = false;
			ans = max(ans, bfs() - ini);
			movable[y][x][3] = movable[y][x - 1][1] = true;
		}
		
		if (y > 0 && movable[y][x][2]) {
			movable[y][x][2] = movable[y - 1][x][0] = false;
			ans = max(ans, bfs() - ini);
			movable[y][x][2] = movable[y - 1][x][0] = true;
		}
	}
	
	cout << ans << endl;
}

int main() {
	cin >> W >> H >> N;
	
	for_(y,0,H) for_(x,0,W) for_(d,0,4) movable[y][x][d] = true;
	
	for_(i,0,N) {
		int sx, sy, ex, ey;
		cin >> sx >> sy >> ex >> ey;
		
		if (sx > ex) swap(sx, ex);
		if (sy > ey) swap(sy, ey);
		
		if (sy > 0) for_(x,sx,ex) movable[sy][x][2] = movable[sy - 1][x][0] = false;
		if (sx > 0) for_(y,sy,ey) movable[y][sx][3] = movable[y][sx - 1][1] = false;
	}
	
	cin >> ix >> iy;
	cin >> ox >> oy;
	
	solve();
}
accuracy: 1
time_ms: 50
memory_kb: 1244
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
const int ddx[] = {1,1,0,1}, ddy[] = {1,0,1,1};

bool hwall[51][51], vwall[51][51];
int W, H;
int ix, iy, ox, oy;

int min_steps() {
    queue<pii> que;
    que.push({ix, iy});
    pii sentry = {-1, -1};
    que.push(sentry);
    bool G[50][50] = {};
    G[iy][ix] = true;
    int steps = 0;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (p == sentry) {
            steps++;
            if (que.empty()) {
                return -1; // unreachable
            }
            que.push(sentry);
            continue;
        }
        if (x == ox && y == oy) {
            return steps;
        }

        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (!G[sy][sx]) {
                    G[sy][sx] = true;
                    que.push({sx, sy});
                }
            }
        }
    }
}

int max_steps() {
    queue<pii> que;
    que.push({ix, iy});
    // ????????????????????????
    // ??´????????????cost-1???????????????????????¨?????§?????????????????°??§????????????????±????????????¨?????§??????
    int G[50][50];
    fill_n((int *)G, 50*50, -1);
    G[iy][ix] = 0;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (x == ox && y == oy) break;

        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (G[sy][sx] == -1) {
                    G[sy][sx] = G[y][x] + 1;
                    que.push({sx, sy});
                }
            }
        }
    }

//    for (int y = H-1; y >= 0; y--) {
//        REP(x, W) {
//            if (G[y][x] == -1) {
//                LOG("#");
//            } else {
//                if (G[y][x] > (0x7e - 0x30)) {
//                    LOG("$");
//                } else {
//                    LOG("%c", '0' + G[y][x]);
//                }
//            }
//        }
//        LOG("\n");
//    }
//    LOG("min: %d\n", G[oy][ox]);

    // ?????????
    bool intersection2[50][50] = {};
    vector<pii> intersection;
    intersection.push_back({ix, iy});
    intersection.push_back({ox, oy});

    while (!que.empty()) que.pop();
    que.push({ox, oy});
    bool G2[50][50] = {};
    G2[oy][ox] = true;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (x == ix && y == iy) break;
        int inter = 0;
        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (G[sy][sx] == G[y][x] - 1 || G[sy][sx] == G[y][x] + 1) {
                    inter++;
                }
                if (!G2[sy][sx] && G[sy][sx] == G[y][x] - 1) {
                    G2[sy][sx] = true;
                    que.push({sx, sy});
                }
            }
        }
        if (inter > 1) {
            intersection2[y][x] = true;
            intersection.push_back({x, y});
        }
    }

//    for (int y = H; y >= 1; y--) {
//        FOR(x, 1, W+1) {
//            LOG("%c+", hwall[y][x] ? '-' : ' ');
//        }
//        LOG("\n");
//        FOR(x, 1, W) {
//            if (intersection2[y-1][x-1]) {
//                LOG("#");
//            } else if (G2[y-1][x-1]) {
//                LOG(".");
//            } else {
//                LOG(" ");
//            }
//            LOG("%c", vwall[y][x] ? '|' : ' ');
//        }
//        LOG("\n");
//    }

    int ma = G[oy][ox];
    for (pii p : intersection) {
        int x = p.first, y = p.second;
        REP(i, 4) {
            if (i % 2 == 0 && !vwall[y+ddy[i]][x+ddx[i]]) {
                vwall[y+ddy[i]][x+ddx[i]] = true;
                ma = max(ma, min_steps());
                vwall[y+ddy[i]][x+ddx[i]] = false;
            }
            if (i % 2 == 1 && !hwall[y+ddy[i]][x+ddx[i]]) {
                hwall[y+ddy[i]][x+ddx[i]] = true;
                ma = max(ma, min_steps());
                hwall[y+ddy[i]][x+ddx[i]] = false;
            }
        }
    }
//    LOG("max: %d\n", ma);
    return ma - G[oy][ox];
}

int main() {
    int n;
    cin >> W >> H >> n;
    REP(i, n) {
        int sx, sy, dx, dy;
        cin >> sx >> sy >> dx >> dy;
        while (sx != dx) {
            if (sx < dx) {
                sx++;
                hwall[sy][sx] = true;
            } else if (sx > dx) {
                sx--;
                hwall[sy][sx+1] = true;
            }
        }
        while (sy != dy) {
            if (sy < dy) {
                sy++;
                vwall[sy][sx] = true;
            } else if (sy > dy) {
                sy--;
                vwall[sy+1][sx] = true;
            }
        }
    }
    cin >> ix >> iy >> ox >> oy;

//    for (int y = H+1; y >= 1; y--) {
//        FOR(x, 1, W+1) {
//            LOG("%c+", hwall[y][x] ? '-' : ' ');
//        }
//        LOG("\n");
//        FOR(x, 1, W+1) {
//            if (x == ix+1 && y == iy+1) {
//                LOG("S");
//            } else if (x == ox+1 && y == oy+1) {
//                LOG("G");
//            } else {
//                LOG(" ");
//            }
//            LOG("%c", vwall[y][x] ? '|' : ' ');
//        }
//        LOG("\n");
//    }

    cout << max_steps() << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 3184
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
const int ddx[] = {1,1,0,1}, ddy[] = {1,0,1,1};

bool hwall[51][51], vwall[51][51];
int W, H;
int ix, iy, ox, oy;

int min_steps() {
    queue<pii> que;
    que.push({ix, iy});
    pii sentry = {-1, -1};
    que.push(sentry);
    bool G[50][50] = {};
    G[iy][ix] = true;
    int steps = 0;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (p == sentry) {
            steps++;
            if (que.empty()) {
                return -1; // unreachable
            }
            que.push(sentry);
            continue;
        }
        if (x == ox && y == oy) {
            return steps;
        }

        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (!G[sy][sx]) {
                    G[sy][sx] = true;
                    que.push({sx, sy});
                }
            }
        }
    }
}

int max_steps() {
    queue<pii> que;
    que.push({ix, iy});
    // ????????????????????????
    // ??´????????????cost-1???????????????????????¨?????§?????????????????°??§????????????????±????????????¨?????§??????
    int G[50][50];
    fill_n((int *)G, 50*50, -1);
    G[iy][ix] = 0;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (x == ox && y == oy) break;

        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;
                if (G[sy][sx] == -1) {
                    G[sy][sx] = G[y][x] + 1;
                    que.push({sx, sy});
                }
            }
        }
    }

//    for (int y = H-1; y >= 0; y--) {
//        REP(x, W) {
//            if (G[y][x] == -1) {
//                LOG("#");
//            } else {
//                if (G[y][x] > (0x7e - 0x30)) {
//                    LOG("$");
//                } else {
//                    LOG("%c", '0' + G[y][x]);
//                }
//            }
//        }
//        LOG("\n");
//    }
//    LOG("min: %d\n", G[oy][ox]);

    // ?????????
    bool intersection2[50][50] = {};
    vector<pii> intersection;
    intersection.push_back({ix, iy});
    intersection.push_back({ox, oy});

    while (!que.empty()) que.pop();
    que.push({ox, oy});
    bool G2[50][50] = {};
    G2[oy][ox] = true;
    while (!que.empty()) {
        pii p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if (x == ix && y == iy) break;
        int inter = 0;
        REP(i, 4) {
            int sx = x + dx[i], sy = y + dy[i];
            if (0 <= sx && sx < W && 0 <= sy && sy < H) {
                if (G[sy][sx] == G[y][x] - 1 || G[sy][sx] == G[y][x] + 1) {
                    inter++;
                }
                if (!G2[sy][sx] && G[sy][sx] == G[y][x] - 1) {
                    G2[sy][sx] = true;
                    que.push({sx, sy});
                }
            }
        }
        if (inter > 1) {
            intersection2[y][x] = true;
            intersection.push_back({x, y});
        }
    }

//    for (int y = H; y >= 1; y--) {
//        FOR(x, 1, W+1) {
//            LOG("%c+", hwall[y][x] ? '-' : ' ');
//        }
//        LOG("\n");
//        FOR(x, 1, W) {
//            if (intersection2[y-1][x-1]) {
//                LOG("#");
//            } else if (G2[y-1][x-1]) {
//                LOG(".");
//            } else {
//                LOG(" ");
//            }
//            LOG("%c", vwall[y][x] ? '|' : ' ');
//        }
//        LOG("\n");
//    }

    int ma = 0;
    for (pii p : intersection) {
        int x = p.first, y = p.second;
        REP(i, 4) {
            if (i % 2 == 0 && !vwall[y+ddy[i]][x+ddx[i]]) {
                vwall[y+ddy[i]][x+ddx[i]] = true;
                ma = max(ma, min_steps());
                vwall[y+ddy[i]][x+ddx[i]] = false;
            }
            if (i % 2 == 1 && !hwall[y+ddy[i]][x+ddx[i]]) {
                hwall[y+ddy[i]][x+ddx[i]] = true;
                ma = max(ma, min_steps());
                hwall[y+ddy[i]][x+ddx[i]] = false;
            }
        }
    }
//    LOG("max: %d\n", ma);
    return ma - G[oy][ox];
}

int main() {
    int n;
    cin >> W >> H >> n;
    REP(i, n) {
        int sx, sy, dx, dy;
        cin >> sx >> sy >> dx >> dy;
        while (sx != dx) {
            if (sx < dx) {
                sx++;
                hwall[sy][sx] = true;
            } else if (sx > dx) {
                sx--;
                hwall[sy][sx+1] = true;
            }
        }
        while (sy != dy) {
            if (sy < dy) {
                sy++;
                vwall[sy][sx] = true;
            } else if (sy > dy) {
                sy--;
                vwall[sy+1][sx] = true;
            }
        }
    }
    cin >> ix >> iy >> ox >> oy;

//    for (int y = H+1; y >= 1; y--) {
//        FOR(x, 1, W+1) {
//            LOG("%c+", hwall[y][x] ? '-' : ' ');
//        }
//        LOG("\n");
//        FOR(x, 1, W+1) {
//            if (x == ix+1 && y == iy+1) {
//                LOG("S");
//            } else if (x == ox+1 && y == oy+1) {
//                LOG("G");
//            } else {
//                LOG(" ");
//            }
//            LOG("%c", vwall[y][x] ? '|' : ' ');
//        }
//        LOG("\n");
//    }

    cout << max_steps() << endl;
}
accuracy: 0.6410256410256411
time_ms: 10
memory_kb: 3184
final_rank: 19

code_snippet: #include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<list>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<string>
#define sgn(v) (abs((v))<eps?0:((v)<0?-1:1))
#define sqr(v) ((v)*(v))
#define tri(v) ((v)*(v)*(v))
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
using namespace std;
typedef long long LL;
typedef long double Double;
typedef unsigned long long ULL;
typedef pair<double,int> PDI;
typedef pair<double,double> PDD;
typedef pair<int,int> PII;
typedef pair<int,LL> PIL;
typedef pair<LL,int> PLI;
typedef pair<LL,LL> PLL;
const double eps=1e-8;
const double pi=acos(-1);
const int maxn=3000;
bool mp[55][55][55][55];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
struct Edge
{
	int v,next;
}edge[maxn*4];
int head[maxn],ne;
void add(int u,int v)
{
	edge[ne].v=v;
	edge[ne].next=head[u];
	head[u]=ne++;
}
queue<int> q;
bool inq[maxn];
int ix,iy,ox,oy,w,h;
int dis[maxn];
const int inf=1e9;
void construct(int debug=0)
{
	int i,j,k,x,y;
	memset(head,-1,sizeof(head));
	ne=0;
	for(i=0;i<w;++i)
		for(j=0;j<h;++j)
			for(k=0;k<4;++k)
			{
				x=i+dx[k];
				y=j+dy[k];
				if(x<0||x==w||y<0||y==h)continue;
				//if(debug&&i==0&&j==0&&x==0&&y==1)cout <<"mp:"<<mp[i][j][x][y]<<endl;
				//if(debug&&i==0&&j==0&&x==1&&y==0)cout <<"mp:"<<mp[i][j][x][y]<<endl;
				if(!mp[i][j][x][y])add(i*h+j,x*h+y);
			}
}
int bfs(int debug=0)
{
	int u,v,x,y,i;
	for(i=0;i<w*h;++i)dis[i]=inf;
	u=ix*h+iy;
	dis[u]=0;
	q.push(u);
	inq[u]=true;
	while(!q.empty())
	{
		u=q.front();
		inq[u]=false;
		q.pop();
		x=u/h;
		y=u%h;
		if(debug)
		{
			cout <<x<<" "<<y<<" "<<dis[u]<<endl;getchar();
		}
		for(i=head[u];i!=-1;i=edge[i].next)
		{
			v=edge[i].v;
			if(dis[u]+1<dis[v])
			{
				dis[v]=dis[u]+1;
				if((x!=ox||y!=oy)&&(!inq[v]))
				{
					inq[v]=true;
					q.push(v);
				}
			}
		}
	}
	//if(debug)cout <<ox*h+oy<<endl;
	return dis[ox*h+oy];
}
int main()
{
	int n,i,j,sx,sy,dx,dy;
	scanf("%d%d%d",&w,&h,&n);
	for(j=0;j<n;++j)
	{
		scanf("%d%d%d%d",&sx,&sy,&dx,&dy);
		//cout <<sx<<" "<<sy<<" "<<dx<<" "<<dy<<endl;
		if(sx==dx&&sx>0)
		{
			if(sy>dy)swap(sy,dy);
			for(i=sy;i<dy;++i)
			{
				mp[sx-1][i][sx][i]=true;
				mp[sx][i][sx-1][i]=true;
			}
		}
		else if(sy==dy&&sy>0)
		{
			if(sx>dx)swap(sx,dx);
			for(i=sx;i<dx;++i)
			{
				mp[i][sy-1][i][sy]=true;
				mp[i][sy][i][sy-1]=true;
			}
		}
	}
	//cout <<mp[1][0][1][1]<<endl;
	scanf("%d%d",&ix,&iy);
	scanf("%d%d",&ox,&oy);
	construct();
	int ans=bfs(),maxv,res;
	maxv=ans;
	//cout <<"Here:"<<ans<<endl;
	for(i=1;i<w;++i)
		for(j=0;j<h;++j)
			if(!mp[i-1][j][i][j])
			{
				mp[i-1][j][i][j]=mp[i][j][i-1][j]=true;
				construct();
				res=bfs();
				if(res!=inf)maxv=max(maxv,res);
				mp[i-1][j][i][j]=mp[i][j][i-1][j]=false;
			}
	for(j=1;j<h;++j)
		for(i=0;i<w;++i)
			if(!mp[i][j-1][i][j])
			{
				mp[i][j-1][i][j]=mp[i][j][i][j-1]=true;
				construct();
				res=bfs();
				if(res!=inf)maxv=max(maxv,res);
				mp[i][j-1][i][j]=mp[i][j][i][j-1]=false;
			}
	//cout <<ans<<" "<<maxv<<endl;
	if(maxv==inf)puts("0");
	else printf("%d\n",maxv-ans);
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 9376
final_rank: 13

code_snippet: #include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <cstring>

using namespace std;

#define X first
#define Y second

const int dx[]={0,0,-1,1};
const int dy[]={1,-1,0,0};

vector<pair<int,int> > way;
pair<int,int> fr[52][52],s[52*52],p1,p2,be,ed,cur;
int d[52][52],dir[52][52][5];
bool vis[52][52];
int w,h,n,ans,mindis;

int calc(pair<int,int>	src,pair<int,int> tar){
	int l=1,r=1,cx,cy,ex,ey;
	for	(int i=1;i<=w;++i)	for	(int j=1;j<=h;++j){
		vis[i][j]=0;d[i][j]=w*h+1;
	}
	s[1]=src;vis[src.X][src.Y]=1;d[src.X][src.Y]=0;
	while	(l<=r){
		if	(vis[tar.X][tar.Y])	break;
		cx=s[l].X;cy=s[l].Y;
		//printf("%d %d\n",cx,cy);
		for	(int k=0;k<4;++k)
			if	(dir[cx][cy][k]){
				ex=cx+dx[k];ey=cy+dy[k];
				if	(!vis[ex][ey]){
					vis[ex][ey]=1;
					fr[ex][ey]=make_pair(cx,cy);
					d[ex][ey]=d[cx][cy]+1;
					s[++r]=make_pair(ex,ey);
				}
			}
		l++;
	}
	if	(d[tar.X][tar.Y]>w*h)	return	-1;
	else	return	d[tar.X][tar.Y];
}

int main(){
	while	(~scanf("%d%d%d",&w,&h,&n)){
		for	(int i=1;i<=w;++i)	for	(int j=1;j<=h;++j)
			for	(int k=0;k<4;++k)
				if	(i+dx[k] && i+dx[k]<=w && j+dy[k] && j+dy[k]<=h)	dir[i][j][k]=1;
				else	dir[i][j][k]=0;
		for	(int i=1;i<=n;++i){
			scanf("%d%d%d%d",&p1.X,&p1.Y,&p2.X,&p2.Y);
			if	(p1>p2)	swap(p1,p2);
			if	(p1.X==p2.X){
				for	(int j=p1.Y+1;j<=p2.Y;++j){
					dir[p1.X][j][3]=dir[p1.X+1][j][2]=0;
					//printf("Cutting %d %d %d %d\n",p1.X,j,p1.X+1,j);
				}
			}
			else{
				for	(int j=p1.X+1;j<=p2.X;++j){
					dir[j][p1.Y][0]=dir[j][p1.Y+1][1]=0;
					//printf("Cutting %d %d %d %d\n",j,p1.Y,j,p1.Y+1);
				}
			}
		}
		scanf("%d%d%d%d",&be.X,&be.Y,&ed.X,&ed.Y);
		be.X++;be.Y++;ed.X++;ed.Y++;
		mindis=calc(be,ed);
		//printf("%d\n",mindis);
		cur=ed;way.clear();
		while	(cur!=be){
			way.push_back(cur);
			cur=fr[cur.X][cur.Y];
		}
		way.push_back(be);
		reverse(way.begin(),way.end());
		ans=0;
		for	(int i=0;i<way.size()-1;++i){
			p1=way[i];p2=way[i+1];
			if	(p1>p2)	swap(p1,p2);
			if	(p1.X==p2.X){
				dir[p1.X][p1.Y][0]=dir[p2.X][p2.Y][1]=0;
				ans=max(ans,calc(be,ed)-mindis);
				dir[p1.X][p1.Y][0]=dir[p2.X][p2.Y][1]=1;
			}
			else{
				dir[p1.X][p1.Y][3]=dir[p2.X][p2.Y][2]=0;
				ans=max(ans,calc(be,ed)-mindis);
				dir[p1.X][p1.Y][3]=dir[p2.X][p2.Y][2]=1;
			}
		}
		printf("%d\n",ans);
	}
}
accuracy: 1
time_ms: 20
memory_kb: 1352
final_rank: 2