alexjercan/bugnet huggingface dataset:
Problem p00604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int n;

  while (cin >> n) {
    int a = 0;
    long long int ans = 0;
    int time[100];
    for (int i = 0; i < n; i++) {
      cin >> time[i];
    }

    sort(time, time + n);
    int temp = n;
    for (int j = 0; j < n; j++) {
      a = 0;
      for (int ii = 0; ii < temp; ii++) {
        a += time[ii];
      }
      ans += a;
      temp--;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int n;

  while (cin >> n, n) {
    int a = 0;
    long long int ans = 0;
    int time[10000];
    for (int i = 0; i < n; i++) {
      cin >> time[i];
    }

    sort(time, time + n);
    int temp = n;
    for (int j = 0; j < n - 1; j++) {
      a = 0;
      for (int ii = 0; ii < temp - 1; ii++) {
        a += time[ii];
      }
      ans += a;
      temp--;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00604 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> time;

  while (cin >> n) {
    time.clear();
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      time.push_back(t);
    }
    sort(time.begin(), time.end());
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += time[i] * (n - i);
    }
    cout << sum << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> time(n);

  while (cin >> n) {
    time.clear();
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      time.push_back(t);
    }
    sort(time.begin(), time.end());
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += time[i] * (n - i);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (int i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

const int MAX_V = 10000;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vi s(m, 0);
    rep(i, m) cin >> s[i];
    int t;
    rep(i, n) {
      rep(j, m) {
        cin >> t;
        s[j] -= t;
      }
    }
    sort(all(s));
    cout << (s[0] < 0 ? "No" : "Yes") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (int i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

const int MAX_V = 10000;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vi s(n, 0);
    rep(i, m) cin >> s[i];
    int t;
    rep(i, n) {
      rep(j, m) {
        cin >> t;
        s[j] -= t;
      }
    }
    sort(all(s));
    cout << (s[0] < 0 ? "No" : "Yes") << endl;
  }
}
Problem p00605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a + b) {
    int S[b];
    for (int i = 0; i < b; i++)
      cin >> S[i];
    int flag = 1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        int B;
        cin >> B;
        S[j] -= B;
        if (S[j] < 0) {
          flag = 0;
        }
      }
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a + b != 0) {
    int S[b];
    for (int i = 0; i < b; i++)
      cin >> S[i];
    int flag = 1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        int B;
        cin >> B;
        S[j] -= B;
        if (S[j] < 0) {
          flag = 0;
          break;
        }
      }
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N, K;
  while (cin >> N >> K, N || K) {
    int s[K];
    for (int i = 0; i < K; i++) {
      cin >> s[i];
    }
    int temp;
    bool flag = true;
    for (int j = 0; j < N; j++) {
      for (int i = 0; i < K; i++) {
        cin >> temp;
        s[i] -= temp;
        if (s[i] < 0)
          flag = false;
      }
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, K;
  while (cin >> N >> K, N || K) {
    int s[N];
    for (int i = 0; i < K; i++) {
      cin >> s[i];
    }
    int temp;
    bool flag = true;
    for (int j = 0; j < N; j++) {
      for (int i = 0; i < K; i++) {
        cin >> temp;
        s[i] -= temp;
        if (s[i] < 0)
          flag = false;
      }
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int N, K, tmp;

void solve() {
  vector<int> bloods(K), family(K);
  for (int i = 0; i < K; i += 1)
    cin >> bloods[i];
  for (int i = 0; i < N; i += 1) {
    for (int j = 0; j < K; j += 1) {
      cin >> tmp;
      family[j] += tmp;
    }
  }
  // calc
  for (int i = 0; i < K; i += 1) {
    if (family[i] > bloods[i]) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
}

int main(int argc, char const *argv[]) {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N, K, tmp;

void solve() {
  map<int, int> bloods;
  for (int i = 0; i < K; i += 1) {
    cin >> tmp;
    bloods.insert(make_pair(i, tmp));
  }
  vector<vector<int>> family(N, vector<int>(N, 0));
  for (int i = 0; i < N; i += 1) {
    for (int j = 0; j < K; j += 1) {
      cin >> family[i][j];
    }
  }
  // calc
  for (int i = 0; i < N; i += 1) {
    for (int j = 0; j < K; j += 1) {
      if (bloods[j] >= family[i][j]) {
        bloods[j] -= family[i][j];
      } else {
        cout << "NO" << endl;
        return;
      }
    }
  }
  cout << "YES" << endl;
}

int main(int argc, char const *argv[]) {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

int main() {
  while (true) {
    int N, K;
    string ans = "Yes";
    cin >> N >> K;
    int S[K];
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < K; ++i) {
      cin >> S[i];
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < K; ++j) {
        int B;
        cin >> B;
        S[j] -= B;
      }
    }
    for (int i = 0; i < K; ++i) {
      if (S[i] < 0) {
        ans = "No";
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  while (true) {
    int N, K;
    string ans = "Yes";
    cin >> N >> K;
    int S[N];
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < K; ++i) {
      cin >> S[i];
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < K; ++j) {
        int B;
        cin >> B;
        S[j] -= B;
      }
    }
    for (int i = 0; i < K; ++i) {
      if (S[i] < 0) {
        ans = "No";
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, k, t;
  while (cin >> n >> k && n || k) {
    bool flag = true;
    vector<int> blood(k);
    for (int i = 0; i < k; i++) {
      cin >> blood[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        cin >> t;
        blood[j] -= t;
        if (blood[j] < 0)
          flag = false;
      }
    }
    if (flag)
      cout << "Yes";
    else
      cout << "No";
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, k, t;
  bool flag;
  while (cin >> n >> k) {
    vector<int> bl(k);
    for (int i = 0; i < k; i++)
      cin >> bl[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> t;
        bl[j] -= t;
        if (bl[j] < 0) {
          flag = true;
        }
      }
      if (flag)
        cout << "NO" << endl;
      else
        cout << "YES" << endl;
    }
  }
}
Problem p00605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int N, K;
  while (cin >> N >> K, N != 0 || K != 0) {
    int str[K];
    for (int i = 0; i < K; i++) {
      cin >> str[i];
    }
    bool f = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < K; j++) {
        int tmp;
        cin >> tmp;
        str[j] -= tmp;
        if (str[j] < 0)
          f = false;
      }
    }
    cout << ((f) ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, K;
  while (cin >> N >> K, N || K) {
    int str[K];
    for (int i = 0; i < K; i++) {
      cin >> str[i];
    }
    bool f = true;
    for (int i = 0; f && i < N; i++) {
      for (int j = 0; j < K; j++) {
        int tmp;
        cin >> tmp;
        if (str[j] - tmp < 0) {
          f = false;
          break;
        } else {
          str[j] -= tmp;
        }
      }
    }
    cout << ((f) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, k, b;
  int stock[100];
  bool judge;

  while (cin >> n >> k, (n || k)) {
    if (!(n || k))
      goto YAMADA;

    judge = 0;

    for (int i = 0; i < k; i++) {
      cin >> stock[i];
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < k; j++) {
        cin >> b;
        stock[j] -= b;

        if (stock[j] < 0)
          judge = 1;
      }

    if (judge == 1)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }

YAMADA:
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int n, k;
  int stock[100];
  bool judge = 0;

  while (cin >> n >> k, (n || k)) {
    memset(stock, 0, sizeof(stock));

    for (int i = 0; i < k; i++) {
      cin >> stock[i];
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < k; j++) {
        int b;
        cin >> b;
        stock[j] -= b;

        if (stock[j] < 0) {
          judge = 1;
          break;
        }
        if (judge)
          break;
      }

    if (judge)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }

  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0)
      break;
    vector<int> blood(k);
    for (int i = 0; i < k; i++)
      cin >> blood[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        blood[j] -= a;
      }
    }
    bool success = true;
    for (int i = 0; i < k; i++) {
      if (blood[i] < 0)
        success = false;
    }
    if (success) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    vector<int> blood(k);
    for (int i = 0; i < k; i++)
      cin >> blood[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        blood[j] -= a;
      }
    }
    bool success = true;
    for (int i = 0; i < k; i++) {
      if (blood[i] < 0)
        success = false;
    }
    if (success) {
      cout << "Yes" << endl;

    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define MAX_N 120

int S[MAX_N];
int sum[MAX_N];
int n, k, a;
int main() {
  while (true) {
    for (int i = 0; i < n; i++) {
      sum[i] = 0;
    }

    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < k; i++) {
      cin >> S[i];
    }
    bool ok = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        cin >> a;
        S[j] -= a;
        if (S[j] < 0) {
          ok = false;
        }
      }
    }
    if (ok == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX_N 120

int S[MAX_N];
int sum[MAX_N];
int n, k, a;
int main() {
  while (true) {
    for (int i = 0; i < n; i++) {
      sum[i] = 0;
    }

    cin >> n >> k;
    for (int i = 0; i < k; i++) {
      cin >> S[i];
    }
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a;
        sum[i] += a;
      }
    }
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (sum[i] > S[i]) {
        ok = false;
      }
    }
    if (ok == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p00605 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, m, x, s[100];
int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < m; i++)
      cin >> s[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> x, s[j] -= x;
    }
    int f = 1;
    for (int i = 0; i < m; i++) {
      if (s[i] < 0)
        f = 0;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n, m, x, s[100];
int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < m; i++)
      cin >> s[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> x, s[x]--;
    }
    int f = 1;
    for (int i = 0; i < m; i++) {
      if (s[i] < 0)
        f = 0;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n;
  while (cin >> n && n) {
    vector<vector<int>> cases(n + 1, vector<int>(9));

    char s, t, b;
    cin >> s >> t >> b;
    int start, battery, junk;
    start = (int)s - 'A';
    battery = (int)t - 'A';
    junk = (int)b - 'A';

    for (int i = 0; i <= 8; i++) {
      cases[0][i] = 0;
    }
    cases[0][battery] = 1;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= 8; j++) {
        cases[i][j] = 0;
        // up
        if (j >= 3 && j - 3 != junk) {
          cases[i][j] += cases[i - 1][j - 3];
        } else {
          cases[i][j] += cases[i - 1][j];
        }

        // down
        if (j <= 5 && j + 3 != junk) {
          cases[i][j] += cases[i - 1][j + 3];
        } else {
          cases[i][j] += cases[i - 1][j];
        }

        // right
        if (j % 3 != 2 && j + 1 != junk) {
          cases[i][j] += cases[i - 1][j + 1];
        } else {
          cases[i][j] += cases[i - 1][j];
        }

        // left
        if (j % 3 != 0 && j - 1 != junk) {
          cases[i][j] += cases[i - 1][j - 1];
        } else {
          cases[i][j] += cases[i - 1][j];
        }
      }
    }

    printf("%.8f", (double)cases[n][start] / pow(4.0, n));
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int result;
int n;
int battery, junk;

void solve(int times, int now) {

  if (n == times) {
    if (now == battery) {
      result += 1;
    }
    return;
  }

  // up
  if (now >= 4 && now - 3 != junk) {
    solve(times + 1, now - 3);
  } else {
    solve(times + 1, now);
  }

  // down
  if (now <= 6 && now + 3 != junk) {
    solve(times + 1, now + 3);
  } else {
    solve(times + 1, now);
  }

  // right
  if (now % 3 != 0 && now + 1 != junk) {
    solve(times + 1, now + 1);
  } else {
    solve(times + 1, now);
  }

  // left
  if (now % 3 != 1 && now - 1 != junk) {
    solve(times + 1, now - 1);
  } else {
    solve(times + 1, now);
  }
}

int main() {

  while (cin >> n && n) {
    result = 0;
    char s, t, b;
    cin >> s >> t >> b;
    int start;
    start = s - 'A' + 1;
    battery = t - 'A' + 1;
    junk = b - 'A' + 1;

    solve(0, start);
    printf("%.8f", (double)result / pow(4.0, n));
    cout << endl;
  }

  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;
typedef pair<int, int> P;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
P position(char bet) {
  if (bet == 'A')
    return P(0, 0);
  else if (bet == 'B')
    return P(0, 1);
  else if (bet == 'C')
    return P(0, 2);
  else if (bet == 'D')
    return P(1, 0);
  else if (bet == 'E')
    return P(1, 1);
  else if (bet == 'F')
    return P(1, 2);
  else if (bet == 'G')
    return P(2, 0);
  else if (bet == 'H')
    return P(2, 1);
  else if (bet == 'I')
    return P(2, 2);
}

int main() {
  int n;
  char sc, tc, bc;
  P s, t, b;

  while (cin >> n && n) {
    double dp[3][3][n + 1];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          dp[j][k][i] = 0;
        }
      }
    }
    cin >> sc >> tc >> bc;
    s = position(sc);
    t = position(tc);
    b = position(bc);
    dp[s.second][s.first][0] = 1;

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          if (dp[i][j][k] == 0 || (i == b.second && j == b.first))
            continue;

          for (int ii = 0; ii < 4; ii++) {
            int nx = j + dx[ii], ny = i + dy[ii];
            if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
                !(nx == b.first && ny == b.second)) {
              dp[ny][nx][k + 1] += dp[i][j][k] * 0.25;
            } else {
              dp[i][j][k + 1] += dp[i][j][k] * 0.25;
            }
          }
        }
      }
    }

    printf("%.8f\\n", dp[t.second][t.first][n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
typedef pair<int, int> P;
bool mep[3][3];
int probability;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n;
P sp, tp, bp;

P position(char bet) {
  if (bet == 'A')
    return P(0, 0);
  else if (bet == 'B')
    return P(0, 1);
  else if (bet == 'C')
    return P(0, 2);
  else if (bet == 'D')
    return P(1, 0);
  else if (bet == 'E')
    return P(1, 1);
  else if (bet == 'F')
    return P(1, 2);
  else if (bet == 'G')
    return P(2, 0);
  else if (bet == 'H')
    return P(2, 1);
  else if (bet == 'I')
    return P(2, 2);
}

void search(P p, int c) {
  if (c > n)
    return;
  else if (p.first == tp.first && p.second == tp.second) {
    probability++;
  }

  for (int i = 0; i < 4; i++) {
    int nx = p.first + dx[i], ny = p.second + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 && mep[ny][nx]) {
      search(P(nx, ny), c + 1);
    } else {
      search(p, c + 1);
    }
  }
}

int main() {
  char s, t, b;
  while (cin >> n && n) {
    probability = 0;
    for (int i = 0; i < 3; i++) {
      mep[i][0] = mep[i][1] = mep[i][2] = true;
    }
    cin >> s >> t >> b;
    sp = position(s), tp = position(t), bp = position(b);
    mep[bp.second][bp.first] = false;
    search(sp, 0);
    printf("%.8f\\n", (double)(probability / pow(4, n)));
  }
  return 0;
}