Problem:
Problem B:
Left Hand Rule
The left-hand rule, which is also known as the wall follower, is a well-known strategy that solves a two-
dimensional maze. The strategy can be stated as follows: once you have entered the maze, walk around
with keeping your left hand in contact with the wall until you reach the goal. In fact, it is proven that this
strategy solves some kind of mazes.
Your task is to write a program that determines whether the given maze is solvable by using the left-hand
rule and (if the maze is solvable) the number of steps to reach the exit. Moving to a cell from the entrance
or the adjacent (north, south, east or west) cell is counted as a step.
In this problem, the maze is represented by a collection of walls placed on the two-dimensional grid.
We use an ordinary Cartesian coordinate system; the positive
x
-axis points right and the positive
y
-axis
points up. Each wall is represented by a line segment which is parallel to the
x
-axis or the
y
-axis, such
that both ends of each wall are located on integer coordinates. The size of the maze is given by
W
and
H
that indicate the width and the height of the maze, respectively. A rectangle whose vertices are on (0, 0),
(
W
, 0), (
W
,
H
) and (0,
H
) forms the outside boundary of the maze. The outside of the maze is always
surrounded by walls except for the entrance of the maze. The entrance is represented by a line segment
whose ends are (
x
E
,
y
E
) and (
x
E
',
y
E
'). The entrance has a unit length and is located somewhere on one
edge of the boundary. The exit is a unit square whose bottom left corner is located on (
x
X
,
y
X
).
A few examples of mazes are illustrated in the figure below. They correspond to the datasets in the sample
input.
Figure 1: Example Mazes (shaded squares indicate the exits)
Input
The input consists of multiple datasets.
Each dataset is formatted as follows:
W H N
x
1
y
1
x
1
'
y
1
'
x
2
y
2
x
2
'
y
2
'
      ...
x
N
y
N
x
N
'
y
N
'
x
E
y
E
x
E
'
y
E
'
x
X
y
X
W
and
H
(0 <
W
,
H
≤ 100) indicate the size of the maze.
N
is the number of walls inside the maze.
The next
N
lines give the positions of the walls, where (
x
i
,
y
i
) and (
x
i
',
y
i
') denote two ends of each wall
(1 ≤
i
≤
N
). The last line gives the positions of the entrance and the exit. You can assume that all the
coordinates given in the input are integer coordinates and located inside the boundary of the maze. You
can also assume that the wall description is not redundant, i.e. an endpoint of a wall is not shared by any
other wall that is parallel to it.
The input is terminated by a line with three zeros.
Output
For each dataset, print a line that contains the number of steps required to reach the exit. If the given
maze is unsolvable, print “
Impossible
” instead of the number of steps.
Sample Input
3 3 3
1 0 1 2
1 2 2 2
2 2 2 1
0 0 1 0 1 1
3 3 4
1 0 1 2
1 2 2 2
2 2 2 1
2 1 1 1
0 0 1 0 1 1
3 3 0
0 0 1 0 1 1
0 0 0
Output for the Sample Input
9
Impossible
Impossible

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>

using namespace std;

const int N = 110;

int w, h, n;
bool wall[N][N][4], used[N][N][4];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
    while(cin >> w >> h >> n) {
        if(w == 0) break;
        for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j) for(int k = 0; k < 4; ++k) wall[i][j][k] = used[i][j][k] = false;
        for(int i = 0; i < h; ++i) {
            wall[i][0][0] = true;
            wall[i][w - 1][2] = true;
        }
        for(int i = 0; i < w; ++i) {
            wall[0][i][1] = true;
            wall[h - 1][i][3] = true;
        }

        for(int i = 0; i < n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if(x1 == x2) {
                for(int y = min(y1, y2); y < max(y1, y2); ++y) {
                    wall[y][x1][0] = true;
                    if(x1 - 1 >= 0) wall[y][x1 - 1][2] = true;
                }
            } else {
                for(int x = min(x1, x2); x < max(x1, x2); ++x) {
                    wall[y1][x][1] = true;
                    if(y1 - 1 >= 0) wall[y1 - 1][x][3] = true;
                }
            }
        }

        int x1, y1, x2, y2, exitx, exity;
        cin >> x1 >> y1 >> x2 >> y2 >> exitx >> exity;
        int curx, cury, cur_direction;
        if(y1 == y2) {
            if(y1 == 0) {
                cur_direction = 3;
                curx = min(x1, x2), cury = y1;
            } else {
                cur_direction = 1;
                curx = min(x1, x2), cury = y1 - 1;
            }
        } else {
            if(x1 == 0) {
                cur_direction = 2;
                curx = x1, cury = min(y1, y2);
            } else {
                cur_direction = 0;
                curx = x1 - 1, cury = min(y1, y2);
            }
        }

        int dist = 1;
        while(true) {
            if(curx == exitx && cury == exity) {
                cout << dist << '\n';
                break;
            }
            if(used[cury][curx][cur_direction]) {
                cout << "Impossible\n";
                break;
            }
            used[cury][curx][cur_direction] = true;
            int new_direction = (cur_direction + 1) % 4;
            for(int i = 0; i < 4; ++i) {
                if(!wall[cury][curx][new_direction]) {
                    curx += dx[new_direction];
                    cury += dy[new_direction];
                    cur_direction = new_direction;
                    break;
                }
                new_direction = (new_direction + 3) % 4;
            }
            ++dist;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3084
final_rank: 8

code_snippet: #include<iostream>
#include<vector>

using namespace std;

const int N = 110;

int w, h, n;
bool wall[N][N][4], used[N][N][4];
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, -1, 0, 1};
//0 - left, 1 - down, 2 - right, 3 - up

int main() {
    while(cin >> w >> h >> n) {
        if(w == 0) break;
        for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j) for(int k = 0; k < 4; ++k) wall[i][j][k] = used[i][j][k] = false;
        for(int i = 0; i < h; ++i) {
            wall[i][0][0] = true;
            wall[i][w - 1][2] = true;
        }
        for(int i = 0; i < w; ++i) {
            wall[0][i][1] = true;
            wall[h - 1][i][3] = true;
        }

        for(int i = 0; i < n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if(x1 == x2) {
                for(int y = min(y1, y2); y < max(y1, y2); ++y) {
                    wall[y][x1][0] = true;
                    if(x1 - 1 >= 0) wall[y][x1 - 1][2] = true;
                }
            } else {
                for(int x = min(x1, x2); x < max(x1, x2); ++x) {
                    wall[y1][x][1] = true;
                    if(y1 - 1 >= 0) wall[y1 - 1][x][3] = true;
                }
            }
        }

        int x1, y1, x2, y2, exitx, exity;
        cin >> x1 >> y1 >> x2 >> y2 >> exitx >> exity;
        int curx, cury, cur_direction;
        if(y1 == y2) {
            if(y1 == 0) {
                cur_direction = 3;
                curx = min(x1, x2), cury = y1;
            } else {
                cur_direction = 1;
                curx = min(x1, x2), cury = y1 - 1;
            }
        } else {
            if(x1 == 0) {
                cur_direction = 2;
                curx = x1, cury = min(y1, y2);
            } else {
                cur_direction = 0;
                curx = x1 - 1, cury = min(y1, y2);
            }
        }

        int dist = 1;
        while(true) {
            if(curx == exitx && cury == exity) {
                cout << dist << '\n';
                break;
            }
            if(used[cury][curx][cur_direction]) {
                cout << "Impossible\n";
                break;
            }
            used[cury][curx][cur_direction] = true;
            int v = (cur_direction + 1) % 4;
            for(int i = 0; i < 4; ++i) {
                if(!wall[cury][curx][v]) {
                    curx += dx[v];
                    cury += dy[v];
                    cur_direction = v;
                    break;
                }
                v = (v + 3) % 4;
            }
            ++dist;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3144
final_rank: 7

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

const int MAX_W = 100;
const int MAX_H = 100;

const int dxs[] = {1, 0, -1, 0};
const int dys[] = {0, 1, 0, -1};

/* typedef */

/* global variables */

int w, h, n;
bool wls[MAX_H][MAX_W][4], vstd[MAX_H][MAX_W][4];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> w >> h >> n;
    if (w == 0) break;
    
    memset(wls, 0, sizeof(wls));
    memset(vstd, 0, sizeof(vstd));

    for (int i = 0; i < n; i++) {
      int x0, y0, x1, y1;
      cin >> x0 >> y0 >> x1 >> y1;

      if (y0 == y1) {
	if (x0 > x1) swap(x0, x1);
	for (int x = x0; x < x1; x++)
	  wls[y0 - 1][x][1] = wls[y0][x][3] = true;
      }
      else {
	if (y0 > y1) swap(y0, y1);
	for (int y = y0; y < y1; y++)
	  wls[y][x0 - 1][0] = wls[y][x0][2] = true;
      }
    }

    for (int x = 0; x < w; x++) wls[0][x][3] = wls[h - 1][x][1] = true;
    for (int y = 0; y < h; y++) wls[y][0][2] = wls[y][w - 1][0] = true;
    
    int x, y, x1, y1, gx, gy, left;
    cin >> x >> y >> x1 >> y1 >> gx >> gy;

    if (y == y1) {
      if (x > x1) swap(x, x1);
      if (y == 0) left = 2;
      else y--, left = 0;
    }
    else {
      if (y > y1) swap(y, y1);
      if (x == 0) left = 1;
      else x--, left = 3;
    }

    int d;
    for (d = 1; x != gx || y != gy; d++) {
      //printf("x=%d, y=%d, left=%d\n", x, y, left);
      
      if (vstd[y][x][left]) { d = 0; break; }
      vstd[y][x][left] = true;
      
      int drc = left;
      for (int di = 0; di < 4; di++, drc = (drc + 3) % 4)
	if (! wls[y][x][drc]) {
	  x += dxs[drc];
	  y += dys[drc];
	  left = (drc + 1) % 4;
	  break;
	}
    }

    if (d <= 0) cout << "Impossible" << endl;
    else cout << d << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3448
final_rank: 9

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

int main() {
	int case_id=0;
	while (true) {
		int W,H,N;cin>>W>>H>>N;
		if(!W)break;
		vector<vector<vector<int>>>walls(H,vector<vector<int>>(W,vector<int>(4)));
		int dx[4] = { -1,0,1,0 };
		int dy[4] = { 0,-1,0,1 };
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if(i==0)walls[i][j][1]=true;
				if(i==H-1)walls[i][j][3]=true;
				if(j==0)walls[i][j][0]=true;
				if(j==W-1)walls[i][j][2]=true;
			}
		}
		for (int i = 0; i < N; ++i) {
			int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;
			if (x1 == x2) {
				int u=max(y1,y2);
				int d=min(y1,y2);
				assert(x1>0&&x1<W);
				for (int y = d; y < u; ++y) {
					walls[y][x1-1][2]=true;
					walls[y][x1][0]=true;
				}
			}
			else {
				int l=min(x1,x2);
				int r=max(x1,x2);
				assert(y1>0&&y1<H);
				for (int x = l; x < r; ++x) {
					walls[y1][x][1]=true;
					walls[y1-1][x][3]=true;
				}
			}
		}
		int sx,sy,gx,gy,sway;
		{
			int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;
			if (x1 == x2) {
				if (x1 == 0) {
					sway=2;
					sx=0;
					sy=min(y1,y2);
				}
				else {
					sway=0;
					sx=W-1;
					sy=min(y1,y2);
				}
			}
			else {
				if (y1 == 0) {
					sway=3;
					sy=0;
					sx=min(x1,x2);
				}
				else {
					sway=1;
					sy=H-1;
					sx=min(x1,x2);
				}
			}
		}
		cin>>gx>>gy;
		int ans=0;
		{
			int nx=sx;
			int ny=sy;
			int nway=sway;
			while (true) {
				if(nx==gx&&ny==gy)break;
				if (ans > 100000) {
					ans=-1;
					break;
				}
				if (walls[ny][nx][(nway+1)%4]) {
					if (walls[ny][nx][nway]) {
						if (walls[ny][nx][(nway + 3) % 4]) {
							if (walls[ny][nx][(nway + 2) % 4]) {
								ans=-1;
								break;
							}
							else {
								nway=(nway+2)%4;
							}
						}
						else {
							nway=(nway+3)%4;
						}
					}
					else {
						nway=nway;
					}
				}
				else {
					nway=(nway+1)%4;
				}
				nx+=dx[nway];
				ny+=dy[nway];
				ans++;
			}
		}
		if (ans == -1) {
			cout<<"Impossible"<<endl;
		}
		else {
			cout<<ans+1<<endl;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3720
final_rank: 16

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum DIR{
	North,
	East,
	South,
	West,
};

struct Info{
	bool left_wall,under_wall;
};

struct Data{
	void  set(int arg_x,int arg_y){
		x = arg_x;
		y = arg_y;
	}
	int x,y;
};

struct State{
	void set(int arg_x,int arg_y,DIR arg_dir,int arg_sum_cost){
		x = arg_x;
		y = arg_y;
		dir = arg_dir;
		sum_cost = arg_sum_cost;
	}
	int x,y;
	DIR dir;
	int sum_cost;
};

int W,H,N;
Info info[101][101];
bool visited[101][101][4];
Data goal;


void func(){

	for(int x = 0; x <= W; x++){
		for(int y = 0; y <= H; y++){
			info[x][y].left_wall = false;
			info[x][y].under_wall = false;

			for(int i = 0; i < 4; i++)visited[x][y][i] = false;
		}
	}

	for(int y = 0; y < H; y++){
		info[0][y].left_wall = true;
	}

	for(int x = 0; x < W; x++){
		info[x][H].under_wall = true;
	}

	for(int y = 0; y < H; y++){
		info[W][y].left_wall = true;
	}

	for(int x = 0; x < W; x++){
		info[x][0].under_wall = true;
	}

	Data left,right;

	for(int i = 0; i < N; i++){
		scanf("%d %d %d %d",&left.x,&left.y,&right.x,&right.y);

		if(left.x == right.x){
			if(right.y < left.y)swap(left,right);

			for(int y = left.y; y < right.y; y++){
				info[left.x][y].left_wall = true;
			}
		}else{
			if(right.x < left.x)swap(left,right);

			for(int x = left.x; x < right.x; x++){
				info[x][left.y].under_wall = true;
			}
		}
	}

	State state;

	scanf("%d %d %d %d",&left.x,&left.y,&right.x,&right.y);

	if(left.x == right.x){
		if(right.y < left.y)swap(left,right);

		if(left.x == 0){

			info[left.x][left.y].left_wall = false;
			state.set(left.x,left.y,East,1);

		}else{

			info[left.x][left.y].left_wall = false;
			state.set(left.x-1,left.y,West,1);

		}
	}else{
		if(right.x < left.x)swap(left,right);

		if(left.y == H){

			info[left.x][left.y].under_wall = false;
			state.set(left.x,left.y-1,South,1);

		}else{

			info[left.x][left.y].under_wall = false;
			state.set(left.x,left.y,North,1);
		}
	}

	visited[state.x][state.y][state.dir] = true;

	scanf("%d %d",&goal.x,&goal.y);

	int ans = BIG_NUM;
	bool FLG;

	while(true){

		FLG = true;

		switch(state.dir){
		case North:
			if(info[state.x][state.y].left_wall == false){
				FLG = false;
			}
			break;
		case East:
			if(info[state.x][state.y+1].under_wall == false){
				FLG = false;
			}
			break;
		case South:
			if(info[state.x+1][state.y].left_wall == false){
				FLG = false;
			}
			break;
		case West:
			if(info[state.x][state.y].under_wall == false){
				FLG = false;
			}
			break;
		}

		if(FLG){

			switch(state.dir){
			case North:
				if(info[state.x][state.y+1].under_wall == false){
					state.y++;
					state.sum_cost++;
				}else{
					state.dir = East;
				}
				break;
			case East:
				if(info[state.x+1][state.y].left_wall == false){
					state.x++;
					state.sum_cost++;
				}else{
					state.dir = South;
				}
				break;
			case South:
				if(info[state.x][state.y].under_wall == false){
					state.y--;
					state.sum_cost++;
				}else{
					state.dir = West;
				}
				break;
			case West:
				if(info[state.x][state.y].left_wall == false){
					state.x--;
					state.sum_cost++;
				}else{
					state.dir = North;
				}
				break;
			}

		}else{

			switch(state.dir){
			case North:
				if(state.x-1 == goal.x && state.y == goal.y){
					if(state.x != 0){
						ans = state.sum_cost+1;
					}
				}else{

					state.x--;
					state.sum_cost++;

					if(info[state.x][state.y].under_wall == true){
						state.dir = West;
					}else{
						state.y--;
						state.sum_cost++;
						state.dir = South;
					}
				}
				break;
			case East:
				if(state.x == goal.x && state.y+1 == goal.y){
					if(state.y != H-1){
						ans = state.sum_cost+1;
					}
				}else{

					state.y++;
					state.sum_cost++;

					if(info[state.x][state.y].left_wall == true){
						state.dir = North;
					}else{
						state.x--;
						state.sum_cost++;
						state.dir = West;
					}
				}
				break;
			case South:
				if(state.x+1 == goal.x && state.y == goal.y){
					if(state.x != W-1){
						ans = state.sum_cost+1;
					}
				}else{

					state.x++;
					state.sum_cost++;

					if(info[state.x][state.y+1].under_wall == true){
						state.dir = East;
					}else{
						state.y++;
						state.sum_cost++;
						state.dir = North;
					}
				}
				break;
			case West:
				if(state.x == goal.x && state.y-1 == goal.y){
					if(state.y != 0){
						ans = state.sum_cost+1;
					}
				}else{

					state.y--;
					state.sum_cost++;

					if(info[state.x+1][state.y].left_wall == true){
						state.dir = South;
					}else{
						state.x++;
						state.sum_cost++;
						state.dir = East;
					}
				}
				break;
			}

			if(ans != BIG_NUM)break;
		}

		if(state.x < 0 || state.x >= W || state.y < 0 || state.y >= H)break;

		if(visited[state.x][state.y][state.dir])break;
		visited[state.x][state.y][state.dir] = true;

		if(state.x == goal.x && state.y == goal.y){
			ans = state.sum_cost;
			break;
		}
	}

	if(ans == BIG_NUM){
		printf("Impossible\n");
	}else{
		printf("%d\n",ans);
	}
}


int main(){

	while(true){
		scanf("%d %d %d",&W,&H,&N);
		if(W == 0 && H == 0 && N == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
 
int W,H,N;
int ex,ey;
bool used[222][222][4];
bool fie[222][222];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
 
 
int solve( int x, int y, int d ){
  if( used[x][y][d] ) return -(1<<29);
  used[x][y][d] = true;
  if( x == ex && y == ey ) return 1;  
  for(int i=0;i<4;i++){
    int nd = (d+i-1+4)%4;
    int nx = x + 2*dx[nd], ny = y + 2*dy[nd];
    int cx = x + dx[nd], cy = y + dy[nd];
    if( fie[cx][cy] ) continue;
    return solve( nx, ny, nd ) + 1;
  }
  return -(1<<29);  
}
 
void nuri(int x1,int y1,int x2,int y2 ){
  for(int x=x1;x<=x2;x++)
    for(int y=y1;y<=y2;y++)
      fie[x][y] = true;
}
 
int main(){
  while( cin >> W >> H >> N && (H||H||N) ){
    memset( used,0,sizeof( used ) );
    memset( fie,0,sizeof( fie ) );
    for(int i=0;i<=2*W;i++)
      fie[i][0] = fie[i][2*H] = true;
    for(int i=0;i<=2*H;i++)
      fie[0][i] = fie[2*W][i] = true;
 
    for(int i=0;i<N;i++){
      int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2;
      if( x1 > x2 ) swap( x1, x2 );
      if( y1 > y2 ) swap( y1, y2 );
      x1*=2; y1*=2; x2*=2; y2*=2;
      nuri( x1,y1,x2,y2 );
    }
 
    int sa,sb,sc,sd;
    cin >> sa >> sb >> sc >> sd >> ex >> ey;
    if( sa > sc ) swap( sa,sc );
    if( sb > sd ) swap( sb,sd );
    sa *= 2; sb *=2; sc *=2; sd*=2;
    ex = 2*ex+1; ey = 2*ey+1;
 
    int res;
    if( sa == sc ){
      if( sa == 0 ) res = solve( 1, sb+1, 1 );
      else res = solve( 2*W-1, sb+1, 3 );
    } else {
      if( sb == 0 ) res = solve( sa+1, 1, 0 );
      else res = solve( sa+1, 2*H-1, 2 );
    }
    if( res < 0 ) cout << "Impossible" << endl;
    else cout << res << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3232
final_rank: 10

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

int W,H,N;
int ex,ey;
bool used[222][222][4];
bool fie[222][222];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};


int solve( int x, int y, int d ){
  if( used[x][y][d] ) return -(1<<29);
  used[x][y][d] = true;
  if( x == ex && y == ey ) return 1;  
  for(int i=0;i<4;i++){
    int nd = (d+i-1+4)%4;
    int nx = x + 2*dx[nd], ny = y + 2*dy[nd];
    int cx = x + dx[nd], cy = y + dy[nd];
    if( fie[cx][cy] ) continue;
    return solve( nx, ny, nd ) + 1;
  }
  return -(1<<29);  
}

void nuri(int x1,int y1,int x2,int y2 ){
  for(int x=x1;x<=x2;x++)
    for(int y=y1;y<=y2;y++)
      fie[x][y] = true;
}

int main(){
  while( cin >> W >> H >> N && (H||H||N) ){
    memset( used,0,sizeof( used ) );
    memset( fie,0,sizeof( fie ) );
    for(int i=0;i<=2*W;i++)
      fie[i][0] = fie[i][2*H] = true;
    for(int i=0;i<=2*H;i++)
      fie[0][i] = fie[2*W][i] = true;

    for(int i=0;i<N;i++){
      int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2;
      if( x1 > x2 ) swap( x1, x2 );
      if( y1 > y2 ) swap( y1, y2 );
      x1*=2; y1*=2; x2*=2; y2*=2;
      nuri( x1,y1,x2,y2 );
    }

    int sa,sb,sc,sd;
    cin >> sa >> sb >> sc >> sd >> ex >> ey;
    if( sa > sc ) swap( sa,sc );
    if( sb > sd ) swap( sb,sd );
    sa *= 2; sb *=2; sc *=2; sd*=2;
    ex = 2*ex+1; ey = 2*ey+1;

    int res;
    if( sa == sc ){
      if( sa == 0 ) res = solve( 1, sb+1, 1 );
      else res = solve( 2*W-1, sb+1, 3 );
    } else {
      if( sb == 0 ) res = solve( sa+1, 1, 0 );
      else res = solve( sa+1, 2*H-1, 2 );
    }
    if( res < 0 ) cout << "Impossible" << endl;
    else cout << res << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3312
final_rank: 11

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 100;
const int MAX_H = 100;

const int dxs[] = {1, 0, -1, 0};
const int dys[] = {0, 1, 0, -1};

/* typedef */

/* global variables */

int w, h, n;
bool wls[MAX_H][MAX_W][4], vstd[MAX_H][MAX_W][4];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> w >> h >> n;
    if (w == 0) break;
    
    memset(wls, 0, sizeof(wls));
    memset(vstd, 0, sizeof(vstd));

    for (int i = 0; i < n; i++) {
      int x0, y0, x1, y1;
      cin >> x0 >> y0 >> x1 >> y1;

      if (y0 == y1) {
	if (x0 > x1) swap(x0, x1);
	for (int x = x0; x < x1; x++)
	  wls[y0 - 1][x][1] = wls[y0][x][3] = true;
      }
      else {
	if (y0 > y1) swap(y0, y1);
	for (int y = y0; y < y1; y++)
	  wls[y][x0 - 1][0] = wls[y][x0][2] = true;
      }
    }

    for (int x = 0; x < w; x++) wls[0][x][3] = wls[h - 1][x][1] = true;
    for (int y = 0; y < h; y++) wls[y][0][2] = wls[y][w - 1][0] = true;
    
    int x, y, x1, y1, gx, gy, left;
    cin >> x >> y >> x1 >> y1 >> gx >> gy;

    if (y == y1) {
      if (x > x1) swap(x, x1);
      if (y == 0) left = 2;
      else y--, left = 0;
    }
    else {
      if (y > y1) swap(y, y1);
      if (x == 0) left = 1;
      else x--, left = 3;
    }

    int d;
    for (d = 1; x != gx || y != gy; d++) {
      //printf("x=%d, y=%d, left=%d\n", x, y, left);
      
      if (vstd[y][x][left]) { d = 0; break; }
      vstd[y][x][left] = true;
      
      int drc = left;
      for (int di = 0; di < 4; di++, drc = (drc + 3) % 4)
	if (! wls[y][x][drc]) {
	  x += dxs[drc];
	  y += dys[drc];
	  left = (drc + 1) % 4;
	  break;
	}
    }

    if (d <= 0) cout << "Impossible" << endl;
    else cout << d << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1244
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

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

int w,h,n,sx,sy,gx,gy,d;
bool G[202][202];

void initG(){
  fill(G[0],G[202],false);
  for(int i=0;i<2*h+1;i++) G[i][0] = G[i][2*w] = true;
  for(int i=0;i<2*w+1;i++) G[0][i] = G[2*h][i] = true;
}

void displayG(int x, int y){
    for(int i=0;i<2*h+1;i++){
      for(int j=0;j<2*w+1;j++){
	if(i == y && x == j) cout << '*' << ' ';
	else cout << G[i][j] << ' ';
      }
      cout << endl;
    }
    cout << endl;
}

void solve(){
  int x = sx, y = sy, ans = 1;
  bool f[202][202][4];

  if(sx == gx && sy == gy){
    cout << 1 << endl;
    return;
  }


  for(int i=0;i<202;i++) for(int j=0;j<202;j++) for(int k=0;k<4;k++) f[i][j][k] = false;
  f[x][y][d] = true;

  while(1){
    bool f2 = false;
    for(int i=-1;i<=2;i++){
      int nd = (d + i + 4) % 4;
      int nx = x + dx[nd];
      int ny = y + dy[nd];

      if(G[ny][nx]) continue;

      nx += dx[nd];
      ny += dy[nd];
      if(f[nx][ny][nd]) break;

      x = nx;
      y = ny;
      d = nd;
      ans++;
      f[x][y][nd] = true;
      f2 = true;

      //displayG(x,y);

      if(x == gx && y == gy){
	cout << ans << endl;
	return;
      }

      break;
    }
    if(!f2) break;
  }
  cout << "Impossible" << endl;
}

int main(){
  int x1,y1,x2,y2;
  while(cin >> w >> h >> n && (w|h|n)){
    initG();

    for(int i=0;i<n;i++){
      cin >> x1 >> y1 >> x2 >> y2;
      if(x1 > x2) swap(x1,x2);
      if(y1 > y2) swap(y1,y2);
      if(x1 == x2) for(int j=2*y1;j<=2*y2;j++) G[j][2*x1] = true;
      if(y1 == y2) for(int j=2*x1;j<=2*x2;j++) G[2*y1][j] = true;
      //displayG();
    }

    cin >> x1 >> y1 >> x2 >> y2 >> gx >> gy;
    if(x1 > x2) swap(x1,x2);
    if(y1 > y2) swap(y1,y2);
    sx = x1 * 2 + 1;
    sy = y1 * 2 + 1;
    if(x1 == x2){
      if(x1 == 0) d = 1;
      else{
	d = 3;
	sx -= 2;
      }
    } else {
      if(y1 == 0) d = 0;
      else {
	d = 2;
	sy -= 2;
      }
    }

    gx = gx * 2 + 1;
    gy = gy * 2 + 1;
    //cout << sx << ' ' << sy << ' ' << d << endl;
    solve();
  }
}
accuracy: 1
time_ms: 70
memory_kb: 1360
final_rank: 5

code_snippet: #include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define MAX 500
using namespace std;

/*
手が壁から離れた -> 手の方向に向きを変える
目の前が壁　　　 -> 手の方向と逆に向きを変える
 */

int W,H,N,xS,yS,xG,yG;
char G[MAX][MAX];
int dx2[2][2] = {{-1,+1},{0,0}};
int dy2[2][2] = {{0,0},{+1,-1}};
/*
int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};
*/
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};

void print()
{
  cout << "print---" << endl;
  rep(i,2*H+3)
    {
      rep(j,2*W+3)
	{
	  cout << G[i][j];
	}
      cout << endl;
    }
  cout << endl;
}


void initGraph()
{
  rep(i,MAX)rep(j,MAX)G[i][j] = 'x';
  rep(i,2*H+1)
    {
      for(int j=(i%2?0:1);j<2*W+1;j+=2)
	{
	  char c = (i%2?'|':'-');
	  if(i == 0 || j == 0 || i == 2*H || j == 2*W)
	  G[i][j] = c;
	  rep(k,2)
	    {
	      int nx = j + dx2[i%2][k];
	      int ny = i + dy2[i%2][k];
	      if(!(0 <= nx && nx < 2*W+1 && 0 <= ny && ny < 2*H+1))continue;
	      if(nx == 0 || ny == 0 || nx == 2*W || ny == 2*H)
		G[ny][nx] = 'o';
	    }
	}
    }

}


void drawO(int x,int y)
{
  //cout << "draw " << x << "," << y << endl;
  rep(k,2)
    {
      int nx = x + dx2[y%2][k];
      int ny = y + dy2[y%2][k];
      if(!(0 <= nx && nx < 2*W+1 && 0 <= ny && ny < 2*H+1))continue;
	G[ny][nx] = 'o';
    }
}

void makeGraph()
{
  int x1,y1,x2,y2;
  rep(i,N)
    {
      cin >> x1 >> y1 >> x2 >> y2;
      //cout << "from " << x1 << "," << y1 << " " << x2 << "," << y2 << endl;
      if(x1 == x2)
	{
	  if(y1 > y2)swap(y1,y2);
	  x1 *= 2, y1 = y1*2+1, y2 = y2*2+1;
      //cout << x1 << "," << y1 << " " << x2 << "," << y2 << endl;
	  for(int y=y1;y<y2;y+=2)
	    {
	      G[y][x1] = '|';
	      drawO(x1,y);
	    }
	}
      else if(y1 == y2)
	{
	  if(x1 > x2)swap(x1,x2);
	  x1 = x1*2+1, x2 = x2*2+1, y1 = y1*2;
	//cout << x1 << "," << y1 << " " << x2 << "," << y2 << endl;
	  for(int x=x1;x<x2;x+=2)
	    {
	      G[y1][x] = '-';
	      drawO(x,y1);
	    }
	}
      else assert(false);
      //print();
    }

  int x1E,y1E,x2E,y2E;
  cin >> x1E >> y1E >> x2E >> y2E >> xG >> yG;

  
  if(x1E == x2E)
    {
      if(y1E > y2E)swap(y1E,y2E);
      x1E *= 2, y1E = y1E*2 + 1, y2E = y2E*2 + 1;
      G[y1E][x1E] = 'x';
      //for(int y=y1E;y<y2E;y+=2)G[y][x1E] = 'x';
    }
  else if(y1E == y2E)
    {
      if(x1E > x2E)swap(x1E,x2E);
      y1E *= 2, x1E = x1E*2 + 1, x2E = x2E*2 + 1;
      G[y1E][x1E] = 'x';
      //for(int x=x1E;x<x2E;x+=2)G[y1E][x] = 'x';
    }
  
  xS = x1E, yS = y1E;

}

void compute()
{
  int dir = inf;
  rep(i,4)
    {
      int nx = xS + dx[i];
      int ny = yS + dy[i];
      if(!(0 <= nx && nx < 2*W+1 && 0 <= ny && ny < 2*H+1))continue;
      if(G[ny][nx] != 'x')continue;
      dir = i;
    }

  xG = xG*2 + 1;
  yG = yG*2 + 1;
  //cout << "goal = " << xG << "," << yG << endl;
  int cnt = 0;
  bool used[4][2*H+1][2*W+1];
  rep(i,4)rep(j,2*H+1)rep(k,2*W+1)used[i][j][k] = false;

  int x = xS, y = yS;

  while(true)
    {
      //cout << x << "," << y << " " << dir << " " << used[dir][y][x]<< endl;
      if(x == xG && y == yG)
	{
	  cout << cnt << endl;
	  return;
	}
      if(used[dir][y][x])
	{
	  cout << "Impossible" << endl;
	  return;
	}
      used[dir][y][x] = true;

      int nx = x + dx[dir];
      int ny = y + dy[dir];
      int lx = nx + dx[(dir+1)%4];
      int ly = ny + dy[(dir+1)%4];
      //cout << "n = " << nx << "," << ny << " dir = " << dir << endl;
      //cout << "l = " << lx << "," << ly << endl;
      if(!(0 <= nx && nx < 2*W+1 && 0 <= ny && ny < 2*H+1))
	{
	  cout << "Impossible" << endl;
	  return;
	}
      if(G[ny][nx] != 'x')
	{
	  //cout << "in1" << endl;
	  dir = (dir + 3)%4;
	}
      else if(G[ly][lx] == 'x')
	{
	  //cout << "in2" << endl;
	  dir = (dir + 1)%4;
	  x = nx, y = ny;
	  cnt++;
	}
      else 
	{
	  //cout << "in3" << endl;
	  if(G[ly][lx] != 'o')cnt++;
	  x = nx;
	  y = ny;
	}

      /*
      char pc,ppc;
      pc = G[y][x];
      int llx = x + dx[(dir+1)%4];
      int lly = y + dy[(dir+1)%4];

      ppc = G[lly][llx];
      
      G[y][x] = 'B';
      G[lly][llx] = 'H';
      print();
      G[y][x] = pc;
      G[lly][llx] = ppc;
      */
      //cout <<"next -> " << x << "," << y << endl;
    }

}


int main()
{
  while(cin >> W >> H >> N,W|H|N)
    {
      initGraph();
      makeGraph();
      //print();
      compute();
      //cout << xG <<" " << yG  <<endl;
    }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1564
final_rank: 6

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

int main()
{
    for(;;){
        int w, h, n;
        cin >> w >> h >> n;
        if(w == 0)
            return 0;

        vector<vector<bool> > wall(2*h+1, vector<bool>(2*w+1, true));
        for(int i=1; i<2*h; ++i){
            for(int j=1; j<2*w; ++j){
                wall[i][j] = false;
            }
        }

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

            if(x1 == x2){
                for(int y=min(y1,y2); y<max(y1,y2); ++y)
                    wall[y*2+1][x1*2] = true;
            }else{
                for(int x=min(x1,x2); x<max(x1,x2); ++x)
                    wall[y1*2][x*2+1] = true;
            }
        }

        int sx1, sy1, sx2, sy2;
        cin >> sx1 >> sy1 >> sx2 >> sy2;
        int y, x, d;
        if(sy1 == 0 && sy2 == 0){
            y = 1;
            x = min(sx1, sx2) * 2 + 1;
            d = 0;
        }else if(sy1 == h && sy2 == h){
            y = 2 * h - 1;
            x = min(sx1, sx2) * 2 + 1;
            d = 2;
        }else if(sx1 == 0 && sx2 == 0){
            y = min(sy1, sy2) * 2 + 1;
            x = 1;
            d = 1;
        }else{
            y = min(sy1, sy2) * 2 + 1;
            x = 2 * w - 1;
            d = 3;
        }

        int gx, gy;
        cin >> gx >> gy;
        gy = gy * 2 + 1;
        gx = gx * 2 + 1;
        
        int move = 1;
        vector<vector<vector<bool> > > check(2*h+1, vector<vector<bool> >(2*w+1, vector<bool>(4, false)));
        for(;;){
            if(y == gy && x == gx){
                cout << move << endl;
                break;
            }else if(check[y][x][d]){
                cout << "Impossible" << endl;
                break;
            }
            check[y][x][d] = true;

            if(!wall[y+dy[(d+3)%4]][x+dx[(d+3)%4]]){
                d = (d + 3) % 4;
                y += dy[d] * 2;
                x += dx[d] * 2;
                ++ move;
            }else if(!wall[y+dy[d]][x+dx[d]]){
                y += dy[d] * 2;
                x += dx[d] * 2;
                ++ move;
            }else{
                d = (d + 1) % 4;
            }
        }
    }
}
accuracy: 1
time_ms: 130
memory_kb: 4096
final_rank: 17

code_snippet: #include <iostream>
#include <cstring>

using namespace std;

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

int main(){
	while(true){
		int W, H, N;
		cin >> W >> H >> N;
		if(W == 0 && H == 0 && N == 0){ break; }
		memset(field, 0, sizeof(field));
		for(int i = 0; i <= H * 2; ++i){
			field[i][0] = field[i][W * 2] = true;
		}
		for(int i = 0; i <= W * 2; ++i){
			field[0][i] = field[H * 2][i] = true;
		}
		while(N--){
			int x1, y1, x2, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			if(x1 != x2){
				if(x2 < x1){ swap(x1, x2); }
				for(int i = x1 * 2; i <= x2 * 2; ++i){
					field[y1 * 2][i] = true;
				}
			}else{
				if(y2 < y1){ swap(y1, y2); }
				for(int i = y1 * 2; i <= y2 * 2; ++i){
					field[i][x1 * 2] = true;
				}
			}
		}
		int xe1, ye1, xe2, ye2, xg, yg, xs, ys;
		cin >> xe1 >> ye1 >> xe2 >> ye2 >> xg >> yg;
		xg = xg * 2 + 1; yg = yg * 2 + 1;
		int dir = 0;
		if(xe1 != xe2){
			xs = min(xe1, xe2) * 2 + 1;
			if(ye1 == 0){
				ys = 0;
				dir = 1;
			}else{
				ys = (H * 2);
				dir = 3;
			}
		}else{
			ys = min(ye1, ye2) * 2 + 1;
			if(xe1 == 0){
				xs = 0;
				dir = 0;
			}else{
				xs = (W * 2);
				dir = 2;
			}
		}
		field[ys][xs] = false;
		int x = xs, y = ys, answer = 0;
		while(true){
			int nx = x + dx[dir], ny = y + dy[dir];
			while(field[ny][nx]){
				dir = (dir - 1) & 3;
				nx = x + dx[dir], ny = y + dy[dir];
			}
			if(!field[ny + dy[(dir + 1) & 3]][nx + dx[(dir + 1) & 3]]){
				dir = (dir + 1) & 3;
			}
			x = nx; y = ny;
			if((x & 1) == 1 && (y & 1) == 1){ ++answer; }
			if(x == xs && y == ys){
				answer = -1;
				break;
			}else if(x == xg && y == yg){
				break;
			}
		}
		if(answer < 0){
			cout << "Impossible" << endl;
		}else{
			cout << answer << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 908
final_rank: 12

code_snippet: #include <iostream>
#include <cstring>
using namespace std;

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

int main()
{
	int W,H,N;
	while (cin >> W >> H >> N, W||H||N)
	{
		int a[2*H+1][2*W+1]; //a[y][x]
		memset(a, 0, sizeof(a));
		for (int i=0; i<=2*W; i++)
		{
			a[  0][i] = -1;
			a[2*H][i] = -1;
		}
		for (int i=0; i<=2*H; i++)
		{
			a[i][  0] = -1;
			a[i][2*W] = -1;
		}

		int x1,x2,y1,y2;
		for (int i=0; i<N; i++)
		{
			cin >> x1 >> y1 >> x2 >> y2;
			if (x1 == x2)
				for (int y=2*min(y1,y2); y<=2*max(y1,y2); y++)
					a[y][2*x1] = -1;
			else
				for (int x=2*min(x1,x2); x<=2*max(x1,x2); x++)
					a[2*y1][x] = -1;
		}
		int xg, yg;
		cin >> x1 >> y1 >> x2 >> y2 >> xg >> yg;

		a[y1+y2][x1+x2] = 0;
		int px=x1+x2, py=y1+y2;
		if (x1==x2)
			px += (x1==0?1:-1);
		else
			py += (y1==0?1:-1);

		int dir = (x1==x2 ? (x1==0 ? 1 : 3) : (y1==0 ? 0 : 2));
		//int pd = -1;
		int co = 1;
		bool f=1/*, pf=0*/;

		/*
		for (int y=0; y<=2*H; y++)
		{
			for (int x=0; x<=2*W; x++)
				cout << (a[2*H-y][x]==-1?'#':' ');
			cout << endl;
		}
		*/

		a[py][px] = 1;
		while (f)
		{
			//cout << "dir:" << dir << ", co:" << co << ",pd:" << pd << ",(" << py << ',' << px << ')' << endl;
			if (px==2*xg+1 && py==2*yg+1)
				break;

			if (a[py+dy[(dir+3)%4]][px+dx[(dir+3)%4]]==0)
				dir = (dir+3)%4;
			else
				while (a[py+dy[dir]][px+dx[dir]] != 0)
					dir = (dir+1)%4;

			px += 2*dx[dir]; py+= 2*dy[dir];
			if (px<0||py<0||px>2*W||py>2*H)
			{
				f=0; break;
			}

			co++;
			/*
			if (pf && co-a[py][px] == pd)
			{
				f=0; break;
			}

			if (a[py][px]==0)
				pf=0;
			else
				pf=1;
			pd = co-a[py][px];
			a[py][px] = co;
			*/
		}

		if (f)
			cout << co << endl;
		else
			cout << "Impossible" << endl;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 1024
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

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

int W,H,N;
bool field[1000][1000];
bool used[4][1000][1000];

int main(){

  while(cin>>W>>H>>N&&(W|H|N)){
    memset(field,0,sizeof(field));
    memset(used,0,sizeof(used));
    for(int i=0;i<=2*H;i++){
      field[i][0]=true;
      field[i][2*W]=true;
    }
    for(int i=0;i<=2*W;i++){
      field[0][i]=true;
      field[2*H][i]=true;
    }
    for(int i=0;i<N;i++){
      int x1,x2,y1,y2;
      cin>>x1>>y1>>x2>>y2;
      if(y1==y2){
	int minx=min(x1,x2);
	int maxx=max(x1,x2);
	for(int j=minx*2;j<=maxx*2;j++)
	  field[2*y1][j]=true;
      }
      else{
	int miny=min(y1,y2);
	int maxy=max(y1,y2);
	for(int j=miny*2;j<=maxy*2;j++)
	  field[j][2*x1]=true;
      }
    }
    int xe1,ye1,xe2,ye2;
    int gx,gy;
    int sx,sy;
    int sang;
    cin>>xe1>>ye1>>xe2>>ye2;
    cin>>gx>>gy;
    gx=gx*2+1;
    gy=gy*2+1;
    if(ye1==ye2){
      if(ye1==0){
	sang=3;
	sx=min(xe1,xe2)*2+1;
	sy=ye1;
      }
      else{
	sang=1;
	sx=min(xe1,xe2)*2+1;
	sy=ye1*2;
      }
    }
    else{
      if(xe1==0){
	sang=0;
	sx=xe1;
	sy=min(ye1,ye2)*2+1;
      }
      else{
	sang=2;
	sx=xe1*2;
	sy=min(ye1,ye2)*2+1;
      }
    }
    field[sy][sx]=false;
    // for(int i=2*H;i>=0;i--){
    //   for(int j=0;j<=2*W;j++){
    // 	if(field[i][j])cout<<"*";
    // 	else cout<<" ";
    //   }
    //   cout<<endl;
    // }
    int cx=sx;
    int cy=sy;
    int cang=sang;
    bool ok=true;
    int tern=0;
    while(1){
      if(used[cang][cy][cx]){
	tern++;
	ok=false;
	break;
      }
      else if(cy==gy&&cx==gx)break;
      //cout<<cang<<" "<<cy<<" "<<cx<<" "<<tern<<endl;
      used[cang][cy][cx]=true;
      int ny=cy+dy[cang];
      int nx=cx+dx[cang];
      int nyl=ny+dy[(cang+3)%4];
      int nxl=nx+dx[(cang+3)%4];
      int ry=cy+dy[(cang+1)%4];
      int rx=cx+dx[(cang+1)%4];
      if(!field[ny][nx]&&!field[nyl][nxl]){
	cy=nyl;
	cx=nxl;
	cang=(cang+3)%4;
	tern++;
	//if(cy%2==1&&cx%2==1)tern++;
      }
      else if(!field[ny][nx]&&field[nyl][nxl]){
	cy=ny;
	cx=nx;
	if(cy%2==1&&cx%2==1)tern++;
      }
      else if(field[ny][nx]&&!field[ry][rx]){
	cy=ry;
	cx=rx;
	cang=(cang+1)%4;
	if(cy%2==1&&cx%2==1)tern++;
      }
      else cang=(cang+1)%4;
    }
    if(!ok)cout<<"Impossible"<<endl;
    else cout<<tern<<endl;
  }
  
  
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 5748
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
#include <sstream>
#include <numeric>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>
#include <cassert>
using namespace std;

typedef long long ll;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
bool ISINT(double x){return fabs(x-(int)x)<EPS;}
bool ISEQ(double x,double y){return fabs(x-y)<EPS;}
string itos(ll x){stringstream ss;ss<<x;return ss.str();}
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define EREP(i,a,b) for(int i=a;i<=b;i++)
#define erep(i,n) EREP(i,0,n)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin();itr!=c.end();itr++)

struct P{
  bool wall[4];
}t[200][200];

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

int main(void){
  while(scanf("%d%d%d",&w,&h,&n),w||h||n){
    memset(t,0,sizeof(t));
    rep(i,h) t[i][0].wall[3] = t[i][w-1].wall[1] = true;
    rep(j,w) t[0][j].wall[0] = t[h-1][j].wall[2] = true;

    rep(i,n){
      int fx,fy,tx,ty;
      scanf("%d%d%d%d",&fx,&fy,&tx,&ty);
      if(fx > tx) swap(fx,tx);
      if(fy > ty) swap(fy,ty);

      if(fx == tx){
        REP(i,fy,ty){
          t[i][fx-1].wall[1] = t[i][fx].wall[3] = true;
        }
      }
      else{
        REP(j,fx,tx){
          t[fy-1][j].wall[2] = t[fy][j].wall[0] = true;
        }
      }
    }

    int x1,y1,x2,y2,gx,gy;
    scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&gx,&gy);

    if(x1 > x2) swap(x1,x2);
    if(y1 > y2) swap(y1,y2);

    int x=x1,y=y1,d=0;
    if(x1 == x2){
      if(x1 == 0){
        d = 1;
      }
      else{
        d = 3;
        x--;
      }
    }
    else{
      if(y1 == 0){
        d = 2;
      }
      else{
        d = 0;
        y--;
      }
    }

    int res = 1;
    bool flg = true;
    bool used[102][102][4];
    memset(used,0,sizeof(used));

    while(x != gx || y != gy){
      if(used[x][y][d]){
        flg = false;
        break;
      }
      used[x][y][d] = true;

      if(!t[y][x].wall[(d+1)%4]){
        d = (d+1)%4;
        x += dx[d];
        y += dy[d];
        res++;
      }
      else if(!t[y][x].wall[d]){
        x += dx[d];
        y += dy[d];
        res++;
      }
      else{
        d = (d + 3) % 4;
      }
    }

    if(flg){
      printf("%d\n",res);
    }
    else{
      printf("Impossible\n");
    }
  }
}
accuracy: 1
time_ms: 40
memory_kb: 1088
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define mp         make_pair
#define x first
#define y second

typedef pair<int,int> pii;

enum{N=0,E=1,S=2,W=3};
enum{RIGHT=1,LEFT=3,FRONT=0};
bool vis[200][200][4];
bool cango[200][200][4];


void setH(pii a1,pii a2,int r,bool fg){
  if (a1 > a2)swap(a1,a2);
  int i=a1.y;
  REP(j,a1.x,a2.x){
    if (i-1>=0)cango[i-1][j][N]=fg;
    if (i<r)cango[i][j][S]=fg;
  }
}

void setV(pii a1,pii a2,int c,bool fg){
  if (a1 > a2)swap(a1,a2);
  int j=a1.x;
  REP(i,a1.y,a2.y){
    if (j-1>=0)cango[i][j-1][E]=fg;
    if (j<c)cango[i][j][W]=fg;
  }
}

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int solve(int x,int y,int dir,int gx,int gy,int r,int c){
  int cnt=1;
  while(true){
    //cout << y <<" " << x <<" " << dir << endl;
    if (x<0||y<0||x==c||y==r||vis[y][x][dir])return -1;
    vis[y][x][dir]=true;
    if (x==gx && y == gy)return cnt;
    if (!cango[y][x][(dir+LEFT)%4]){
      dir=(dir+LEFT)%4;
      x+=dx[dir];
      y+=dy[dir];
      cnt++;
    }else if (cango[y][x][(dir+LEFT)%4] && !cango[y][x][dir]){
      x+=dx[dir];
      y+=dy[dir];
      cnt++;
    }else if (cango[y][x][(dir+LEFT)%4] && cango[y][x][dir]){
      dir=(dir+1)%4;
    }
  }
  return -1;
}


main(){
  int c,r,n;
  while(cin>>c>>r>>n &&c){
    rep(i,r)rep(j,c)rep(k,4)cango[i][j][k]=vis[i][j][k]=false;
    rep(i,n){
      pii a1,a2;
      cin>>a1.x>>a1.y>>a2.x>>a2.y;
      if (a1.x == a2.x)setV(a1,a2,c,true);
      else if (a1.y == a2.y)setH(a1,a2,r,true);
    }
    setV(mp(0,0),mp(0,r),c,true);
    setH(mp(0,0),mp(c,0),r,true);
    setH(mp(0,r),mp(c,r),r,true);
    setV(mp(c,0),mp(c,r),c,true);

    int sy,sx,dir;
    pii a1,a2;
    cin>>a1.x>>a1.y>>a2.x>>a2.y;
    if (a1 > a2)swap(a1,a2);
    if (a1.x==a2.x){
      setV(a1,a2,c,false);
      if (a1.x == 0)sx=a1.x,dir=E;
      else if (a1.x==c)sx=a1.x-1,dir=W;
      sy=a1.y;
    }else if (a1.y==a2.y){
      setH(a1,a2,r,false);
      if (a1.y == 0)sy=a1.y,dir=N;
      else if (a1.y == r)sy=a1.y-1,dir=S;
      sx=a1.x;
    }
    int gy,gx;
    cin>>gx>>gy;

//     rep(i,r){
//       rep(j,c){
//      cout << i <<" " << j <<" ";
//      rep(k,4)cout << cango[i][j][k];
//      cout << endl;
//       }
//     }
    int ans =solve(sx,sy,dir,gx,gy,r,c);
    //cout << sy <<" " << sx <<" " << dir << endl;
    if (ans == -1)cout <<"Impossible"<<endl;
    else cout << ans << endl;
  }
  return false;
}


/*
0 0 0101
0 1 0011
0 2 0110
1 0 0101
1 1 1101
1 2 0101
2 0 1001
2 1 1010
2 2 1100
0 0 0
1 0 0
2 0 0
2 0 1
2 1 1
2 2 1
3 2 0
Impossible
0 0 0101
0 1
2 2 1
3 2 0
Impossible
 */
accuracy: 1
time_ms: 140
memory_kb: 1024
final_rank: 15

code_snippet: #include<cstdio>
#include<algorithm>

#define	rep(i,n)	for(int i=0;i<n;i++)

using namespace std;

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

int main(){
	for(int W,H,n;scanf("%d%d%d",&W,&H,&n),W;){
		bool wallH[101][101]={},wallV[101][101]={};
		rep(x,W)	wallH[0][x]=wallH[H][x]=true;
		rep(y,H)	wallV[y][0]=wallV[y][W]=true;
		rep(i,n){
			int xa,ya,xb,yb;	scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
			if(xb<xa)	swap(xa,xb);
			if(yb<ya)	swap(ya,yb);
			if(ya==yb)	for(int x=xa;x<xb;x++)	wallH[ya][x]=true;
			else		for(int y=ya;y<yb;y++)	wallV[y][xa]=true;
		}
		int xs,ys,dir;
		{
			int xa,ya,xb,yb;	scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
			xs=min(xa,xb);
			ys=min(ya,yb);
			if(ya==yb){
				xs=min(xa,xb);
				if(ya==0)	ys=0,dir=1;
				else		ys=H-1,dir=3;
			}
			else{
				ys=min(ya,yb);
				if(xa==0)	xs=0,dir=0;
				else		xs=W-1,dir=2;
			}
		}
		int xg,yg;	scanf("%d%d",&xg,&yg);

		bool ok=false;
		int x=xs,y=ys,cnt=1;
		bool visited[100][100][4]={};
		while(1){
			if(x==xg && y==yg){ ok=true; break; }
			if(visited[y][x][dir])	break;
			visited[y][x][dir]=true;

			dir=(dir+1)%4;
			rep(i,4){
				int xx=x+dx[dir],yy=y+dy[dir];
				if((dir==0 && !wallV[y][xx]) || (dir==1 && !wallH[yy][x])
				|| (dir==2 && !wallV[y][ x]) || (dir==3 && !wallH[ y][x])){
					x=xx,y=yy;
					break;
				}
				dir=(dir+3)%4;
			}
			cnt++;
		}

		if(ok)	printf("%d\n",cnt);
		else	puts("Impossible");
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 836
final_rank: 1

code_snippet: #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

#define  ROT(dr) dr=(dr+1)%4;

bool f[205][205];

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

int wdx[]={-1,-1, 0, 1, 1,  1,  0,-1};
int wdy[]={ 0, 1, 1, 1, 0, -1, -1,-1};

void setw(int x1, int y1, int x2, int y2)
{
	x1*=2; y1*=2; x2*=2; y2*=2;
	y1=H*2-y1; y2=H*2-y2; 
	if(x1>x2) swap(x1,x2);
	if(y1>y2) swap(y1,y2);
	
	for(int i=y1; i<=y2; i++)
	for(int j=x1; j<=x2; j++)
		f[j][i]=1;
}

int main()
{
	int N;
	while(cin >> W >> H >> N,(W||H||N))
	{
		memset(f,0,sizeof(f));
		
		setw(0,0,0,H);
		setw(0,0,W,0);
		setw(W,0,W,H);
		setw(0,H,W,H);
		
		while(N--)
		{
			int x1,y1,x2,y2;
			cin >> x1 >> y1 >> x2 >> y2;
			setw(x1,y1,x2,y2);
		}
		
		int ux,uy,vx,vy,gx,gy;
		cin >> ux >> uy >> vx >> vy >> gx >> gy;
		int sx = max(ux,vx)*2;
		int sy = max(uy,vy)*2;
		int dr = 0;
		gx*=2; gx++; gy*=2; gy++;
		gy=H*2-gy;
		
		
		if(sx!=min(ux,vx)*2) sx--;
		if(sy!=min(uy,vy)*2) sy--;
		
		sy=H*2-sy;
		
		f[sx][sy]=0;
		
		bool v[205][205][4]={0};
		
		for(int i=0; i<4; i++)
		{
			int tx=sx+dx[i], ty=sy+dy[i];
			if(tx<0||ty<0||tx>=2*W+1||ty>=2*H+1) continue;
			if(f[tx][ty]) continue;
			
			dr=i;
			break;
		}
		/*
		for(int i=0; i<=H*2; i++)
		{
			for(int j=0; j<=W*2; j++)
			{
				cout << f[j][i];
			}
			cout << endl;
		}
		*/
		
		int c=0;
		bool g=false;
		while(1)
		{
			if(v[sx][sy][dr]) break;
			v[sx][sy][dr]=1;
			
			if(sx==gx&&sy==gy)
			{
				g=true;
				cout << c << endl;
				break;
			}
			
			int tx=sx+dx[dr], ty=sy+dy[dr];

			if(f[tx][ty]) 
			{
				for(int i=0; i<4; i++)
				{
					int wx=sx+dx[(dr+3+i)%4], wy=sy+dy[(dr+3+i)%4];
					if(f[wx][wy]==0)
					{
						dr=(dr+3+i)%4;
						break;
					}
				}
			}
			else
			{
				int wx=sx+dx[(dr+3)%4], wy=sy+dy[(dr+3)%4];
				
				if(f[wx][wy]==0)
				{
					dr=(dr+3)%4;
				}
			}

			sx+=dx[dr]; sy+=dy[dr];
			if(c!=0)
			{
				sx+=dx[dr]; sy+=dy[dr];
			}
			if(sx<0||sy<0||sx>=2*W+1||sy>=2*H+1) break;
			c++;
			
		}
		
		if(!g) cout << "Impossible" << endl;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 1072
final_rank: 14