C.
思路:
注意坐标有负的,不要拿数组存
每次拿完物品后,要清零
补充知识点:
pair
将2个数据组合成一组数据,pair的实现是一个结构体
map
提供一对一的数据处理,key-value键值对,其类型可以自己定义,第一个称为关键字,第二个为关键字的值。比如
map<string,int> tmap;第一个是键的类型,第二个是值的类型
1.通过下标访问
tmap [' c '] =10
2.通过迭代器访问
用it->first来访问键,使用it->second访问值
map<char,int>maps;
maps['d']=10;
maps['e']=20;
maps['a']=30;
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
map内部是自动排序的
auto
auto c : steps
就是将steps的值一个个赋给c 。for(auto c:steps)
等价于 for(int i=1;i<=n;i++) steps[ i ]=='U'
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, h, k;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef pair<int, int> PII;
map<PII, bool>v;
int main()
{
cin >> n >> m >> h >> k;
string steps;
cin >> steps;
for(int i = 1; i <= m; i ++ )
{
int x, y;
cin >> x >> y;
v[{x, y}] = 1;
}
int sx = 0, sy = 0, dir = 0;
for(auto c : steps)
{
if(c == 'U') dir = 0;
if(c == 'R') dir = 1;
if(c == 'D') dir = 2;
if(c == 'L') dir = 3;
sx += dx[dir];
sy += dy[dir];
h --;
if(h < 0)
{
puts("No");
return 0;
}
if(v[{sx, sy}] && h < k) h = k, v[{sx, sy}] = 0;
}
puts("Yes");
return 0;
}