#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;
navst[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 < m; i++)
{
cin >> s;
for (int j = 0; j < n; 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;
}
//cout << "TU" << 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++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12108 KB |
Output is correct |
2 |
Correct |
10 ms |
12108 KB |
Output is correct |
3 |
Correct |
9 ms |
12108 KB |
Output is correct |
4 |
Correct |
11 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |