# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
98086 | Ort | Clickbait (COCI18_clickbait) | C++14 | 28 ms | 5368 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
while(true) {
while((mat[y][x+1]=='-' || mat[y][x+1]=='+') && !visited[y][x+1]) {visited[y][x] = 3; x++;}
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++;}
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--;}
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) {
while(true) {
while((mat[y][x-1]=='-' || mat[y][x-1]=='+') && !visited[y][x-1]) {visited[y][x] = 3; x--;}
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++;}
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++;}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |