Advertisement
ProgramoBien

reinos jutge

May 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. typedef vector<vector<int>> vvi;
  8. typedef vector<int> vi;
  9.  
  10. vvi d;
  11. vector<string> r;
  12. vector<string> t; //quien llega primero
  13. queue<pair<int,int>> q;
  14. int f,c;
  15. int df[] = {0,1,0,-1};
  16. int dc[] = {1,0,-1,0};
  17.  
  18. void bfs(int ff, int cc, int ds, char ch){
  19.     if(ff<0 || cc<0) return;
  20.     if(ff>=f || cc>=c) return;
  21.     if(r[ff][cc]=='#') return;
  22.     if(r[ff][cc]=='*') return;
  23.     if(ds>d[ff][cc] && d[ff][cc]!=-1) return;
  24.     //if(d[ff][cc]>0 && d[ff][cc])
  25.  
  26.     if(r[ff][cc]=='.'){
  27.         r[ff][cc]=ch;
  28.         q.push(make_pair(ff,cc));
  29.         d[ff][cc]=ds;
  30.     }
  31.     else {
  32.         if(ds == d[ff][cc] && ch!=r[ff][cc])
  33.             r[ff][cc]='*';
  34.     }
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40.     while(cin>>f){
  41.         cin>>c;
  42.         vvi dist(f, vi (c,-1));
  43.         d = dist;
  44.         vector<string> rr(f, string (c,'7'));  //rr es el mapa con los reinos
  45.         r=rr;
  46.  
  47.         for(int i =0;i<f;++i){
  48.             cin>> r[i];
  49.             for(int dc=0;dc<c;++dc){
  50.                 char ch = r[i][dc];
  51.                 if(ch >='A' && ch<='Z'){
  52.                     q.push(make_pair (i,dc));
  53.                     d[i][dc]=0;
  54.                 }
  55.             }
  56.         }
  57.         string g; cin>>g;
  58.  
  59.         while(!q.empty()){
  60.             pair<int,int>
  61.             p=q.front();
  62.             q.pop();
  63.             int ds, ff=p.first, cc=p.second;
  64.             char ch = r[ff][cc];
  65.             ds=d[ff][cc];
  66.  
  67.             for(int i=0; i<4;++i){
  68.                 bfs(ff + df[i], cc + dc[i], ds + 1,ch);
  69.             }
  70.  
  71.             /*for(auto w: r){ //para ver como se van asignando los caracteres
  72.                 cerr<<w<<endl; //es decir, pasito a pasito del BFS
  73.             } cerr<<endl;*/
  74.  
  75.         }
  76.  
  77.         for(auto w: r){
  78.                 cout<<w<<endl;
  79.         } cout<<"---\n";
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement