#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 128,dx[] = {0,-1,1,0},dy[] = {1,0,0,-1};
const char *str = "ENSW";
struct edge{int v,d;};
vector<edge> G[maxn * maxn];
inline void addedge(int u,int v,int d){G[u].push_back(edge{v,d});}
int n,m,vis[maxn * maxn];
inline int get(int x,int y){return (x - 1) * m + y;}
struct node{
int u;
ll h;
bool operator < (const node &rhs)const{
return h > rhs.h;
}
};
ll dis[maxn * maxn];
inline void dijkstra(int s){
memset(dis,0x7f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s] = 0;
priority_queue<node> q;
q.push(node{s,0});
while(!q.empty()){
int u = q.top().u;q.pop();
if(vis[u])continue;
vis[u] = 1;
for(int i = 0;i < (int)G[u].size();i++){
const edge &e = G[u][i];
if(dis[u] + e.d < dis[e.v]){
dis[e.v] = dis[u] + e.d;
q.push(node{e.v,dis[e.v]});
}
}
}
}
char mp[maxn][maxn];
int main(){
scanf("%d %d",&n,&m);
for(int i = 1;i <= n;i++)scanf("%s",mp[i] + 1);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(mp[i][j] == '<')addedge(get(i,j),get(i,j - 1),1);
else if(mp[i][j] == '>')addedge(get(i,j),get(i,j + 1),1);
else if(mp[i][j] == '^')addedge(get(i,j),get(i - 1,j),1);
else if(mp[i][j] == 'v')addedge(get(i,j),get(i + 1,j),1);
int sx,sy,tx,ty;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(mp[i][j] == 'o')sx = i,sy = j;
else if(mp[i][j] == 'x')tx = i,ty = j;
ll tmp = 0x7fffffffffffffff;char ans = 'G';
for(int i = 0;i < 4;i++){
int nx = sx + dx[i],ny = sy + dy[i];
dijkstra(get(nx,ny));
if(dis[get(tx,ty)] < tmp){
tmp = dis[get(tx,ty)];
ans = str[i];
}
}
if(tmp != 0x7f7f7f7f7f7f7f7f)puts(":)"),putchar(ans);
else puts(":(");
return 0;
}
Compilation message
patkice.cpp: In function 'int main()':
patkice.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
43 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
patkice.cpp:44:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
44 | for(int i = 1;i <= n;i++)scanf("%s",mp[i] + 1);
| ~~~~~^~~~~~~~~~~~~~~~
patkice.cpp:13:50: warning: 'ty' may be used uninitialized in this function [-Wmaybe-uninitialized]
13 | inline int get(int x,int y){return (x - 1) * m + y;}
| ^
patkice.cpp:51:15: note: 'ty' was declared here
51 | int sx,sy,tx,ty;
| ^~
patkice.cpp:51:12: warning: 'tx' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | int sx,sy,tx,ty;
| ^~
patkice.cpp:58:23: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
58 | int nx = sx + dx[i],ny = sy + dy[i];
| ^~
patkice.cpp:58:7: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
58 | int nx = sx + dx[i],ny = sy + dy[i];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
876 KB |
Output is correct |
2 |
Correct |
1 ms |
876 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
876 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
908 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
876 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1004 KB |
Output is correct |
12 |
Correct |
1 ms |
1004 KB |
Output is correct |
13 |
Correct |
1 ms |
1004 KB |
Output is correct |
14 |
Correct |
1 ms |
876 KB |
Output is correct |
15 |
Correct |
1 ms |
1004 KB |
Output is correct |
16 |
Correct |
1 ms |
876 KB |
Output is correct |
17 |
Correct |
1 ms |
876 KB |
Output is correct |
18 |
Correct |
1 ms |
876 KB |
Output is correct |
19 |
Correct |
1 ms |
876 KB |
Output is correct |
20 |
Correct |
1 ms |
876 KB |
Output is correct |