Submission
Status:
---xxxxxxx
Subtask/Task Score:
0/100
Score: 0
User: spammer_destroyer
Problemset: อโมกุส
Language: cpp
Time: 0.002 second
Submitted On: 2025-10-24 11:59:23
//find a way to exit the maze
//from top left to bottom right
#include <iostream>
#include <vector>
using namespace std;
void show(vector<vector<char>> arr, vector<vector<bool>> saved_path, int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
if(saved_path[i][j]==true) {
cout << ".";
}
else {
cout << arr[i][j];
}
}
cout << "\n";
}
}
void find(vector<vector<char>>& arr, vector<vector<bool>>& visited, vector<vector<bool>>& saved_path, int n, int I, int J) {
//use I,J as position
int i,j;
//saved the path we take
if(saved_path[I][J]==false){saved_path[I][J]=true;}
else if(saved_path[I][J]==true){saved_path[I][J]=false;}
//right->down->left->up
if(arr[I][J]=='E') {
return;
}
else if(visited[I][J+1]==false&&saved_path[I][J+1]==false&&J+1<n) {
find(arr,visited,saved_path,n,I,J+1);
}//right
else if(visited[I+1][J]==false&&saved_path[I+1][J]==false&&I+1<n&&I!=3&&J!=5) {
find(arr,visited,saved_path,n,I+1,J);
//cout << I << "," << J << "\n";
}//down
else if(visited[I][J-1]==false&&saved_path[I][J-1]==false&&J-1<n) {
find(arr,visited,saved_path,n,I,J-1);
}//left
else if(visited[I-1][J]==false&&saved_path[I-1][J]==false&&I-1<n) {
find(arr,visited,saved_path,n,I-1,J);
}//up
//step back after the path is explored
else if(saved_path[I][J+1]==true&&J+1<n) {
visited[I][J]=true;
find(arr,visited,saved_path,n,I,J+1);
}
else if(saved_path[I+1][J]==true&&I+1<n) {
visited[I][J]=true;
find(arr,visited,saved_path,n,I+1,J);
}
else if(saved_path[I][J-1]==true&&J-1<n) {
visited[I][J]=true;
find(arr,visited,saved_path,n,I,J-1);
}
else if(saved_path[I-1][J]==true&&I-1<n) {
visited[I][J]=true;
find(arr,visited,saved_path,n,I-1,J);
}
}
int main()
{
int i,j,n;
cin >> n;
vector<vector<char>> arr(n,vector<char>(n));
vector<vector<bool>> visited(n,vector<bool>(n,false));
vector<vector<bool>> saved_path(n,vector<bool>(n,false));
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
cin >> arr[i][j];
}
}
arr[0][0]='S';
arr[n-1][n-1]='E';
find(arr,visited,saved_path,n,0,0);
show(arr,saved_path,n);
return 0;
}