#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define LE(vec) vec[vec.size()-1]
#define TSTS int t; cin >> t; while(t--)solve()
const int INF = 1e9;
const long long LINF = 1e18;
const long double PI = asin(1)*2;
const int MOD = 1e9+7;
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
typedef long double ld;
void setIO()
{
FAST_IO;
}
void setIO (string s) {
setIO();
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
const int N=510;
int n, m, g[N][N], ng[N][N], d[N][N];
bool v[N][N];
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> q;
/*
-1-X
0-N
1-E
2-S
3-W
*/
int c(char x)
{
if(x=='X') return -1;
if(x=='N') return 0;
if(x=='E') return 1;
if(x=='S') return 2;
return 3;
}
void re(int p, int &x, int &y)
{
if(p==0) y--;
if(p==1) x++;
if(p==2) y++;
if(p==3) x--;
}
pii res(int p, int x, int y)
{
if(p==0) return MP(x, y-1);
if(p==1) return MP(x+1, y);
if(p==2) return MP(x, y+1);
return MP(x-1, y);
}
int main()
{
setIO();
cin >> m >> n;
if(m==3 && n==3){
FOR(i, 0, m)
{
FOR(j, 0, n)
{
char x;
cin >> x;
g[j][i]=c(x);
}
}
int ans=INF;
FOR(a, 0, 4) FOR(b, 0, 4) FOR(c, 0, 4) FOR(d, 0, 4) FOR(e, 0, 4) FOR(f, 0, 4) FOR(gi, 0, 4) FOR(h, 0, 4)
{
FOR(i, 0, m)
{
FOR(j, 0, n)
{
ng[i][j]=0;
}
}
int tot=0;
if(g[0][0]!=-1){
ng[0][0]=(g[0][0]+a)%4; tot+=a;}
if(g[1][0]!=-1){
ng[1][0]=(g[1][0]+b)%4; tot+=b;}
if(g[2][0]!=-1){
ng[2][0]=(g[2][0]+c)%4; tot+=c;}
if(g[0][1]!=-1){
ng[0][1]=(g[0][1]+d)%4; tot+=d;}
if(g[1][1]!=-1){
ng[1][1]=(g[1][1]+e)%4; tot+=e;}
if(g[2][1]!=-1){
ng[2][1]=(g[2][1]+f)%4; tot+=f;}
if(g[0][2]!=-1){
ng[0][2]=(g[0][2]+gi)%4; tot+=gi;}
if(g[1][2]!=-1){
ng[1][2]=(g[1][2]+h)%4; tot+=h;}
FOR(i, 0, n) FOR(j, 0, m) v[i][j]=false;
int x=0, y=0;
while(x>=0 && y>=0 && x<n && y<m){
if(v[x][y]) break;
if(x==2 && y==2) break;
v[x][y]=true;
if(g[x][y]==-1) break;
re(ng[x][y], x, y);
}
if(x==2 && y==2){
ans=min(ans, tot);
}
}
cout << (ans==INF?-1:ans);
}
else{
FOR(i, 0, m)
{
FOR(j, 0, n)
{
char x;
cin >> x;
g[j][i]=c(x);
}
}
FOR(i, 0, n) FOR(j, 0, m) d[i][j]=INF;
v[n-1][m-1]=true;
d[0][0]=0;
q.push(MP(0, MP(0, 0)));
while(!q.empty())
{
int k=q.top().F, x=q.top().S.F, y=q.top().S.S;
if(!v[x][y])
{
v[x][y]=true;
FOR(i, 0, 4)
{
int nx=res((g[x][y]+i)%4, x, y).F, ny=res((g[x][y]+i)%4, x, y).S;
if(nx<0 || nx>=n || ny<0 || ny>=m || (g[nx][ny]==-1 && nx!=n-1 && ny!=m-1)) continue;
if(k+i<d[nx][ny]){
d[nx][ny]=k+i;
q.push(MP(k+i, MP(nx, ny)));
}
}
}
q.pop();
}
cout << d[n-1][m-1];
}
}
Compilation message
adventure.cpp: In function 'void setIO(std::string)':
adventure.cpp:31:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |