// Sean's Juicer !!!!! DONE !!!!! #include #include // input char A[100][100]; long D[100*100 + 1]; // gives > 100*100 cameras const long big = 100*100; // ok int MaxX, MaxY; #define min(a, b) (a < b ? a : b) #define LEFT 1 #define RIGHT 2 #define UP 3 #define DOWN 4 int weight (int from, int to) { // ? int x = to % MaxX; int y = to / MaxX; if (A[y][x] == '0') return 1; else if (A[y][x] == 'E') return 1; else return big + 1; } int connect (int from, int how) { int x = from % MaxX; int y = from / MaxX; if (how == LEFT) return (x > 0 && A[y][x-1] != '1'); else if (how == RIGHT) return (x < MaxX-1 && A[y][x+1] != '1'); else if (how == DOWN) return (y+1 < MaxY && A[y+1][x] != '1'); else return (y > 0 && A[y-1][x] != '1'); } void update (int from, int to) { if (D[to] < 0) D[to] = D[from] + weight(from, to); else D[to] = min(D[to], D[from] + weight(from, to)); } void main() { ifstream in; in.open("rock.in"); ofstream out; out.open("rock.out"); while (1) { in >> MaxX >> MaxY; if (MaxX < 0) break; char buff[20]; in.getline(buff, 20); // skip line int start; for (int i=0; i < MaxY; i++) for (int j=0; j < MaxX; j++) { in >> A[i][j]; if (A[i][j] == 'S') start = j + i*MaxX; } int N = MaxX*MaxY; for (i=0; i < N+1; i++) D[i] = -1; D[start] = 0; // Dykstra's (Dietrich variant) for (i=0; i < N; i++) for (j=0; j < N; j++) if (D[j] >= 0) { // do the 4 neighbours if (connect(j, LEFT)) update(j, j-1); if (connect(j, RIGHT)) update(j, j+1); if (connect(j, DOWN)) update(j, j+MaxX); if (connect(j, UP)) update(j, j-MaxX); } // find exits int last = -1; for (i=0; i < MaxY; i++) for (j=0; j < MaxX; j++) if (A[i][j] == 'E') { // find smallest D... & mod by MAGIC int temp = j + i*MaxX; if (D[temp] >= 0 && (D[temp] < D[last] || last < 0)) last = temp; cerr << "Distance to (x,y) " << j << ", " << i << " = " << D[j+i*MaxX] % big << ";weight =" << D[j+i*MaxX] / big << endl; } if (last < 0) cout << "IMPOSSIBLE" << endl; else cout << "(" << last % MaxX << ", " << last / MaxX << ") " << D[last] % big << endl; out << D[last] / big << " " << D[last] % big << endl; } in.close(); out.close(); } Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.