Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool used[100][100];
- int dist[100][100];
- int n;
- cin >> n;
- for(int i = 1; i <= n; i++) {
- for(int j = 1; j <= n; j++) {
- dist[i][j] = (i == j ? 0 : 1e9);
- }
- }
- queue<pair<int, int> > q;
- for(int i = 1; i <= n; i++) {
- q.push(make_pair(i, i));
- }
- while(!q.empty()) {
- int x = q.front().first;
- int y = q.front().second;
- q.pop();
- for(int i = 1; y - i > 0; i <<= 1) {
- if(dist[x][y - i] == 1e9) {
- q.push(make_pair(x, y - i));
- dist[x][y - i] = dist[x][y] + 1;
- }
- }
- for(int i = 1; y + i <= n; i <<= 1) {
- if(dist[x][y + i] == 1e9) {
- q.push(make_pair(x, y + i));
- dist[x][y + i] = dist[x][y] + 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment