Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define nl "\n"
- #define fi first
- #define se second
- #define pi 3.14159
- #define ll long long
- #define odd(a) (a&1)
- #define even(a) !(a&1)
- #define Mod 1'000'000'007
- #define INF 2'000'000'000 // 2e9
- #define sz(x) int(x.size())
- #define charToInt(s) (s - '0')
- #define ull unsigned long long
- #define number_line iota(all(vec) , 1)
- #define all(s) s.begin(), s.end()
- #define rall(v) v.rbegin() , v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define Num_of_Digits(n) ((int)log10(n) + 1)
- #define to_decimal(bin) stoll(bin, nullptr, 2)
- #define Ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
- #define Floor(n, m) (((n) / (m)) - ((n) % (m) ? 0 : 1))
- #define Upper(s) transform(all(s), s.begin(), ::toupper);
- #define Lower(s) transform(all(s), s.begin(), ::tolower);
- #define cout_map(mp) for(auto& [f, s] : mp) cout << f << " " << s << "\n";
- // ----- bits-------
- #define pcnt(n) __builtin_popcount(n)
- #define pcntll(n) __builtin_popcountll(n)
- #define clz(n) __builtin_clz(n) // <---100
- #define clzll(n) __builtin_clzll(n)
- #define ctz(n) __builtin_ctz(n) // 0001---->
- #define ctzll(n) __builtin_ctzll(n)
- using namespace std;
- template < typename T = int > istream& operator >> (istream &in, vector < T > & v){
- for(auto & x : v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > & v){
- for(const T & x : v) out << x << " ";
- return out;
- }
- void esraa()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- struct node{
- int i , j , cnt;
- node(int i , int j , int cnt) : i(i) , j(j) , cnt(cnt) {}
- };
- int dx[] = {0 , 0 , 1 , -1};
- int dy[] = {1 , -1 , 0 , 0};
- void solve(){
- int n , m , d;
- cin >> n >> m >> d;
- vector < string > v(n);
- for(auto & i : v) cin >> i;
- auto valid = [&](int x , int y){
- return x >= 0 and x < n and y >= 0 and y < m and v[x][y] != 'M';
- };
- int sx , sy , fx , fy;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- if(v[i][j] == 'S') sx = i , sy = j;
- if(v[i][j] == 'F') fx = i , fy = j;
- }
- }
- auto bfs1 = [&](){
- queue < node > q;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- if(v[i][j] == 'M') q.push(node(i , j , d));
- }
- }
- while(!q.empty()){
- auto [i , j , cnt] = q.front();
- q.pop();
- if(cnt <= 0) continue;
- for(int k = 0; k < 4; k++){
- int x = i + dx[k];
- int y = j + dy[k];
- if(valid(x , y)){
- v[x][y] = 'M';
- q.push(node(x , y , cnt - 1));
- }
- }
- }
- };
- bfs1();
- auto bfs2 = [&](){
- vector < vector < bool > > vis(n , vector < bool > (m));
- queue < node > q;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- if(v[i][j] == 'S') q.push(node(i , j , 0));
- }
- }
- vis[sx][sy] = true;
- while(!q.empty()){
- auto [i , j , cnt] = q.front();
- q.pop();
- if(v[i][j] == 'F') return cnt;
- for(int k = 0; k < 4; k++){
- int x = i + dx[k];
- int y = j + dy[k];
- if(valid(x , y) and !vis[x][y]){
- vis[x][y] = true;
- q.push(node(x , y , cnt + 1));
- }
- }
- }
- return -1;
- };
- cout << bfs2() << nl;
- }
- int main()
- {
- esraa();
- int t = 1;
- // cin >> t;
- //cin.ignore();
- while(t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement