908B - New Year and Buggy Bot - CodeForces Solution


brute force implementation *1200

Please click on ads to support us..

Python Code:

import itertools

n, m = map(int, input().rstrip().split())
mas = []
start = [0, 0]
end = [0, 0]
for i in range(n):
    s = input()
    for j in range(len(s)):
        if s[j] == 'S':
            start = [i, j]
        elif s[j] == 'E':
            end = [i, j]
    mas.append(s)
k = input()

cou = 0
for i1, i2, i3, i4 in itertools.permutations(([0, 1], [1, 0], [-1, 0],  [0, -1])):
    x, y = start
    for i in k:
        if i == '0':
            x += i1[0]
            y += i1[1]
        elif i == '1':
            x += i2[0]
            y += i2[1]
        elif i == '2':
            x += i3[0]
            y += i3[1]
        elif i == '3':
            x += i4[0]
            y += i4[1]
        if x >= n or x < 0 or y >= m or y < 0 or mas[x][y] == '#':
            break
        elif [x, y] == end:
            cou += 1
            break

print(cou)

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>

#define ll long long
#define ld long double
#define el '\n'
#define pi acos(-1)
#define IO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sumn(n) n*(n+1)/2
using namespace std;
template<typename T>
#pragma warning(suppress : 4996)
void print(T t, string s = " ") {
    for (auto e : t) {
        cout << e << s;
    }
    //cout << endl;
}
template<typename T>
void print(queue<T> q, string s = " ") {
    while (q.size()) {
        cout << q.front() << s;
        q.pop();
    }
    //cout << endl;
}
template<typename T>
void print(stack<T> st, string s = " ") {
    while (st.size()) {
        cout << st.top() << s;
        st.pop();
    }
    //cout << endl;
}
ll min();
ll max();
const int N = 1e7 + 1, mod = 1e9 + 7, inf = 1e9, pp = 31, K = 20;
int countBits(ll x) {
    int cntr = 0;
    while (x) {
        x >>= 1;
        cntr++;
    }
    return cntr;
}
ll sumRange(ll a, ll b, bool inclusive = true) {
    if (a == b)return 0;
    ll gg = sumn(b) - sumn(a) + a;
    if (!inclusive) {
        gg = gg - a - b;
    }
    return gg;
}
ll countSetBits(ll n)
{
    ll count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
int countdigits(ll x) {
    int cntr = 0;
    while (x) {
        x /= 10;
        cntr++;
    }
    return cntr;
}
bool isprime(int x) {
    if (x < 2)return 0;
    for (int i = 2; (ll)i * i <= x; i++) {
        if (x % i == 0)return 0;
    }
    return 1;
}
int primes[N];
void seive(int n) {
    for (int i = 0; i < n; i++)primes[i] = 1;
    primes[0] = primes[1] = 0;
    for (int i = 2; i < N; i++) {
        if (primes[i]) {
            for (int j = i + i; j < n; j += i)primes[j] = 0;
        }
    }
}
int getfirstmultiple(int x) {
    for (int i = 2; i < x; i++) {
        if (x % i == 0)return i;

    }
    return -1;
}
int getprimeFFactorization(int x) {
    for (int i = 2; 1ll * i * i < x; i++) {
        if (x % i == 0) {
            return i;
        }
    }
    return -1;
}
int lucky[] = { 4,7,44,47,74,77,444,447,474,477,744,747,774,777 };











bool rec(int p1, int p2, bool right, string a, string b) {
    if (p2 + 1 == b.size()) {
        return true;
    }
    bool f1 = 0, f2 = 0;

    if (right && p1 + 1 < a.size() && a[p1 + 1] == b[p2 + 1]) {
        f1 = rec(p1 + 1, p2 + 1, 1, a, b);
    }
    if (p1 > 0 && a[p1 - 1] == b[p2 + 1]) {
        f2 = rec(p1 - 1, p2 + 1, 0, a, b);
    }
    if (f1 == 1 || f2 == 1)return 1;
    else return 0;







}

void go() {
    int n, m;
    cin >> n >> m;
    char mm[50 + 2][50 + 2];
    int st1, st2;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> mm[i][j];
            if (mm[i][j] == 'S') {
                st1 = i;
                st2 = j;
            }
        }
    }
    string s;
    cin >> s;
    string maping = "0123";
    int res = 0;
    int temp1 = st1, temp2 = st2;

    do {
        bool flag = false;
        st1 = temp1;
        st2 = temp2;
        for (int i = 0; i < s.size(); i++) {
            int x = maping.find(s[i]);
            if (x == 0)st1--;
            else if (x == 1)st1++;
            else if (x == 2)st2--;
            else if (x == 3)st2++;

            if (st1<0 || st2<0 || st1>n - 1 || st2>m - 1 || mm[st1][st2] == '#') {
                break;
            }
            if (mm[st1][st2] == 'E') {
                flag = true;
                break;
            }

        }
        if (flag) {
           // cout << maping << el;
            res++;

        }


    } while (next_permutation(maping.begin(),maping.end()));

    cout << res;






}





void Beforego() {


}



int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif


    IO
        // online submission
        int cases = 1;
    //cin >> cases;
    Beforego();
    while (cases--) {
        go();
        if (cases)cout << el;
    }

    //cout<<el;

}

  	 	  	 	    			  	 			 				


Comments

Submit
0 Comments
More Questions

1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs