Go to diff to previous submission
#define PROJECT 4 #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <limits> typedef unsigned long long ull; using namespace std; #if PROJECT == 1 int main() { int l, a; while (cin >> l >> a) { int pos; char dir; cin >> pos >> dir; } } #elif PROJECT == 2 #elif PROJECT == 3 #elif PROJECT == 4 struct task { int x, y; task(int _x, int _y) : x(_x), y(_y) {} }; int grid[100][100]; const int INF = /* numeric_limits<int>::max() - 1;*/ 100000; int r, c, gr, gc, lr, lc; void dump() { for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++ ) { cout << '\t' << (grid[i][j]); } endl(cout); } } int main() { while (cin >> r >> c >> gr >> gc >> lr >> lc) { //endl(cout); //cout << "#############################################################" << endl << endl; for (int i = 0; i < r; i++ ) { for (int j = 0; j < c; j++) { grid[i][j] = INF; } } gr--, gc--, lr--, lc--; bool flag = false; grid[gr][gc] = 0; queue<task> todo; todo.push(task(gr, gc)); while (!todo.empty() || flag) { task current = todo.front(); todo.pop(); //cout << "************************" << endl; // dump(); for (int i = -2; i <= 2; i++) { if (i == 0 || current.x + i < 0 || current.x + i > c - 1) continue; if (abs(i) == 1) { for (int j = -2; j <= 2; j += 4) { if (current.y + j < 0 || current.y + j > r - 1) continue; int tx = current.x + i; int ty = current.y + j; if (grid[tx][ty] == INF) { grid[tx][ty] = grid[current.x][current.y] + 1; todo.push(task(tx, ty)); if ((lr == tx) & (lc == ty)) flag == true; } } } else if (abs(i) == 2) { for (int j = -1; j <= 1; j += 2) { if (current.y + j < 0 || current.y + j > r - 1) continue; int tx = current.x + i; int ty = current.y + j; if (grid[tx][ty] == INF) { grid[tx][ty] = grid[current.x][current.y] + 1; todo.push(task(tx, ty)); if ((lr == tx) & (lc == ty)) flag == true; } } }else { //cerr << "dafuq?" << endl; } } } // cout << "&&&&&&&&&&" << endl; if (grid[lr][lc] == INF) { cout << "impossible" << endl; } else { cout << grid[lr][lc] << endl; } } return 0; } #elif PROJECT == 5 #elif PROJECT == 6 int main() { unsigned long long m, p, l, e ,r, s, n; while (cin >> m >> p >> l >> e >> r >> s >> n) { for (ull i = 0; i < n; i++) { ull m1, p1, l1; m1 = p / s; p1 = l / r; l1 = m * e; m = m1; p = p1; l = l1; } cout << m << endl; } return 0; } #endif
--- c4.s693.cteam098.grasshop.cpp.0.main.cpp +++ c4.s985.cteam098.grasshop.cpp.0.main.cpp @@ -35,33 +35,52 @@ int grid[100][100]; +const int INF = /* numeric_limits<int>::max() - 1;*/ 100000; + +int r, c, gr, gc, lr, lc; +void dump() { + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++ ) { + cout << '\t' << (grid[i][j]); + } + endl(cout); + } +} + int main() { - int r, c, gr, gc, lr, lc; while (cin >> r >> c >> gr >> gc >> lr >> lc) { + //endl(cout); + //cout << "#############################################################" << endl << endl; for (int i = 0; i < r; i++ ) { for (int j = 0; j < c; j++) { - grid[i][j] = numeric_limits<int>::max(); + grid[i][j] = INF; } } gr--, gc--, lr--, lc--; + bool flag = false; grid[gr][gc] = 0; queue<task> todo; todo.push(task(gr, gc)); - while (!todo.empty()) { - task next = todo.front(); todo.pop(); + while (!todo.empty() || flag) { + task current = todo.front(); todo.pop(); + //cout << "************************" << endl; + // dump(); for (int i = -2; i <= 2; i++) { - if (i == 0 || next.x + i < 0 || next.x + i > c - 1) continue; + if (i == 0 || current.x + i < 0 || current.x + i > c - 1) continue; if (abs(i) == 1) { for (int j = -2; j <= 2; j += 4) { - if (next.y + j < 0 || next.y + j > r - 1) continue; + if (current.y + j < 0 || current.y + j > r - 1) continue; - int tx = next.x + i; - int ty = next.y + j; - if (grid[ty][tx] > grid[next.y][next.x] + 1) { - grid[ty][tx] = grid[next.y][next.x] + 1; + int tx = current.x + i; + int ty = current.y + j; + if (grid[tx][ty] == INF) { + grid[tx][ty] = grid[current.x][current.y] + 1; todo.push(task(tx, ty)); + if ((lr == tx) & (lc == ty)) + flag == true; } + } } else if (abs(i) == 2) { @@ -68,16 +87,17 @@ for (int j = -1; j <= 1; j += 2) { - if (next.y + j < 0 || next.y + j > r - 1) continue; - - int tx = next.x + i; - int ty = next.y + j; - if (grid[ty][tx] > grid[next.y][next.x] + 1) { - grid[ty][tx] = grid[next.y][next.x] + 1; + if (current.y + j < 0 || current.y + j > r - 1) continue; + int tx = current.x + i; + int ty = current.y + j; + if (grid[tx][ty] == INF) { + grid[tx][ty] = grid[current.x][current.y] + 1; todo.push(task(tx, ty)); + if ((lr == tx) & (lc == ty)) + flag == true; } } }else { - cerr << "dafuq?" << endl; + //cerr << "dafuq?" << endl; } } @@ -86,5 +106,7 @@ } - if (grid[lr][lc] == numeric_limits<int>::max()) { + // cout << "&&&&&&&&&&" << endl; + + if (grid[lr][lc] == INF) { cout << "impossible" << endl;