forked from s-kachroo/SamsungPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jewel maze.cpp
143 lines (114 loc) · 3.59 KB
/
jewel maze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
https://blog.csdn.net/lihenair/article/details/17227667
http://www.mamicode.com/info-detail-995985.html
https://www.oipapio.com/cn/article-8650635
https://blog.csdn.net/broadCE/article/details/47959227
--------------------------------------------------------
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.
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.
Input
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 ).
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.
Output
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.
*/
#include<iostream>
#include<climits>
#define MAX 21
using namespace std;
int n, ans;
bool isValid(int i, int j){
return (i>=0 && i<n && j>=0 && j<n);
}
void printMatrix(int **arr){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
int dirX[] = {1,0,-1,0};
int dirY[] = {0,1,0,-1};
void jewelMaze(int **maze, int x, int y, int value, int **visited, int **path){
if(x == n-1 && y == n-1){
if(value >= ans){
ans = value;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
if(visited[i][j]){
path[i][j] = 3;
}
else{
path[i][j] = maze[i][j];
}
}
}
return;
}
for(int i=0; i<4; i++){
int newX = x + dirX[i];
int newY = y + dirY[i];
if(isValid(newX, newY)){
if(visited[newX][newY] == 0 && maze[newX][newY] == 0){
visited[newX][newY] = 1;
jewelMaze(maze, newX, newY, value, visited, path);
visited[newX][newY] = 0;
}
if(visited[newX][newY] == 0 && maze[newX][newY] == 2){
visited[newX][newY] = 1;
jewelMaze(maze, newX, newY, value+1, visited, path);
visited[newX][newY] = 0;
}
}
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> n;
int **maze = new int * [n + 1];
for(int i=0; i<n; i++){
maze[i] = new int[n + 1];
}
int **visited = new int * [n + 1];
for(int i=0; i<n; i++){
visited[i] = new int[n + 1];
}
int **path = new int * [n + 1];
for(int i=0; i<n; i++){
path[i] = new int[n + 1];
}
// Cleaner and input Maze
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> maze[i][j];
visited[i][j] = 0;
path[i][j] = 0;
}
}
ans = INT_MIN;
int sX = 0, sY = 0;
visited[sX][sY] = 1;
// printMatrix(maze);
if(maze[sX][sY] == 2)
jewelMaze(maze, sX, sY, 1, visited, path);
else
jewelMaze(maze, sX, sY, 0, visited, path);
cout << "Jewel collected : " << ans << endl;
cout << "Path traversed --> " << endl;
printMatrix(path);
cout << endl;
}
return 0;
}