Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Antonelli Alen, https://trello.com/c/2rs9hoSl/32-2004n4d2p1-escapar-de-un-laberinto
- ///http://www.oia.unsam.edu.ar/_media/prob/c3a04n4p5.pdf
- ///primero chequea hasta que lugares se puede llegar desde las entradas y luego se fija si se llego a esos P lugares
- #include <iostream>
- #include <vector>
- using namespace std;
- int dx[] = { 0, 0, 1,-1}; // , 1, 1,-1,-1};
- int dy[] = {-1, 1, 0, 0}; // , 1,-1, 1,-1};
- struct Grafo {
- vector<string> ady;
- vector< vector<bool> > visit;
- int n, m, p;
- void leer ()
- {
- cin>>n>>m;
- ady.resize(n);
- visit = vector< vector<bool> > (n, vector<bool> (m, false) );
- for(int i=0; i<n; i++)
- cin>>ady[i];
- }
- bool visitable (int x, int y)
- {
- if (0<=x && x<n && 0<=y && y<m)
- if (ady[x][y]!='#')
- if (!visit[x][y])
- return true;
- return false;
- }
- void DFS (int x, int y)
- {
- visit[x][y]=true;
- for(int i=0; i<4; i++) /// 4
- {
- int vx = x+dx[i];
- int vy = y+dy[i];
- if ( visitable(vx,vy) )
- DFS(vx,vy);
- }
- }
- void flodfill ()
- {
- for(int i=0; i<max(n,m); i++) ///bordes
- {
- if ( visitable(i,0) )
- DFS(i,0);
- if ( visitable(0,i) )
- DFS(0,i);
- if ( visitable(i,m-1) )
- DFS(i,m-1);
- if ( visitable(n-1,i) )
- DFS(n-1,i);
- }
- cin>>p;
- cout<<p<<endl;
- int x, y;
- for(int i=0; i<p; i++)
- {
- cin>>x>>y;
- cout<<"["<<x<<"-"<<y<<"] -> ";
- if(visit[x-1][y-1])
- cout<<"SI"<<endl;
- else cout<<"NO"<<endl;
- }
- }
- };
- int main()
- {
- Grafo g;
- g.leer();
- g.flodfill();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment