#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <tuple>
#include <queue>
#include <functional>
using namespace std;
/*long long int power(long long int n, int k)
{
long long int vys = 1;
for (int i = 0; i < k; i++)
{
vys = (vys * (n - i)) / (i + 1);
}
return vys;
}*/
const long long int maxn = 100000000000000037;
int n;
int m;
string s;
int pole[537][537];
long long vzdial[250037];
vector<vector<int>>v(250037);
vector<vector<int>>w(250037);
vector<bool>navst(250037);
void dijkstrovalgoritmus()
{
for (int i = 0; i < n * m; i++)
{
vzdial[i] = -maxn;
}
vzdial[0] = 0;
for (int i = 0; i < n * m; i++)
{
navst[i] = false;
}
priority_queue<pair<int, int>> q;
q.push({ 0, 0 });
while (!q.empty())
{
int t = -q.top().first;
int u = q.top().second; q.pop();
if (navst[u])
{
continue;
}
vzdial[u] = t;
vzdial[u] = true;
for (int i = 0; i < v[u].size(); i++)
{
q.push({ -t - w[u][i], v[u][i] });
}
}
}
int vzdialednost(int x, int y)
{
int vys = y + 4 - x;
return vys%4;
}
int main()
{
/*for (int i = 0; i < 10; i++)
{
std::cout << "Hello Flash!\n";
}*/
cin >> m;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s;
for (int j = 0; j < m; j++)
{
if (s[j] == 'N')
{
pole[i][j] = 1;
}
if (s[j] == 'E')
{
pole[i][j] = 2;
}
if (s[j] == 'S')
{
pole[i][j] = 3;
}
if (s[j] == 'W')
{
pole[i][j] = 4;
}
if (s[j] == 'X')
{
pole[i][j] = -1;
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (pole[i][j] == -1)
{
}
else
{
int poz = i * m + j;
if (i == 0 && j == 0)
{
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
//////
else if (i == n - 1 && j == m - 1)
{
v[poz].push_back(i * m + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
}
else if (i == 0 && j == m - 1)
{
v[poz].push_back(i * n + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
else if (j == 0 && i == n - 1)
{
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
}
else if (i == 0)
{
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back(i * m + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
else if (j == 0)
{
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
else if (i == n - 1)
{
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back(i * m + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
}
else if (j == m - 1)
{
v[poz].push_back(i * m + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
else
{
v[poz].push_back(i * m + j - 1);
w[poz].push_back(vzdialednost(pole[i][j], 4));
v[poz].push_back(i * m + j + 1);
w[poz].push_back(vzdialednost(pole[i][j], 2));
v[poz].push_back((i - 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 1));
v[poz].push_back((i + 1) * m + j);
w[poz].push_back(vzdialednost(pole[i][j], 3));
}
}
}
}
dijkstrovalgoritmus();
if (vzdial[n * m - 1] == -maxn)
{
cout << -1 << endl;
}
else
{
cout << vzdial[n * m - 1] << endl;
}
return 0;
}
Compilation message
adventure.cpp: In function 'void dijkstrovalgoritmus()':
adventure.cpp:53:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 0; i < v[u].size(); i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
915 ms |
102404 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
915 ms |
102404 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1913 ms |
102404 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1108 ms |
102404 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
915 ms |
102404 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |