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

Правка en1, от ayush29azad, 2021-11-03 15:25:28

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; } }

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский ayush29azad 2021-11-03 15:27:09 65
en1 Английский ayush29azad 2021-11-03 15:25:28 845 Initial revision (published)