Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

E. Robot on the Board 1 — (Div. 3)

Revision en2, by ayush29azad, 2021-11-03 15:27:09

Problem Link :https://codeforces.com/contest/1607/problem/E

How is the below solution working ?? I found this in submission relatively easier than others . Can someone explain the intuition behind it? What I have understood is that we can move opposite to the direction s[i] in the string to get the required cooordinate. Can someone give a proper explanation for this and why are traversing string in the reverse direction?


#include <bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n,m; cin>>n>>m; string s; cin>>s; int x=1,y=1; for(int i=s.size()-1;i>=0;i--){ if(s[i]=='L'&&x<m)x++; if(s[i]=='R'&&x>1)x--; if(s[i]=='U'&&y<n)y++; if(s[i]=='D'&&y>1)y--; } cout<<y<<" "<<x<<endl; } }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ayush29azad 2021-11-03 15:27:09 65
en1 English ayush29azad 2021-11-03 15:25:28 845 Initial revision (published)