Submission #1011810

#TimeUsernameProblemLanguageResultExecution timeMemory
1011810DedibeatPatkice (COCI20_patkice)C++17
50 / 50
2 ms1384 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int,int>> adj[200][200];
     int x, y, ex, ey;
void dfs(pair<int,int> v, int &ans, bool &found){
   // cout << v.first << " " << v.second << endl;
    if(v.first == ex && v.second == ey) {
            found = true;
           return;
    }
    ans++;
    for(auto u : adj[v.first][v.second]){
            dfs(u, ans, found);
    }
  }
signed main(){
       int n, m;
       cin >> n >> m;
       string a[n];
       for(int i = 0; i<n; i++) cin >> a[i];


       for(int i = 0; i<n; i++){
           for(int j = 0; j<m; j++){
                  if(a[i][j] == '<') adj[i][j].push_back({i, j - 1});
                  if(a[i][j] == '>') adj[i][j].push_back({i, j + 1});
                  if(a[i][j] == '^') adj[i][j].push_back({i - 1, j});
                  if(a[i][j] == 'v') adj[i][j].push_back({i + 1, j});
                  if(a[i][j] == 'x') ex = i, ey = j;
                  if(a[i][j] == 'o') x = i, y = j;

           }
       }
       vector<pair<int, char>> vc;
       int ans = 0;
       if(x > 0) {
            bool flag = false;
                dfs({x - 1, y}, ans, flag);
                if(flag)
                 vc.push_back({ans, 'N'});
                  ans = 0;
                  flag = false;
        // cout << endl;
       }
       if(x < n-1) {
            bool flag = false;
                dfs({x + 1, y}, ans, flag);
                if(flag)
                 vc.push_back({ans, 'S'});
                  ans = 0;
                   flag = false;
             //      cout << endl;
       }
       if(y > 0) {
            bool flag = false;
                dfs({x, y - 1}, ans, flag);
                if(flag)
                 vc.push_back({ans, 'W'});
                  ans = 0;
                   flag = false;
                //   cout << endl;
       }
   //    cout << "gg\n";
       if(y < m-1) {
            bool flag = false;
                dfs({x, y + 1}, ans, flag);
                if(flag)
                 vc.push_back({ans, 'E'});
                  ans = 0;
                   flag = false;
             //      cout << endl;
       }
       if(vc.size() == 0) cout << ":(\n";
       else {
            sort(vc.begin(), vc.end());
           cout << ":)\n";
         //  cout << vc.size() << endl;
           cout << vc[0].second << endl;
       }






}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...