ann8497

jewels samsung

Aug 16th, 2019
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /*
  2. There is a maze that has one entrance and one exit. Jewels are placed in passages of the maze. You want to pick up the jewels after getting into the maze through the entrance and before getting out of it through the exit. You want to get as many jewels as possible, but you don’t want to take the same passage you used once.
  3.  
  4. When locations of a maze and jewels are given, find out the greatest number of jewels you can get without taking the same passage twice, and the path taken in this case.
  5.  
  6. Input
  7. There can be more than one test case in the input file. The first line has T, the number of test cases. Then the totally T test cases are provided in the following lines (T ≤ 10 ).
  8.  
  9. In each test case, In the first line, the size of the maze N (1 ≤ N ≤ 10) is given. The maze is N×N square-shaped. From the second line through N lines, information of the maze is given. “0” means a passage, “1” means a wall, and “2” means a location of a jewel. The entrance is located on the upper-most left passage and the exit is located on the lower-most right passage. There is no case where the path from the entrance to the exit doesn’t exist.
  10.  
  11. Output
  12. From the first line through N lines, mark the path with 3 and output it. In N+1 line, output the greatest number of jewels that can be picked up. Each test case must be output separately as a empty.
  13.  
  14. MAX DIAMONDS COLLECTED AND ITS PATH IS THE OUTPUT.
  15.  
  16. */
  17.  
  18. #include<iostream>
  19. using namespace std;
  20.  
  21. int n;
  22. int a[100][100];
  23.  
  24. int dx[] = {-1,0,1,0};
  25. int dy[] = {0,1,0,-1};
  26.  
  27. bool valid(int x, int y){
  28.     return ((a[x][y] == 0 || a[x][y] == 2) && x>=0 && x<n && y>=0 && y<n);
  29. }
  30.  
  31. int ans[50][50];
  32. //int paths;
  33. int value = -100;
  34.  
  35. void print(){
  36.     for(int i = 0; i<n;i++){
  37.         for(int j = 0; j<n; j++){
  38.             cout<<ans[i][j]<<" ";
  39.         }
  40.         cout<<endl;
  41.     }
  42.     cout<<endl;
  43. }
  44.  
  45. void solve(int r, int c, int diamonds){
  46.    
  47.     if(r == n-1 && c == n-1){
  48.         if(diamonds>value){
  49.           value = diamonds;
  50.           for(int i = 0; i<n; i++){
  51.              for(int j = 0; j<n; j++){
  52.                 ans[i][j] = a[i][j];
  53.                 //print();
  54.              }
  55.           }
  56.         }
  57.     }
  58.    
  59.     for(int i=0; i<4; i++){
  60.        
  61.         int x = r + dx[i];
  62.         int y = c + dy[i];
  63.        
  64.         if(valid(x,y)){
  65.            
  66.             int check = (a[x][y] == 2) ? 1:0;
  67.             a[x][y] = 3;
  68.             solve(x,y,diamonds + check);
  69.             a[x][y] = (check == 1) ? 2:0;
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. int main(){
  76.    
  77.     cin>>n;
  78.     for(int i =0; i<n; i++)
  79.     for(int j =0; j<n; j++)
  80.     cin>>a[i][j];
  81.    
  82.     /* here 2 is diamond
  83.        0 means a passage
  84.        1 means a wall
  85.       */
  86.     //paths = 0;
  87.     value = -100;
  88.     a[0][0] = 3;
  89.     solve(0,0,0);
  90.     cout<<value<<endl;
  91.     print();
  92.    
  93.     return 0;
  94. }
Add Comment
Please, Sign In to add comment