199D - Jumping on Walls - CodeForces Solution


dfs and similar shortest paths *1400

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
pared_izq = [*input()]
pared_der = [*input()]

cola = [0]*2*n
inicio = 0
final = 0
se_hunde = False

matrix = [[-1]*n,[-1]*n]

def meter(x):   
    global final 
    cola[final] = x
    final = final + 1 

def sacar():
    global inicio
    x = cola[inicio]
    inicio = inicio + 1
    return x

def buscar(pos, mov):
    global se_hunde
    if mov[0] == 0:
        if pared_izq[mov[1]] == "-":
            
            if matrix[mov[0]][mov[1]] == -1:
                matrix[mov[0]][mov[1]] = matrix[pos[0]][pos[1]] + 1
                if matrix[mov[0]][mov[1]] > mov[1]:
                    se_hunde = True
                else:
                    meter(mov)
    else:
        if pared_der[mov[1]] == "-":

            if matrix[mov[0]][mov[1]] == -1:
                matrix[mov[0]][mov[1]] = matrix[pos[0]][pos[1]] + 1
                if matrix[mov[0]][mov[1]] > mov[1]:
                    se_hunde = True
                else:
                    meter(mov)
    return se_hunde

estado_inicial = [0,0]
matrix[0][0] = 0    
meter(estado_inicial)

resultado = False

while inicio != final:
    pos = sacar()


        arriba = [pos[0], pos[1]+1]

    if arriba[1] < n:
        buscar(pos, arriba)
    else:
        resultado = True
        break
            
        abajo = [pos[0], pos[1]-1]

    if abajo[1] > 0:
        buscar(pos, abajo)
 
        if pos[0] == 0:
        salto = [1, pos[1]+k]
    else:
        salto = [0, pos[1]+k]

    if salto[1] < n:
        buscar(pos, salto)
    else:
        resultado = True
        break

if resultado == True:
    print("YES")
else:
    print("NO")

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
using namespace std;
const ll mod = 1e9 + 7;
// const ll minmod=-1e18;
const ll inf = 1e18;
 
ll min(ll a, ll b)
{
    if (a > b)
    {
        return b;
    }
    return a;
}
 
ll max(ll a, ll b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}
 
ll bexpmod(ll a, ll b,ll mod)
{
    if (b == 0)
    {
        return 1;
    }
    if (b % 2 == 0)
    {
        return bexpmod((((a % mod) * (a % mod)) % mod), b / 2,mod);
    }
    return ((a % mod) * (bexpmod((((a % mod) * (a % mod)) % mod), (b - 1) / 2,mod) % mod)) % mod;
}
 
ll gcd(ll a,ll b)
{
    if(b==0){
        return a;
    }
    return gcd(b,a%b);
}
 
ll mylcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
 
ll bexp(ll a, ll b)
{
    if (b == 0)
    {
        return 1;
    }
    if (b % 2 == 0)
    {
        return bexp((a * a), b / 2);
    }
    return (a  * (bexp((a * a), (b - 1) / 2)));
}
 
ll nCr(ll n, ll k) {
    ld res = 1;
    for (ll i = 1; i <= k; ++i)
        res = res * (n - k + i) / i;
    return (ll)(res + 0.01);
}
 
string decToBin(ll num)
{
	string str;
  	while(num){
  		if(num & 1){
    		str+='1';
    	}
  		else{
    		str+='0';
    	}
  		num>>=1;
	}
	reverse(str.begin(),str.end());
	return str;
} 
 
ll binToDec(string s){
	ll num=0;
	ll pw=1;
	ll i=s.size()-1;
	while(i>=0){
		num+=((s[i]-'0')*pw);
		i--;
		pw*=2;
	}
	return num;
}
 
ll gcdEd(ll a,ll b,ll *x,ll *y){
	if(a==0){
		*x=0;
		*y=1;
		return b;
	}
	ll x1,y1;
	ll gcd1 = gcdEd(b%a,a,&x1,&y1);
	
	*x=y1-(b/a)*x1;
	*y=x1;
	
	return gcd1;
}

string u,d;

bool isSafe(ll i,ll j,ll time){
	if(i<time){
		return false;
	}
	if(j==0){
		if(u[i]=='X'){
			return false;
		}
	}
	else{
		if(d[i]=='X'){
			return false;
		}
	}
	return true;
}

void solve()
{	
	ll n,k;
	cin>>n>>k;
	cin>>u>>d;
	queue<pair<ll,ll> >q;
	q.push({0,0});
	vector<vector<ll> >time(n,vector<ll>(2,-1));
	time[0][0]=0;
	ll pos=-1;
	while(!q.empty()){
		if(pos==1){
			break;
		}
		pair<ll,ll>cur=q.front();
		q.pop();
		ll x=cur.ff;
		ll y=cur.ss;
		ll cy;
		// cout<<x<<" "<<y<<endl;
		if(y==0){
			cy=1;
		}
		else{
			cy=-1;
		}
		ll dx[3]={-1,1,k};
		ll dy[3]={0,0,cy};
		for(ll i=0;i<3;i++){
			ll nx=x+dx[i];
			ll ny=y+dy[i];
			// cout<<nx<<" "<<ny<<" "<<i<<endl;
			if(nx>=n){
				pos=1;
				break;
			}
			if(isSafe(nx,ny,time[x][y]+1)){
				// cout<<1<<endl;
				// cout<<time[nx][ny]<<endl;
				if(time[nx][ny]==-1){
					// cout<<2<<endl;
					q.push({nx,ny});
					time[nx][ny]=1+time[x][y];
				}
				else if(time[nx][ny]>1+time[x][y]){
					// cout<<3<<endl;
					q.push({nx,ny});
					time[nx][ny]=1+time[x][y];
				}
				// if(time[nx][nx]==-1){
// 					
				// }
				// else if(time[nx][ny]>1+time[x][y]){
// 					
				// }
			}
		}
	}
	if(pos==1){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
}
 
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    ll t = 1;
    for(ll i=1;i<=t;i++){
        // cout << "Case #"<<i<<": ";
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence