CodeForces 908B New Year and Buggy Bot

题目链接: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;
}
posted @ 2024-08-01 15:42  薛定谔的AC  阅读(2)  评论(0编辑  收藏  举报