#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; set > possibleMoves(int x, int y, int a, int b) { set > possibleMoves; possibleMoves.insert(make_pair(x+a, y+b)); possibleMoves.insert(make_pair(x+a, y-b)); possibleMoves.insert(make_pair(x-a, y+b)); possibleMoves.insert(make_pair(x-a, y-b)); possibleMoves.insert(make_pair(x+b, y+a)); possibleMoves.insert(make_pair(x+b, y-a)); possibleMoves.insert(make_pair(x-b, y+a)); possibleMoves.insert(make_pair(x-b, y-a)); return possibleMoves; } int minimumMoves(int a, int b, int n) { int n_moves = -1; int steps = 0; set > visited; set > to_visit; pair origin = make_pair(0,0); pair destination = make_pair(n-1,n-1); to_visit.insert(origin); while(to_visit.size() > 0) { set > to_visit_in_next_step; for(set >::iterator it=to_visit.begin(); it!=to_visit.end(); ++it) { if(visited.find(*it) != visited.end()) continue; visited.insert(*it); int x1 = (*it).first; int y1 = (*it).second; set > possible_moves = possibleMoves(x1,y1,a,b); for(set >::iterator itp = possible_moves.begin(); itp != possible_moves.end(); ++itp) { int x2 = (*itp).first; int y2 = (*itp).second; if(x2 >= 0 && x2 < n && y2 >= 0 && y2 < n) to_visit_in_next_step.insert(make_pair(x2,y2)); } } to_visit = to_visit_in_next_step; steps++; if(to_visit_in_next_step.find(destination) != to_visit_in_next_step.end()) { n_moves = steps; break; } } return n_moves; } int main(){ map, int> moves; pair idx; int n; cin >> n; // your code goes here for(int i=1; i