Advertisement
Guest User

pac-man-solved

a guest
Dec 22nd, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <sstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <queue>
  6. using namespace std;
  7. typedef pair<int,int> coord;
  8. typedef vector<vector<char> > mapa;
  9. typedef vector<vector<bool> > visitades;
  10. const int X[4]={1,-1,0,0};
  11. const int Y[4]={0,0,1,-1};
  12. const int XX[8]={1,-1,1,-1,0,0,1,-1};
  13. const int YY[8]={1,-1,-1,1,1,-1,0,0};
  14. int n;
  15. int m;
  16.  
  17. coord leer_mapa(mapa& M) {
  18.     coord inicial;
  19.     for (int i = 0; i < M.size(); ++i) {
  20.         for (int j = 0; j < M[i].size(); ++j) {
  21.             cin >> M[i][j];
  22.             if(M[i][j]=='P'){
  23.                 inicial.first=i;
  24.                 inicial.second=j;
  25.             }
  26.         }
  27.     }
  28.     return inicial;
  29. }
  30.  
  31. bool valid(coord act, mapa & M){
  32.     return(act.first < n and act.first > 0 and act.second < m and act.second >0 and M[act.first][act.second]!='X');
  33. }
  34.  
  35. bool bfs_pac_man(mapa& M, coord inicial , visitades& v){
  36.     queue<coord> usame;
  37.     usame.push(inicial);
  38.     v[inicial.first][inicial.second]=true;
  39.     bool s=true;
  40.     for(int k=0; k < 8 ;++k){
  41.             coord alrrededor;
  42.             alrrededor.first=inicial.first+XX[k];
  43.             alrrededor.second=inicial.second+YY[k];
  44.             if(M[alrrededor.first][alrrededor.second]=='F')s=false;
  45.     }
  46.     if(not s) return false;
  47.     while(not usame.empty()){
  48.         coord aux;
  49.         aux=usame.front();
  50.         usame.pop();
  51.         for(int i=0; i < 4; ++i){
  52.             coord act;
  53.             act.first=aux.first+X[i];
  54.             act.second=aux.second+Y[i];
  55.             bool b=valid(act,M);
  56.             if(b and not v[act.first][act.second]){
  57.                 bool s=true;   
  58.                 for(int k=0; k < 8 ;++k){
  59.                     coord alrrededor;
  60.                     alrrededor.first=act.first+XX[k];
  61.                     alrrededor.second=act.second+YY[k];
  62.                     if(M[alrrededor.first][alrrededor.second]=='F')s=false;
  63.                 }
  64.                 if (s){
  65.                      if(M[act.first][act.second]=='B') return true;
  66.                      v[act.first][act.second]=true;
  67.                      usame.push(act);
  68.                 }
  69.                
  70.             }
  71.         }
  72.     }
  73.     return false;  
  74. }
  75.  
  76.  
  77. int main(){
  78.     while(cin >> n >> m ){
  79.         mapa M (n, vector<char> (m));
  80.         visitades v (n , vector<bool> (m,false));
  81.         coord inicial;
  82.         inicial=leer_mapa(M);  
  83.         //cout << inicial.first << inicial.second << endl;
  84.        
  85.         bool b=bfs_pac_man(M,inicial,v);
  86.         if(b) cout << "yes" << endl;
  87.         else cout << "no" << endl;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement