#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(x, to) for (int x = 0; x < (to); x++) #define REP(x, a, to) for (int x = (a); x < (to); x++) #define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++) #define EPS (1e-14) using namespace std; typedef long long ll; typedef pair PII; typedef pair PLL; typedef complex Complex; typedef vector< vector > Mat; const int INF = 1e+9 + 7; int W, H, N, M; int imos_v[2005][2005]; int imos_h[2005][2005]; queue Q; int dist[1005][1005]; int di[] = {1, -1}; void update(int y, int x, int ny, int nx, int imos[2005][2005]) { if (ny < 0 || ny >= H) return; if (nx < 0 || nx >= W) return; if (imos[2*y][2*x] <= 0 || imos[2*ny][2*nx] <= 0) return; if (y == ny && imos[2*y][2*min(x,nx)+1] <= 0) return; if (x == nx && imos[2*min(y,ny)+1][2*x] <= 0) return; if (dist[y][x] + 1 >= dist[ny][nx]) return; dist[ny][nx] = dist[y][x] + 1; if (imos[2*y][2*x] <= 0 || imos[2*ny][2*nx] <= 0) return; Q.push(PII(ny, nx)); } void bfs() { rep(i, 1005) rep(j, 1005) { dist[i][j] = INF; } dist[0][0] = 0; Q.push(PII(0, 0)); while (!Q.empty()) { PII cur = Q.front(); Q.pop(); int y = cur.first; int x = cur.second; for (int i = 0, ny = 0, nx = 0; i < 2; i++) { ny = y + di[i]; nx = x; update(y, x, ny, nx, imos_v); ny = y; nx = x + di[i]; update(y, x, ny, nx, imos_h); } } } void solve() { for (int i = 0; i <= 2*H; i++) { for (int j = 0; j <= 2*W; j++) { imos_h[i][j] += j-1>=0 ? imos_h[i][j-1] : 0; imos_v[i][j] += i-1>=0 ? imos_v[i-1][j] : 0; } } #if 0 rep(i, 2*H + 1) rep(j, 2*W + 1) { printf("%3d%c", imos_v[i][j], j == 2*W ? '\n' : ' '); } printf("-----\n"); rep(i, 2*H + 1) rep(j, 2*W + 1) { printf("%3d%c", imos_h[i][j], j == 2*W ? '\n' : ' '); } printf("-----\n"); #endif bfs(); #if 0 rep(i, H) rep(j, W) { printf("%3d%c", dist[i][j], j == W-1 ? '\n' : ' '); } #endif if (dist[H-1][W-1] >= INF) { cout << "Odekakedekinai.." << endl; } else { cout << dist[H-1][W-1] << endl; } } PII toYX(int p) { int y = p / W; int x = p % W; return PII(y, x); } int main() { scanf("%d %d %d\n", &W, &H, &N); rep(i, N) { scanf("%d", &M); vector B(M+1); rep(j, M+1) scanf("%d", &B[j]); rep(j, M) { PII s = toYX(B[j]); PII e = toYX(B[j+1]); int sy = s.first * 2; int sx = s.second * 2; int ey = e.first * 2; int ex = e.second * 2; // printf("(%d,%d)->(%d,%d)\n", sx, sy, ex, ey); if (sx == ex) { // vertical int py = min(sy, ey); int qy = max(sy, ey); imos_v[py][sx] += 1; imos_v[qy+1][sx] -= 1; } else { // horizontal int px = min(sx, ex); int qx = max(sx, ex); imos_h[sy][px] += 1; imos_h[sy][qx+1] -= 1; } } // printf("=====\n"); } solve(); return 0; }