tsypko

Untitled

May 30th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1.     bool used[100][100];
  2.     int dist[100][100];
  3.  
  4.     int n;
  5.     cin >> n;
  6.  
  7.     for(int i = 1; i <= n; i++) {
  8.         for(int j = 1; j <= n; j++) {
  9.             dist[i][j] = (i == j ? 0 : 1e9);
  10.         }
  11.     }
  12.  
  13.     queue<pair<int, int> > q;
  14.     for(int i = 1; i <= n; i++) {
  15.         q.push(make_pair(i, i));
  16.     }
  17.  
  18.     while(!q.empty()) {
  19.         int x = q.front().first;
  20.         int y = q.front().second;
  21.         q.pop();
  22.         for(int i = 1; y - i > 0; i <<= 1) {
  23.             if(dist[x][y - i] == 1e9) {
  24.                 q.push(make_pair(x, y - i));
  25.                 dist[x][y - i] = dist[x][y] + 1;
  26.             }
  27.         }
  28.         for(int i = 1; y + i <= n; i <<= 1) {
  29.             if(dist[x][y + i] == 1e9) {
  30.                 q.push(make_pair(x, y + i));
  31.                 dist[x][y + i] = dist[x][y] + 1;
  32.             }
  33.         }
  34.  
  35.     }
Add Comment
Please, Sign In to add comment