# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98077 | Ort | Clickbait (COCI18_clickbait) | C++14 | 22 ms | 6144 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<iostream>
#include<cctype>
#include<string>
#include<vector>
#define MAX 1005
#define fst first
#define sec second
using namespace std;
char mat[MAX][MAX];
int visited[MAX][MAX];
vector<int> sol;
int n, m, start1i, start1j, nx, ny;
pair<int,int> getnext_right(int y, int x) {
visited[y][x] = 3;
while(1) {
while((mat[y][x+1]=='-' || mat[y][x+1]=='+') && !visited[y][x+1]) {
visited[y][x] = 3;
x++;
}
visited[y][x] = 3;
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x-1]=='-')) return make_pair(x,y+2);
while((mat[y+1][x]=='|' || mat[y+1][x]=='+') && !visited[y+1][x]) {
visited[y][x] = 3;
y++;
}
visited[y][x] = 3;
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x-1]=='-')) return make_pair(x,y+2);
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
while((mat[y][x-1]=='-' || mat[y][x-1]=='+') && !visited[y][x-1]) {
visited[y][x] = 3;
x--;
}
visited[y][x] = 3;
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='-')) return make_pair(x,y+1);
}
}
pair<int,int> getnext_left(int y, int x) {
visited[y][x] = 3;
while(1) {
while((mat[y][x-1]=='-' || mat[y][x-1]=='+') && !visited[y][x-1]) {
visited[y][x] = 3;
x--;
}
visited[y][x] = 3;
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x-1]=='-')) return make_pair(x,y+2);
while((mat[y+1][x]=='|' || mat[y+1][x]=='+') && !visited[y+1][x]) {
visited[y][x] = 3;
y++;
}
visited[y][x] = 3;
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x-1]=='-')) return make_pair(x,y+2);
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
while((mat[y][x+1]=='-' || mat[y][x+1]=='+') && !visited[y][x+1]) {
visited[y][x] = 3;
x++;
}
visited[y][x] = 3;
if((mat[y-1][x+1]=='+' || mat[y-1][x+1]=='|') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='|')) return make_pair(x+2,y);
if((mat[y+1][x-1]=='+' || mat[y+1][x-1]=='-') && (mat[y+1][x+1]=='+' || mat[y+1][x+1]=='-')) return make_pair(x,y+1);
}
}
int process(int y, int x) {
string ss = "";
int l = x, r = x;
while(isdigit(mat[y][l])) l--; l++;
while(isdigit(mat[y][r])) r++;
for(int j=l;j<r;j++) {
visited[y][j] = 1; ss += (mat[y][j]);
}
return stoi(ss);
}
void traverse(int y, int x) {
int num = -1;
if(isdigit(mat[y][x]) && !visited[y][x]) num = process(y,x);
while(mat[y+1][x]!='-' && mat[y+1][x]!='+') y++;
while(mat[y-1][x]!='-' && mat[y-1][x]!='+') {
int l = x; int r = x;
while(mat[y][l-1]!='|') {
if(isdigit(mat[y][l]) && !visited[y][l]) num = process(y,l);
l--;
}
while(mat[y][r+1]!='|') {
if(isdigit(mat[y][r]) && !visited[y][r]) num = process(y,r);
r++;
}
if(isdigit(mat[y][l]) && !visited[y][l]) num = process(y,l);
if(isdigit(mat[y][r]) && !visited[y][r]) num = process(y,r);
if(mat[y][r+1]=='|' && mat[y][r+2]=='-') {
pair<int,int> p = getnext_right(y, r+2);
traverse(p.sec,p.first);
}
if(mat[y][l-1]=='|' && mat[y][l-2]=='-') {
pair<int,int> p = getnext_left(y, l-2);
traverse(p.sec,p.first);
}
y--;
}
int l = x; int r = x;
while(mat[y][l-1]!='|') {
if(isdigit(mat[y][l]) && !visited[y][l]) num = process(y,l);
l--;
}
while(mat[y][r+1]!='|') {
if(isdigit(mat[y][r]) && !visited[y][r]) num = process(y,r);
r++;
}
if(isdigit(mat[y][l]) && !visited[y][l]) num = process(y,l);
if(isdigit(mat[y][r]) && !visited[y][r]) num = process(y,r);
if(mat[y][r+1]=='|' && mat[y][r+2]=='-') {
pair<int,int> p = getnext_right(y, r+2);
traverse(p.sec,p.first);
}
if(mat[y][l-1]=='|' && mat[y][l-2]=='-') {
pair<int,int> p = getnext_left(y, l-2);
traverse(p.sec,p.first);
}
sol.push_back(num);
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
cin >> n >> m;
for(int i=0;i<n+4;i++) for(int j=0;j<m+4;j++) mat[i][j] = '.';
for(int i=1;i<n+1;i++) for(int j=1;j<m+1;j++) cin >> mat[i][j];
for(int i=1;i<n+1;i++) for(int j=1;j<m+1;j++) {if(mat[i][j]=='1' && !isdigit(mat[i][j-1]) && !isdigit(mat[i][j+1])) {start1i = i; start1j = j;}};
traverse(start1i,start1j);
for(auto i:sol) cout << i << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |