Advertisement
Manioc

steak

Nov 19th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m;
  6. bitset<1100007> vis;
  7. bitset<1100007> matriz;
  8.  
  9. struct god{
  10.   int x, y;
  11.   god(){}
  12.   god(int newx, int newy): x(newx), y(newy) {}
  13.  
  14. };
  15.  
  16. int mx[] = {-1, 0, 0, 1};
  17. int my[] = {0, -1, 1, 0};
  18.  
  19. bool checa(int x, int y) {
  20.     if(x >= 0 && x <= n && y  >= 0 && y <= m && !vis[(x*m)+ y]){
  21.         return true;  
  22.     }
  23.     return false;
  24. }
  25.  
  26. int bfs(god loja) {
  27.     queue<god> Q;
  28.     Q.push(loja);
  29.  
  30.     if(matriz[(loja.x*m)+ loja.y]){
  31.         printf("%d %d\n", loja.x, loja.y);
  32.         return 0;
  33.     }
  34.     while(!Q.empty()) {
  35.         god top = Q.front();
  36.         Q.pop();
  37.  
  38.         for(int i = 0; i < 4; i++) {
  39.             int tx = top.x + mx[i];
  40.             int ty = top.y + my[i];
  41.             if(checa(tx, ty)) {
  42.                 if(matriz[(tx*m)+ty]){
  43.                     printf("%d %d\n", tx, ty);
  44.                     return 0;
  45.                 }
  46.                 vis[(tx*m)+ty] = true;
  47.                 Q.push(god(tx, ty));
  48.             }
  49.         }
  50.     }
  51.     printf("10000 10000\n");
  52.     return 0;
  53. }
  54.  
  55. int main() {
  56.    
  57.     int inst = 1;
  58.     while(true) {
  59.         scanf("%d %d",&n, &m);
  60.         if(n == 0 || m == 0)break;
  61.         if(inst != 1) printf("\n");
  62.  
  63.         int num_lojas;
  64.         matriz.reset();
  65.         scanf("%d" , &num_lojas);
  66.  
  67.         for(int j = 0; j < num_lojas; j++) {
  68.             int x, y;
  69.             scanf("%d %d", &x, &y);
  70.             matriz[(x*m)+ y] = 1;
  71.         }
  72.  
  73.         int pedidos;
  74.         scanf("%d", &pedidos);
  75.         printf("Instancia %d\n", inst);
  76.         inst++;
  77.         for(int i = 0; i < pedidos; i++){
  78.             god pos;
  79.             scanf("%d %d",&pos.x, &pos.y);
  80.             vis.reset();
  81.             bfs(pos);
  82.         }
  83.     }
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement