#include <bits/stdc++.h>
using namespace std;
const int nx=105;
int n, m, dp[nx][nx];
char mp[nx][nx];
vector<pair<int, int>> d[nx][nx];
pair<int, int> st, ed;
pair<int, char> res={1e9, 0};
void dfs(pair<int, int> u)
{
//cout<<"here "<<u.first<<' '<<u.second<<'\n';
for (auto v:d[u.first][u.second]) dp[v.first][v.second]=dp[u.first][u.second]+1, dfs(v);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cin>>mp[i][j];
dp[i][j]=1e9;
if (mp[i][j]=='o') st={i, j};
if (mp[i][j]=='x') ed={i, j};
if (mp[i][j]=='>') d[i][j+1].push_back({i, j});
if (mp[i][j]=='<') d[i][j-1].push_back({i, j});
if (mp[i][j]=='^') d[i-1][j].push_back({i, j});
if (mp[i][j]=='v') d[i+1][j].push_back({i, j});
}
}
dp[ed.first][ed.second]=1;
dfs(ed);
/*
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cout<<dp[i][j]<<' ';
}
cout<<'\n';
}
*/
if (st.first>1) res=min(res, {dp[st.first-1][st.second], 'N'});
if (st.first<n) res=min(res, {dp[st.first+1][st.second], 'S'});
if (st.second>1) res=min(res, {dp[st.first][st.second-1], 'W'});
if (st.second<m) res=min(res, {dp[st.first][st.second+1], 'E'});
if (res.first>=1e9) cout<<":(";
else cout<<":)\n"<<res.second;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |