#include<bits/stdc++.h> #define ll long long using namespace std; bool is_valid(int x,int y,int r,int c,vector<vector<char>>&v){ if(x<1 || x>r || y<1 || y>c || v[x][y]=='#') return false; return true; } void dfs(int &x,int &y,vector<vector<char>>&v,string &s,int r,int c,int &idx,int l){ if(idx<l && s[idx]=='U' && is_valid(x,y,r,c,v)){ dfs(x-1,y,v,s,r,c,idx+1); } if(idx<l && s[idx]=='L' && is_valid(x,y,r,c,v)){ dfs(x,y-1,v,s,r,c,idx+1); } if(idx<l && s[idx]=='R' && is_valid(x,y,r,c,v)){ dfs(x,y+1,v,s,r,c,idx+1); } if(idx<l && s[idx]=='D' && is_valid(x,y,r,c,v)){ dfs(x+1,y,v,s,r,c,idx+1); } return; } int main(){ int r,c,x,y; cin>>r>>c>>x>>y; vector<vector<char>>v(r,vector<char>(c)); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cin>>a[i][j]; } } string s; cin>>s; int l=s.length(); dfs(x,y,v,s,r,c,0,l); cout<<x<<" "<<y; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter