华为笔试8.31

// Q1
int main(int argc, char const* argv[]) {

  // string l =

  //     "\"An introduction, is. \" the first paragraph \" of your"

  //     "paper...... \"";

  string l = "a   a";

  // getline(cin, l);

  string mp_l = "what say first Second IS introduction IS end";

  // getline(cin, mp_l);



  mp_l += " ";

  unordered_map word_mp;

  int prev = 0;

  int cur = mp_l.find(' ', prev);

  int cnt = 0;

  while (cur != string::npos) {

    string word = mp_l.substr(prev, cur - prev);

    transform(word.begin(), word.end(), word.begin(), ::tolower);

    word_mp[word] = cnt;

    ++cnt;

    prev = cur + 1;

    cur = mp_l.find(' ', prev);

  }

int left, right;

  string res = "";

  for (left = 0; left < l.size();) {

    if (l[left] == '\"') {

      int nxt = l.find('\"', left + 1);

      res += l.substr(left, nxt - left);

      left = l.find_first_not_of(' ', nxt);

      continue;

    } else if (l[left] == ' ' || l[left] == ',' || l[left] == '.') {

      res += l[left++];

      continue;

    }



    string tmp = "";

    for (right = left; right < l.size(); right++) {

      if (l[right] == ' ' || l[right] == ',' || l[right] == '.') break;

      tmp += l[right];

    }



    string tmp_lower = tmp;

    transform(tmp_lower.begin(), tmp_lower.end(), tmp_lower.begin(),

    ::tolower); if (word_mp.find(tmp_lower) != word_mp.end()) {

      res += to_string(word_mp[tmp_lower]);

    } else

      res += tmp;

    res += l[right];

    left = right + 1;

  }



  cout << res << endl;

  return 0;

}
// Q2
int main() {

  int n, m;

  cin >> n >> m;



  int st_x, st_y, end_x, end_y;

  vector> vec(n, vector(m, 0));

  vector> visited(n, vector(m, false));

  for (int i = 0; i < n; i++) {

    for (int j = 0; j < m; j++) {

      cin >> vec[i][j];

      if (vec[i][j] == '2') {

        st_x = i;

        st_y = j;

      } else if (vec[i][j] == '3') {

        end_x = i;

        end_y = j;

      }

    }

  }

  for (int i = 0; i < n; i++) {

    for (int j = 0; j < m; j++) {

      if (vec[i][j] == '6') {

        vec[i + 1][j] = '0';

        vec[i][j + 1] = '0';

        if (i > 0) vec[i - 1][j] = '0';

        if (j > 0) vec[i][j - 1] = '0';

      }

    }

  }



  int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  auto cmp = [](vector& a, vector& b) -> bool { return a[0] > b[0];

  }; priority_queue, vector>, decltype(cmp)> pq(cmp);

  pq.push({0, st_x, st_y});

  visited[st_x][st_y] = true;

  while (!pq.empty()) {

    auto cur = pq.top();

    pq.pop();

    int cur_x = cur[1], cur_y = cur[2];

    for (auto dir : dirs) {

      int nxt_x = cur_x + dir[0];

      int nxt_y = cur_y + dir[1];

      if (nxt_x < 0 || nxt_y < 0 || nxt_x >= n || nxt_y >= m ||

          vec[nxt_x][nxt_y] == '1' || visited[nxt_x][nxt_y]) {

        continue;

      } else {



        int cost = vec[nxt_x][nxt_y]=='3'

      }

    }

  }



  return 0;

}

你可能感兴趣的:(华为,算法)