#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 < 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;
}
//cout << "TU" << endl;
return 0;
}#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 >> n;
cin >> m;
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;
}
//cout << "TU" << endl;
return 0;
}
Compilation message
adventure.cpp:216:2: error: stray '#' in program
216 | }#include <iostream>
| ^
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++)
| ~~^~~~~~~~~~~~~
adventure.cpp: At global scope:
adventure.cpp:216:3: error: 'include' does not name a type
216 | }#include <iostream>
| ^~~~~~~
adventure.cpp:234:21: error: redefinition of 'const long long int maxn'
234 | const long long int maxn = 100000000000000037;
| ^~~~
adventure.cpp:19:21: note: 'const long long int maxn' previously defined here
19 | const long long int maxn = 100000000000000037;
| ^~~~
adventure.cpp:235:9: error: redefinition of 'int n'
235 | int n;
| ^
adventure.cpp:20:9: note: 'int n' previously declared here
20 | int n;
| ^
adventure.cpp:236:9: error: redefinition of 'int m'
236 | int m;
| ^
adventure.cpp:21:9: note: 'int m' previously declared here
21 | int m;
| ^
adventure.cpp:237:12: error: redefinition of 'std::string s'
237 | string s;
| ^
adventure.cpp:22:12: note: 'std::string s' previously declared here
22 | string s;
| ^
adventure.cpp:238:9: error: redefinition of 'int pole [537][537]'
238 | int pole[537][537];
| ^~~~
adventure.cpp:23:9: note: 'int pole [537][537]' previously declared here
23 | int pole[537][537];
| ^~~~
adventure.cpp:239:15: error: redefinition of 'long long int vzdial [250037]'
239 | long long vzdial[250037];
| ^~~~~~
adventure.cpp:24:15: note: 'long long int vzdial [250037]' previously declared here
24 | long long vzdial[250037];
| ^~~~~~
adventure.cpp:240:24: error: redefinition of 'std::vector<std::vector<int> > v'
240 | vector<vector<int>>v(250037);
| ^
adventure.cpp:25:24: note: 'std::vector<std::vector<int> > v' previously declared here
25 | vector<vector<int>>v(250037);
| ^
adventure.cpp:241:24: error: redefinition of 'std::vector<std::vector<int> > w'
241 | vector<vector<int>>w(250037);
| ^
adventure.cpp:26:24: note: 'std::vector<std::vector<int> > w' previously declared here
26 | vector<vector<int>>w(250037);
| ^
adventure.cpp:242:17: error: redefinition of 'std::vector<bool> navst'
242 | vector<bool>navst(250037);
| ^~~~~
adventure.cpp:27:17: note: 'std::vector<bool> navst' previously declared here
27 | vector<bool>navst(250037);
| ^~~~~
adventure.cpp:243:10: error: redefinition of 'void dijkstrovalgoritmus()'
243 | void dijkstrovalgoritmus()
| ^~~~~~~~~~~~~~~~~~~
adventure.cpp:28:10: note: 'void dijkstrovalgoritmus()' previously defined here
28 | void dijkstrovalgoritmus()
| ^~~~~~~~~~~~~~~~~~~
adventure.cpp: In function 'void dijkstrovalgoritmus()':
adventure.cpp:268:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
268 | for (int i = 0; i < v[u].size(); i++)
| ~~^~~~~~~~~~~~~
adventure.cpp: At global scope:
adventure.cpp:274:9: error: redefinition of 'int vzdialednost(int, int)'
274 | int vzdialednost(int x, int y)
| ^~~~~~~~~~~~
adventure.cpp:59:9: note: 'int vzdialednost(int, int)' previously defined here
59 | int vzdialednost(int x, int y)
| ^~~~~~~~~~~~
adventure.cpp:279:5: error: redefinition of 'int main()'
279 | int main()
| ^~~~
adventure.cpp:64:5: note: 'int main()' previously defined here
64 | int main()
| ^~~~