# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
115740 | Breno_XD | Retro (COCI17_retro) | C++14 | 1085 ms | 512 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAX 310
string seq, ans;
int r, s, x, y;
char v[MAX][MAX];
bool valid(int i, int j){
if(i >= 0 && i <= r && j >= 1 && j <= s)return true;
return false;
}
void dfs(int i, int j){
if(v[i][j] == '*' || (i < 1 && j >= 1 && j <= s)){
stack<char> pilha;
bool ok = true;
for(int i = 0; i < seq.size(); i++){
if(seq[i] == '('){
pilha.push('(');
}else if(seq[i] == ')'){
if(pilha.size() > 0){
pilha.pop();
}else{
ok = false;
break;
}
}
}
if(pilha.size() > 0) ok = false;
if(ok && seq.size() > ans.size()){
ans = seq;
}else if(ok && seq.size() == ans.size()){
for(int i = 0; i < seq.size(); i++){
if(ans[i] == ')' && seq[i] == '('){
ans = seq;
break;
}
}
}
return;
}
seq += v[i][j];
if(valid(i-1, j-1)){
int tmp = seq.size();
dfs(i-1, j-1);
if(seq.size() > tmp)
seq.erase(seq.begin()+seq.size()-1);
}
if(i-1, j){
int tmp = seq.size();
dfs(i-1, j);
if(seq.size() > tmp)
seq.erase(seq.begin()+seq.size()-1);
}
if(valid(i-1, j+1)){
int tmp = seq.size();
dfs(i-1, j+1);
if(seq.size() > tmp)
seq.erase(seq.begin()+seq.size()-1);
}
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> r >> s;
for(int i = 1; i <= r; i++){
for(int j = 1; j <= s; j++){
cin >> v[i][j];
if(v[i][j] == 'M'){
x = i; y = j;
}
}
}
dfs(x, y);
string ans2 = "";
for(int i = 0; i < ans.size(); i++){
if(ans[i] == '(' || ans[i] == ')'){
ans2 += ans[i];
}
}
cout << ans2.size() << "\n";
cout << ans2 << "\n";
/*
for(int i = 0; i < ans2.size(); i++){
cout << ans2[i] << " ";
}
cout << "\n";
*/
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |