Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main(){
- ll n,m,i,j,com;
- while(cin>>n>>m>>com){
- if(n+m+com==0) break;
- char mat[n][m];
- int vis[n][m];
- for(i=0;i<n;i++){
- for(j=0;j<m;j++){
- scanf(" %c",&mat[i][j]);
- vis[i][j]=0;
- }
- }
- com--;
- ll pas=0;
- pair <int,int> pos;
- pos.first=0;
- pos.second=com;
- ll ans,loop,tam_loop;
- while(true){
- pas++;
- //cout<<pos.first<<" "<<pos.second<<endl;
- if(mat[pos.first][pos.second]=='N'){
- pos.first--;
- }else if(mat[pos.first][pos.second]=='S'){
- pos.first++;
- }else if(mat[pos.first][pos.second]=='E'){
- pos.second++;
- }else if(mat[pos.first][pos.second]=='W'){
- pos.second--;
- }
- if(pos.first<0 || pos.first==n || pos.second<0 || pos.second == m){
- ans=1;
- break;
- }else if(vis[pos.first][pos.second]>0){
- //cout<<vis[pos.first][pos.second]<<endl;
- ans=0;
- loop = vis[pos.first][pos.second];
- tam_loop = pas-loop;
- break;
- }else{
- vis[pos.first][pos.second]=pas;
- }
- }
- if(ans){
- cout<<pas<<" step(s) to exit"<<endl;
- }else{
- cout<<loop<<" step(s) before a loop of "<<tam_loop<<" step(s)"<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement