CodeForces 908B New Year and Buggy Bot

薛定谔的AC發表於2024-08-01

題目連結:CodeForces 908B【New Year and Buggy Bot】



思路

簡單模擬,用pair陣列存下四個方向然後,依次列舉全排列,將每個方向依次對映給0,1,2,3,然後就是跟著String走,遇到障礙或者走出地圖就返回false,表示當前方案是錯誤的,走完String的所有指示時還沒有找到出口也返回false。


程式碼

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 60;

int n, m, len, res;
vector<pair<int, int>> ve;
pair<int, int> start, target;
char mp[N][N];
string s;

bool check(int x, int y) {
  if (x >= 1 && y >= 1 && x <= n && y <= m && mp[x][y] != '#')
    return true;
  else
    return false;
}

bool search() {
  pair<int, int> now = start;
  for (int i = 1; i <= len; i++) {
    now.first += ve[s[i] - '0'].first, now.second += ve[s[i] - '0'].second;
    if (check(now.first, now.second)) {
      if (now == target) {
        return true;
      }
    } else {
      return false;
    }
  }
  return false;
}

void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (mp[i][j] == 'S') {
        start.first = i, start.second = j;
      } else if (mp[i][j] == 'E') {
        target.first = i, target.second = j;
      }
    }
  }
  cin >> s;
  len = s.length();
  s = " " + s;
  
  ve.push_back({0, 1});
  ve.push_back({1, 0});
  ve.push_back({0, -1});
  ve.push_back({-1, 0});
  sort(ve.begin(), ve.end());
  
  do {
    if (search()) {
      res++;
    }
  } while (next_permutation(ve.begin(), ve.end()));

  cout << res << endl;
  return;
}

int main() {
  int t = 1;
  while (t--) {
    solve();
  }

  return 0;
}

相關文章