Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("O3")
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define ln '\n'
- #define int long long
- #define double long double
- typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_set;
- typedef tree<int , null_type , less_equal<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_multiset;
- #define _FastIO ios_base::sync_with_stdio(0); cin.tie(0)
- #define pii pair<int , int>
- #define pb push_back
- #define vi vector<int>
- #define mp make_pair
- #define lb lower_bound
- #define ub upper_bound
- #define sz(x) (int)((x).size())
- #define all(x) (x).begin() , (x).end()
- #define rall(x) (x).rbegin() , (x).rend()
- #define F first
- #define S second
- const int mod = 1e9 + 7;
- const int N = 1e3 + 5;
- const int INF = (int)2e18 + 123;
- int dx[4] = {1 , -1 , 0 , 0};
- int dy[4] = {0 , 0 , 1 , -1};
- int n , m;
- char a[N][N];
- int d[N][N] , p[N][N];
- void bfs(int nodei , int nodej){
- queue<pii> q;
- q.push({nodei , nodej});
- d[nodei][nodej] = 0;
- while(!q.empty()){
- int fromi = q.front().F , fromj = q.front().S;
- q.pop();
- cout << fromi << " " << fromj << '\n';
- for(int i = 0; i < 4; i++){
- int x = fromi + dx[i];
- int y = fromj + dy[i];
- if(x < 1 || y < 1 || x > n || y > m) continue;
- if((d[x][y] > d[fromi][fromj] + 1) || a[x][y] == '#') continue;
- cout << "ok" << '\n';
- d[x][y] = d[fromi][fromj] + 1;
- p[x][y] = i;
- q.push({x , y});
- }
- }
- }
- void solve(){
- cin >> n >> m;
- int xa , xb , ya , yb;
- for(int i = 1; i <= n; i++){
- for(int j = 1; j <= m; j++){
- cin >> a[i][j];
- d[i][j] = INF;
- if(a[i][j] == 'A'){
- xa = i;
- ya = j;
- }
- if(a[i][j] == 'B'){
- xb = i;
- yb = j;
- }
- }
- }
- bfs(xa , ya);
- if(d[xb][yb] == INF){
- cout << "NO";
- return ;
- }
- cout << "YES\n" << d[xb][yb] << '\n';
- }
- signed main()
- {
- _FastIO;
- int T = 1;
- //cin >> T;
- while(T--)
- solve();
- //system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement