Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool used[40100];
- int l[40100],u[40100],d[40100],r[40100];
- using pipii=pair<int,pair<int,int>>;
- int board[210][210];
- int main(){
- int n,m;
- scanf("%d%d",&n,&m);
- for(int i=0;i<n*m;i++){
- l[i]=-1;
- u[i]=-1;
- r[i]=-1;
- d[i]=-1;
- }
- for(int i=0;i<(n*m)-1;i++){
- int x,z;
- char c;
- cin >> x >> c >> z;
- if(c=='U'){
- u[z]=x;
- d[x]=z;
- }
- else{
- l[z]=x;
- r[x]=z;
- }
- used[z]=true;
- }
- queue<int> s;
- for(int i=0;i<n*m;i++){
- if(!used[i]){
- s.push(i);
- }
- }
- while(!s.empty()){
- vector<bool> visited(n*m+10,false);
- int cnt=0;
- int st=s.front();
- s.pop();
- queue<pipii> q;
- q.push({st,{0,0}});
- while(!q.empty()){
- int a=q.front().first;
- int b=q.front().second.first;
- int c=q.front().second.second;
- q.pop();
- if(visited[a]) continue;
- visited[a]=true;
- board[b][c]=a;
- if(b!=0&&u[a]!=-1&&!visited[u[a]]) q.push({u[a],{b-1,c}});
- if(c!=0&&l[a]!=-1&&!visited[l[a]]) q.push({l[a],{b,c-1}});
- if(b!=n-1&&d[a]!=-1&&!visited[d[a]]) q.push({d[a],{b+1,c}});
- if(c!=m-1&&r[a]!=-1&&!visited[r[a]]) q.push({r[a],{b,c+1}});
- cnt++;
- }
- if(cnt==n*m){
- for(int i=0;i<n;i++){
- for(int j=0;j<m;j++){
- printf("%d ",board[i][j]);
- }
- cout << endl;
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment