Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n, m;
- bitset<1100007> vis;
- bitset<1100007> matriz;
- struct god{
- int x, y;
- god(){}
- god(int newx, int newy): x(newx), y(newy) {}
- };
- int mx[] = {-1, 0, 0, 1};
- int my[] = {0, -1, 1, 0};
- bool checa(int x, int y) {
- if(x >= 0 && x <= n && y >= 0 && y <= m && !vis[(x*m)+ y]){
- return true;
- }
- return false;
- }
- int bfs(god loja) {
- queue<god> Q;
- Q.push(loja);
- if(matriz[(loja.x*m)+ loja.y]){
- printf("%d %d\n", loja.x, loja.y);
- return 0;
- }
- while(!Q.empty()) {
- god top = Q.front();
- Q.pop();
- for(int i = 0; i < 4; i++) {
- int tx = top.x + mx[i];
- int ty = top.y + my[i];
- if(checa(tx, ty)) {
- if(matriz[(tx*m)+ty]){
- printf("%d %d\n", tx, ty);
- return 0;
- }
- vis[(tx*m)+ty] = true;
- Q.push(god(tx, ty));
- }
- }
- }
- printf("10000 10000\n");
- return 0;
- }
- int main() {
- int inst = 1;
- while(true) {
- scanf("%d %d",&n, &m);
- if(n == 0 || m == 0)break;
- if(inst != 1) printf("\n");
- int num_lojas;
- matriz.reset();
- scanf("%d" , &num_lojas);
- for(int j = 0; j < num_lojas; j++) {
- int x, y;
- scanf("%d %d", &x, &y);
- matriz[(x*m)+ y] = 1;
- }
- int pedidos;
- scanf("%d", &pedidos);
- printf("Instancia %d\n", inst);
- inst++;
- for(int i = 0; i < pedidos; i++){
- god pos;
- scanf("%d %d",&pos.x, &pos.y);
- vis.reset();
- bfs(pos);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement