0%

Codeforces Round 516 D(Div. 2, by Moscow Team Olympiad)

题目链接

学习下deque用法

正解非常的巧妙:
可以这样想:如果我们保证了到达一个点时向左走的次数最少,那么是不是也可以保证向右走的次数最少呢?
答案是肯定的,因为向右走了一次之后肯定需要向左走一次来抵消掉这次操作
向右同理
把向左/右的边权看成1,向上/下的边权看成0

参考 博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//2021-05-21 22:00:01
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, r, c, X, Y, vis[N][N], ans;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
char s[N][N];
struct node{
int x, y, l, r;
};

void bfs(){
deque<node> Q;
Q.push_back((node){r, c, X, Y});
while(!Q.empty()){
node now = Q.front();
Q.pop_front();
if(vis[now.x][now.y] || (now.l<0) || (now.r<0)){
continue;
}
vis[now.x][now.y] = 1; ans++;
for(int i = 0; i < 4; i++){
int xx = now.x + dx[i];
int yy = now.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > m || s[xx][yy] == '*' || vis[xx][yy]) continue;
if(i == 0 || i == 1) {
Q.push_front((node){xx, yy, now.l, now.r});
continue;
}
if(i == 2){
Q.push_back((node){xx, yy, now.l-1, now.r} );
continue;
}
if(i == 3) {
Q.push_back((node){xx, yy, now.l, now.r-1} );
continue;
}
}
}
printf("%d\n", ans);
}

int main(){
cin >> n >> m >> r >> c >> X >> Y;
for(int i = 1; i <= n; i++){
scanf("%s", s[i]+1);
}
bfs();

return 0;
}


求大佬赏个饭