Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <queue>
- using namespace std;
- typedef pair<int,int> coord;
- typedef vector<vector<char> > mapa;
- typedef vector<vector<bool> > visitades;
- const int X[4]={1,-1,0,0};
- const int Y[4]={0,0,1,-1};
- const int XX[8]={1,-1,1,-1,0,0,1,-1};
- const int YY[8]={1,-1,-1,1,1,-1,0,0};
- int n;
- int m;
- coord leer_mapa(mapa& M) {
- coord inicial;
- for (int i = 0; i < M.size(); ++i) {
- for (int j = 0; j < M[i].size(); ++j) {
- cin >> M[i][j];
- if(M[i][j]=='P'){
- inicial.first=i;
- inicial.second=j;
- }
- }
- }
- return inicial;
- }
- bool valid(coord act, mapa & M){
- return(act.first < n and act.first > 0 and act.second < m and act.second >0 and M[act.first][act.second]!='X');
- }
- bool bfs_pac_man(mapa& M, coord inicial , visitades& v){
- queue<coord> usame;
- usame.push(inicial);
- v[inicial.first][inicial.second]=true;
- bool s=true;
- for(int k=0; k < 8 ;++k){
- coord alrrededor;
- alrrededor.first=inicial.first+XX[k];
- alrrededor.second=inicial.second+YY[k];
- if(M[alrrededor.first][alrrededor.second]=='F')s=false;
- }
- if(not s) return false;
- while(not usame.empty()){
- coord aux;
- aux=usame.front();
- usame.pop();
- for(int i=0; i < 4; ++i){
- coord act;
- act.first=aux.first+X[i];
- act.second=aux.second+Y[i];
- bool b=valid(act,M);
- if(b and not v[act.first][act.second]){
- bool s=true;
- for(int k=0; k < 8 ;++k){
- coord alrrededor;
- alrrededor.first=act.first+XX[k];
- alrrededor.second=act.second+YY[k];
- if(M[alrrededor.first][alrrededor.second]=='F')s=false;
- }
- if (s){
- if(M[act.first][act.second]=='B') return true;
- v[act.first][act.second]=true;
- usame.push(act);
- }
- }
- }
- }
- return false;
- }
- int main(){
- while(cin >> n >> m ){
- mapa M (n, vector<char> (m));
- visitades v (n , vector<bool> (m,false));
- coord inicial;
- inicial=leer_mapa(M);
- //cout << inicial.first << inicial.second << endl;
- bool b=bfs_pac_man(M,inicial,v);
- if(b) cout << "yes" << endl;
- else cout << "no" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement