1. 程式人生 > >【非原創】codeforces 1063B Labyrinth 【01bfs】

【非原創】codeforces 1063B Labyrinth 【01bfs】

學習部落格:戳這裡

附本人程式碼:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 2e3 + 10;
 5 const ll mod = 998244353;
 6 char st[maxn][maxn];
 7 int vis[maxn][maxn];
 8 int dx[11]={1,-1, 0, 0};
 9 int dy[11]={0, 0, 1,-1};
10 const int inf = 0x3f3f3f3f;
11 struct nod{ 12 int x, y; 13 }; 14 int main() { 15 16 int n, m; 17 int r, c; 18 int X, Y; 19 scanf("%d %d", &n, &m); 20 scanf("%d %d", &r, &c); 21 scanf("%d %d", &X, &Y); 22 for(int i = 1; i <= n; ++i) { 23 scanf("%s", st[i]+1); 24
} 25 memset(vis, inf, sizeof(vis)); 26 deque<nod>q; 27 nod u, v; 28 u.x = r, u.y = c; 29 q.push_front(u); 30 vis[u.x][u.y] = 0; 31 while(!q.empty()) { 32 u = q.front(); q.pop_front(); 33 for(int i = 0; i < 4; ++i) { 34 int xx = u.x + dx[i];
35 int yy = u.y + dy[i]; 36 if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && st[xx][yy] != '*') { 37 if(vis[u.x][u.y] + (i==2) < vis[xx][yy]) { 38 vis[xx][yy] = vis[u.x][u.y] + (i==2); 39 v.x = xx, v.y = yy; 40 if(i == 2) q.push_back(v); 41 else q.push_front(v); 42 } 43 } 44 } 45 } 46 int ans = 0; 47 for(int i = 1; i <= n; ++i) { 48 for(int j = 1; j <= m; ++j) { 49 if(vis[i][j] <= Y && vis[i][j] - j + c <= X) { 50 // printf("%d %d %d\n", vis[i][j], i, j); 51 ++ans; 52 } 53 } 54 } 55 printf("%d\n", ans); 56 return 0; 57 }
View Code