Skip to content

Commit b76287d

Browse files
authored
Merge pull request div-bargali#827
Merge pull request div-bargali#827
2 parents 9a2f56c + dcb9417 commit b76287d

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<string> grid(10);
5+
vector<string> words;
6+
bool f;
7+
8+
void call(int ind)
9+
{
10+
if(!f) {
11+
return;
12+
}
13+
if(ind == words.size()) {
14+
if(f) {
15+
for(auto word: grid) {
16+
cout<<word<<endl;
17+
}
18+
f=false;
19+
}
20+
return;
21+
}
22+
int i,j,p,q,k;
23+
for(i=0;i<10;++i) {
24+
for(j=0;j<10;++j) {
25+
p=i,q=j;
26+
for(k=0;k<words[ind].size() && p+k<10;++k) {
27+
if(grid[p+k][q] != '-' && grid[p+k][q] != words[ind][k]) {
28+
break;
29+
}
30+
}
31+
32+
if(k==words[ind].size()) {
33+
vector<string> temp = grid;
34+
for(k=0;k<words[ind].size();++k) {
35+
grid[p+k][q] = words[ind][k];
36+
}
37+
call(ind+1);
38+
grid = temp;
39+
}
40+
41+
for(k=0;k<words[ind].size() && q+k<10;++k) {
42+
if(grid[p][q+k] != '-' && grid[p][q+k] != words[ind][k]) {
43+
break;
44+
}
45+
}
46+
47+
if(k==words[ind].size()) {
48+
vector<string> temp = grid;
49+
for(k=0;k<words[ind].size();++k) {
50+
grid[p][q+k] = words[ind][k];
51+
}
52+
call(ind+1);
53+
grid = temp;
54+
}
55+
}
56+
}
57+
}
58+
59+
int main()
60+
{
61+
f=true;
62+
63+
int i,j;
64+
for(i=0;i<10;++i) {
65+
cin>>grid[i];
66+
}
67+
68+
string s,w;
69+
cin>>w;
70+
71+
for(auto x: w) {
72+
if(x==';') {
73+
words.push_back(s);
74+
s="";
75+
} else
76+
s+=x;
77+
}
78+
words.push_back(s);
79+
call(0);
80+
81+
return 0;
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
void printSolution(int** solution,int n){
7+
for(int i=0;i<n;i++){
8+
for(int j=0;j<n;j++){
9+
cout << solution[i][j] << " ";
10+
}
11+
}
12+
cout<<endl;
13+
}
14+
void mazeHelp(int maze[][20],int n,int** solution,int x,int y){
15+
16+
17+
if(x == n-1 && y == n-1){
18+
solution[x][y] =1;
19+
printSolution(solution,n);
20+
solution[x][y] =0;
21+
return;
22+
}
23+
if(x>=n || x<0 || y>=n || y<0 || maze[x][y] ==0 || solution[x][y] ==1){
24+
return;
25+
}
26+
solution[x][y] = 1;
27+
mazeHelp(maze,n,solution,x-1,y);
28+
mazeHelp(maze,n,solution,x+1,y);
29+
mazeHelp(maze,n,solution,x,y-1);
30+
mazeHelp(maze,n,solution,x,y+1);
31+
solution[x][y] = 0;
32+
}
33+
void ratInAMaze(int maze[][20], int n){
34+
35+
int** solution = new int*[n];
36+
for(int i=0;i<n;i++){
37+
solution[i] = new int[n];
38+
}
39+
mazeHelp(maze,n,solution,0,0);
40+
41+
42+
}
43+
44+
45+
46+
int main(){
47+
48+
int n;
49+
cin >> n ;
50+
int maze[20][20];
51+
for(int i = 0; i < n ;i++){
52+
for(int j = 0; j < n; j++){
53+
cin >> maze[i][j];
54+
}
55+
}
56+
ratInAMaze(maze, n);
57+
}
58+
59+

0 commit comments

Comments
 (0)