#include #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; map, list>> graph; //initialize to zero map, int> explored; //intialize to zero queue> q; //intialize to empty map, int> mydistance; //intialize to infinity void BFS(pair s) { q.push(s); mydistance[s] = 0; explored[s] = 1; while(!q.empty()) { pair u = q.front(); q.pop(); for (list>::iterator it = graph[u].begin(); it != graph[u].end(); ++it) { if (!explored[*it]) { q.push(*it); explored[*it]=1; mydistance[*it]=mydistance[u]+1; } } } } // undirected graph so minimun distance is given by BFS int main(){ int n; cin >> n; // your code goes here vector> res(n+1, vector(n+1, -1)); for (int i=1; i>(); mydistance.clear(); // constructing the graph for (int row=0; row>(); //graph.insert(make_pair(row,column), list>()); // inserting the position in the graph // explored.insert(make_pair(row,column)); explored[make_pair(row,column)] = 0; //mydistance.insert(make_pair(row,column)); mydistance[make_pair(row,column)] = +1000; // inserting edges if ( (row+i)=0 && (column+j)=0 ) graph[make_pair(row,column)].push_back(make_pair(row+i, column-j)); if ( (row-i)>=0 && (column-j)>=0 ) graph[make_pair(row,column)].push_back(make_pair(row-i, column-j)); if (i != j) { if ( (row+j)=0 && (column+i)=0 ) graph[make_pair(row,column)].push_back(make_pair(row+j, column-i)); if ( (row-j)>=0 && (column-i)>=0 ) graph[make_pair(row,column)].push_back(make_pair(row-j, column-i)); } }//end column loop }// end row loop BFS(make_pair(0,0)); int result = mydistance[make_pair(n-1,n-1)]; if (result != 1000) res[i][j] = result; } } for (int i=1; i= i)? res[i][j]:res[j][i]; cout <