Tuesday, 31 March 2015

                                                Maze




 Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.
Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.
Input
The first line contains three integers nmk (1 ≤ n, m ≤ 5000 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.
Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.
Output
Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").
It is guaranteed that a solution exists. If there are multiple solutions you can output any of them
Sample test(s)
input
3 4 2
#..#
..#.
#...
output
#.X#
X.#.
#...
input
5 4 5
#...
#.#.
.#..
...#
.#.#
output
#XXX
#X#.
X#..
...#
###################################### editorial ##########################################


Start  DFS from any free cell. As the maze is connected, this search will visit all s free cells. But we can stop the search when it visits s - k free cells. It's obvious that these s - k cells are connected to each other. Remaining k cells can be transformed into the walls.
Solutions which every move transform the cell which has the minimal number of neighbours passed pretests. However, it's wrong. Here is the counter-test:
....
.#..
..##
..##


##################################code#################################################
#include<iostream>
using namespace std;
    int n,m,k;
    int c=0;
    int s=0;
    char arr[510][510];
    #include<bits/stdc++.h>
     int   visited[510][510];
  void  dfs(int a,int b)
   {
    
                int re=s-k-1;  
            //     cout<<re<<endl;
        stack<pair<int,int> >s;
        s.push(make_pair(a,b));
        visited[a][b]=1;
         while(!s.empty())
           {
              int x=s.top().first;
              int y=s.top().second;
                  
                  s.pop();
                  int f=0;
                //   cout<<x<<" "<<y<<endl;
                  if(x+1<n && visited[x+1][y]==0 && arr[x+1][y]=='.' )
                   {
                      if(re<=0) arr[x+1][y]='X';
                      
                      visited[x+1][y]=1;
                        s.push(make_pair(x+1,y));
                        f=1;
                        re--;
                      
                   }
                  if(x-1>=0 && visited[x-1][y]==0 && arr[x-1][y]=='.' )
                   {
                     if(re<=0) arr[x-1][y]='X';
                      visited[x-1][y]=1;
                        s.push(make_pair(x-1,y));
                        f=1;
                        re--;
                      
                   }
                    if (y+1<m && visited[x][y+1]==0 && arr[x][y+1]=='.' )
                   {
                        if(re<=0) arr[x][y+1]='X';
                      visited[x][y+1]=1;
                        s.push(make_pair(x,y+1));
                        f=1;
                        re--;
                      
                   }
                    if(y-1>=0 && visited[x][y-1]==0 && arr[x][y-1]=='.' )
                   {
                     if(re<=0) arr[x][y-1]='X';
                      visited[x][y-1]=1;
                        s.push(make_pair(x,y-1));
                        f=1;
                      re--;
                   }
                //cout<<re<<endl;    
            
           }
   }
int main()
 {
     freopen("abc.txt","r",stdin);
 
    cin>>n>>m>>k;
     
      for(int i=0;i<n;i++)
       cin>>arr[i];
        for(int i=0;i<n;i++)
          for(int j=0;j<m;j++) if(arr[i][j]=='.') s++;
       int f=0;
      for(int i=0;i<n;i++)
       {
          for(int j=0;j<m;j++)
           {
              if(arr[i][j]=='.')
              {
               dfs(i,j);
                  f=1;
                 break;
              }
                
            
           }
            if(f==1)
             break;
       }
        for(int i=0;i<n;i++)
         {
            cout<<arr[i];
             cout<<endl;
         }
       return 0;
 }




No comments:

Post a Comment