540C - Ice Cave - CodeForces Solution


dfs and similar *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define stream(line) istringstream in((line))
#define endl "\n"
#define F first
#define S second
#define clr(a, d) memset((a),d,sizeof(a))
const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int M = 2e6 + 5;

void print_vector(vector<ll> &v) {
    for (ll i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
}
int n,m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool vis[N][N],f;
char arr[N][N];
int st,en,st2,en2,cnt;
void dfs(int i,int j){
    vis[i][j] = 1;
    if(arr[i][j] == 'X' && st2 == i && en2 == j){
        f = true;
        cnt++;
    }
    if(arr[i][j] == '.' && st2 == i && en2 == j){
        arr[i][j] = 'X';
        cnt++;
    }
    for (int k = 0; k < 4; ++k) {
        int x = i+dx[k], y = j+dy[k];
        if(x >=0 && y >= 0 && x <n && y < m && !vis[x][y] && arr[x][y] == '.'){
            dfs(x,y);
        }
        else if(x == st2 && y == en2){
            dfs(x,y);
        }
    }
}


int main() {
    fast
    int tc = 1;
    //cin >> tc;
    while (tc--) {
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cin >> arr[i][j];
            }
        }
        cin >> st >> en >> st2 >> en2;
        st--,en--,st2--,en2--;
        f = false;
        dfs(st,en);
        if(st == st2 && en == en2 && cnt < 2){
            f = false;
        }
        if(f){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    return 0;
}
			     		   		  	  	   	   			


Comments

Submit
0 Comments
More Questions

454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection