forked from Autoratch/Thailand-Olympiad-in-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toi10_map.cpp
64 lines (51 loc) · 1.85 KB
/
toi10_map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int m,n,in;
cin >> m >> n;
in = m*n;
vector<int> u(in,-1),d(in,-1),l(in,-1),r(in,-1);
vector<vector<int> > ans(m);
for(int i = 0;i < m;i++) ans[i].resize(n);
for(int i = 0;i < in-1;i++)
{
int a,b;
char c;
cin >> a >> c >> b;
if(c=='L'){ r[a] = b; l[b] = a; }
else{ d[a] = b; u[b] = a; }
}
int ul = 0,mx = 0;
queue<pair<int,pair<int,int> > > q;
vector<bool> visited(in,false);
q.push({0,{0,0}});
visited[0] = true;
while(!q.empty())
{
int p = q.front().first,x = q.front().second.first,y = q.front().second.second;
q.pop();
if(x+y>mx){ ul = p; mx = x+y; }
if(l[p]!=-1) if(!visited[l[p]]){ q.push({l[p],{x,y+1}}); visited[l[p]] = true; }
if(r[p]!=-1) if(!visited[r[p]]){ q.push({r[p],{x,y-1}}); visited[r[p]] = true; }
if(u[p]!=-1) if(!visited[u[p]]){ q.push({u[p],{x+1,y}}); visited[u[p]] = true; }
if(d[p]!=-1) if(!visited[d[p]]){ q.push({d[p],{x-1,y}}); visited[d[p]] = true; }
}
q.push({ul,{0,0}});
visited.assign(in,false);
visited[ul] = true;
while(!q.empty())
{
int p = q.front().first,x = q.front().second.first,y = q.front().second.second;
q.pop();
ans[x][y] = p;
if(l[p]!=-1) if(!visited[l[p]]){ q.push({l[p],{x,y-1}}); visited[l[p]] = true; }
if(r[p]!=-1) if(!visited[r[p]]){ q.push({r[p],{x,y+1}}); visited[r[p]] = true; }
if(u[p]!=-1) if(!visited[u[p]]){ q.push({u[p],{x-1,y}}); visited[u[p]] = true; }
if(d[p]!=-1) if(!visited[d[p]]){ q.push({d[p],{x+1,y}}); visited[d[p]] = true; }
}
for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++) cout << ans[i][j] << ' '; cout << endl; }
}