题解:P14736 [ICPC 2021 Seoul R] Find the House
思路
直接纯模拟即可。
步骤:
- 存储所有的三元组,方便快速查找。
- 从初始位置开始,循环
n 次移动。 - 每次根据当前位置找到对应的三元组并移动。
代码
#include<bits/stdc++.h> #include<unordered_map> using namespace std; long long n,q; int main(){ cin>>n; unordered_map<int,pair<char, int> >map;//中间加个空格,有些编译器编不出来,比如我的 //用哈希表存储三元组,键为位置 i,值为(方向,距离)对 for (int i=1;i<=n;i++){//读取所有三元组 int ii,k; char j; cin>>ii>>j>>k; map[ii]=make_pair(j,k); } cin>>q; for (int i=1;i<=n;i++){//模拟 n 次移动过程 auto it=map.find(q); if(it!=map.end()){ char c=it->second.first; int d=it->second.second; // 根据方向移动 if (c=='L') q=q-d; else q=q+d; } } cout<<q; }