Submission #1350380

#TimeUsernameProblemLanguageResultExecution timeMemory
1350380vjudge1Clickbait (COCI18_clickbait)C++20
108 / 120
12 ms5324 KiB
#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
vector<pair<int, int>> ma[N];
void dfs(int x)
{
	sort(rbegin(ma[x]), rend(ma[x]));
	for (auto y : ma[x])
	{
		dfs(y.second);
	}
	cout << x << endl;
}
int main()
{
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	char g[n][m];
	int val[n][m] = {0};
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> g[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (val[i][j])
				continue;
			if ('0' <= g[i][j] and g[i][j] <= '9')
			{
				int num = 0;
				{
					int k = j;
					while (k < m and '0' <= g[i][k] and g[i][k] <= '9')
					{
						num = (num * 10) + g[i][k] - '0';
						k++;
					}
				}
				{
					int p = i, q = j;
					// cout<<"Gol "<<p<<' '<<q<<endl;
					while (g[p][q] != '-')
						p--;
					while (g[p][q] != '+')
						q--;
					// // (p,q-1)
					int fr = p, fc = q;
					q++;
					while (g[p][q] != '+')
						q++;
					int lc = q;
					p++;
					while (g[p][q] != '+')
						p++;
					int lr = p;
					for (int ip = fr; ip <= lr; ip++)
					{
						for (int jp = fc; jp <= lc; jp++)
						{
							val[ip][jp] = num;
						}
					}
					// cout<<"Found "<<num<<endl;
					// cout<<"Rec: ";
					// cout<<fr<<' '<<lr<<' '<<fc<<' '<<lc<<endl;

					p = fr;
					q = fc + 1;
					while (g[p][q] == '-')
					{
						if (p > 0 and g[p - 1][q] == '|')
						{
							queue<pair<int, int>> qe;
							qe.push({p - 1, q});
							val[p - 1][q] = num;
							int lstx = -1, lsty = -1;
							while (qe.size())
							{
								auto it = qe.front();
								qe.pop();
								int x = it.first, y = it.second;
								lstx = x, lsty = y;
								// cout<<x<<' '<<y<<endl;
								if (g[x][y] == '+' or g[x][y] == '-')
								{
									if (y > 0 and !val[x][y - 1] and (g[x][y - 1] == '-' or g[x][y - 1] == '+'))
									{
										val[x][y - 1] = val[x][y];
										qe.push({x, y - 1});
									}
									if (y + 1 < m and !val[x][y + 1] and (g[x][y + 1] == '-' or g[x][y + 1] == '+'))
									{
										val[x][y + 1] = val[x][y];
										qe.push({x, y + 1});
									}
								}
								if (g[x][y] == '+' or g[x][y] == '|')
								{
									if (x > 0 and !val[x - 1][y] and (g[x - 1][y] == '|' or g[x - 1][y] == '+'))
									{
										val[x - 1][y] = val[x][y];
										qe.push({x - 1, y});
									}
									if (x + 1 < n and !val[x + 1][y] and (g[x + 1][y] == '|' or g[x + 1][y] == '+'))
									{
										val[x + 1][y] = val[x][y];
										qe.push({x + 1, y});
									}
								}
							}
							if (lsty > 0 and g[lstx][lsty - 1] == '|')
							{
								ma[val[lstx][lsty - 1]].push_back({lstx, val[lstx][lsty]});
							}
							if (lsty + 1 < m and g[lstx][lsty + 1] == '|')
							{
								ma[val[lstx][lsty + 1]].push_back({lstx, val[lstx][lsty]});
							}
							break;
						}
						q++;
					}
				}
			}
		}
	}
	dfs(1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...