Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/stack:200000000"]
- #pragma GCC optimize("Ofast"]
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native"]
- #include<iostream>
- #include<cmath>
- #include<algorithm>
- #include<vector>
- #include<map>
- #include<unordered_set>
- #include<unordered_map>
- #include<map>
- #include<algorithm>
- #include<queue>
- #include<stack>
- #include<cstdio>
- #include<random>
- #include<cassert>
- #include<sstream>
- #include<set>
- using namespace std;
- int n, m;
- int price[6];
- int inf[6] = {1, 1, 500, 500, 500, 1};
- vector<vector<int>> ways[6] = {
- {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}, //Пешка
- {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}}, //Конь
- {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}, //Ладья
- {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}, //Слон
- {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}, //Ферзь
- {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}}; //Король
- bool mp[305][305];
- int d[6][305][305], ans[305][305];
- bool in(int i, int j){
- return i >= 0 && i < n && j >= 0 && j < m;
- }
- struct node{
- int x, y, mod, dist;
- node(int x = 0, int y = 0, int mod = 0, int dist = 0){
- this->x = x;
- this->y = y;
- this->mod = mod;
- this->dist = dist;
- }
- };
- struct cmp{
- bool operator ()(const node & a, const node & b) const {
- return a.dist < b.dist || (a.dist == b.dist && a.x < b.x) || (a.dist == b.dist && a.x == b.x && a.y < b.y) || (a.dist == b.dist && a.x == b.x && a.y == b.y && a.mod < b.mod);
- }
- };
- set<node, cmp> se;
- void getWays(node & a){
- int x = a.x, y = a.y, mod = a.mod, dist = a.dist;
- for(auto w : ways[mod]){
- for(int j = 1; j <= inf[mod]; j++){
- int nx = x + w[0] * j;
- int ny = y + w[1] * j;
- if(!in(nx, ny) || !mp[nx][ny])break;
- if(mp[nx][ny] && d[mod][nx][ny] > dist + 1){
- se.erase(node(nx, ny, mod, d[mod][nx][ny]));
- d[mod][nx][ny] = dist + 1;
- se.insert(node(nx, ny, mod, d[mod][nx][ny]));
- }
- }
- }
- for(int i = 0; i < 6; i++){
- if(i == mod)continue;
- if(d[i][x][y] > dist + price[i]){
- se.erase(node(x, y, i, d[i][x][y]));
- d[i][x][y] = dist + price[i];
- se.insert(node(x, y, i, d[i][x][y]));
- }
- }
- }
- void print(vector<vector<int>> & a){
- for(auto i : a){
- for(auto j : i)cout << j << " ";
- cout << "\n";
- }
- }
- signed main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int x, y; cin >> n >> m >> x >> y; x--; y--;
- for(int i = 0; i < 6; i++)cin >> price[i];
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- char c; cin >> c;
- if(c == '#')mp[i][j] = 0;
- else mp[i][j] = 1;
- }
- }
- for(int i = 0; i < 6; i++){
- for(int j = 0; j < 305; j++){
- for(int k = 0; k < 305; k++)d[i][j][k] = 1e9;
- }
- }
- d[0][x][y] = 0;
- se.insert(node(x, y, 0, 0));
- while(!se.empty()){
- auto v = (*se.begin()); se.erase(se.begin());
- getWays(v);
- }
- int res = 0, resX = x, resY = y;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- ans[i][j] = d[0][i][j];
- for(int k = 1; k < 6; k++){
- ans[i][j] = min(ans[i][j], d[k][i][j]);
- }
- if(mp[i][j] && res < ans[i][j]){
- res = ans[i][j];
- resX = i;
- resY = j;
- }
- }
- }
- cout << resX + 1 << " " << resY + 1 << " ";
- if(res < 1e9)cout << res << "\n";
- else cout << "INF\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement