Submission #98086

# Submission time Handle Problem Language Result Execution time Memory
98086 2019-02-20T14:44:37 Z Ort Clickbait (COCI18_clickbait) C++14
120 / 120
28 ms 5368 KB
#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;
}

Compilation message

clickbait.cpp: In function 'int process(int, int)':
clickbait.cpp:47:2: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  while(isdigit(mat[y][l])) l--; l++;
  ^~~~~
clickbait.cpp:47:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  while(isdigit(mat[y][l])) l--; l++;
                                 ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 768 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 768 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 5 ms 1536 KB Output is correct
9 Correct 7 ms 3968 KB Output is correct
10 Correct 28 ms 5368 KB Output is correct