Go to diff to previous submission
#include <iostream> #include <algorithm> #include <cstdlib> #include <queue> #include <climits> #include <cstdio> using namespace std; int field[105][105]; bool vis[105][105]; int x[] = {1,2,2,1,-1,-2,-2,-1}; int y[] = {-2,-1,1,2,2,1,-1,-2}; int r,c,gx,gy,lx,ly; bool check(int x, int y){ if(x < 1 || x > c || y < 1 || y > r) return false; return true; } int main(){ while(cin >> r >> c>>gy>>gx>>ly>>lx){ for(int i = 0; i < r + 1; i++) for(int j = 0; j < c + 1; j++){ vis[i][j] = false; field[i][j] = INT_MAX >> 1; } queue < pair <int,int> > q; q.push( pair<int,int>(gx,gy) ); field[gy][gx] = 0; vis[gy][gx] = true; bool ok = false; pair<int,int> fq; while(!q.empty()){ fq = q.front(); q.pop(); if(fq.first == lx && fq.second == ly){ ok = true; break; } for(int i = 0; i < 8; i++){ if(check(fq.first + x[i], fq.second + y[i])) if(!vis[fq.second + y[i]][fq.first + x[i]]){ vis[fq.second + y[i]][fq.first + x[i]] = true; field[fq.second + y[i]][fq.first + x[i]] = field[fq.second][fq.first] + 1; q.push( pair<int,int>(fq.first + x[i],fq.second + y[i]) ); } } } /* for(int i = 0; i < r + 1; i++){ for(int j = 0; j < c + 1; j++){ cout << field[i][j] << " "; } cout << endl; } */ if(ok) cout << field[ly][lx] << endl; else cout << "impossible" << endl; } return 0; }
--- c4.s636.cteam042.grasshop.cpp.0.grasshop.cpp +++ c4.s672.cteam042.grasshop.cpp.0.grasshop.cpp @@ -17,5 +17,5 @@ bool check(int x, int y){ - if(x < 1 || x >= c || y < 1 || y >= r) + if(x < 1 || x > c || y < 1 || y > r) return false; return true; @@ -26,6 +26,8 @@ while(cin >> r >> c>>gy>>gx>>ly>>lx){ for(int i = 0; i < r + 1; i++) - for(int j = 0; j < c + 1; j++) + for(int j = 0; j < c + 1; j++){ vis[i][j] = false; + field[i][j] = INT_MAX >> 1; + } queue < pair <int,int> > q; @@ -54,4 +56,13 @@ } } +/* + for(int i = 0; i < r + 1; i++){ + for(int j = 0; j < c + 1; j++){ + cout << field[i][j] << " "; + } + cout << endl; + } +*/ + if(ok) cout << field[ly][lx] << endl;