码迷,mamicode.com
首页 > 其他好文 > 详细

日常训练17-10-25

时间:2017-10-25 19:50:55      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:col   scan   bfs   its   std   target   bre   mes   closed   

CF877

链接

 

D. Olya and Energy Drinks

bfs

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn =1010;
 4 
 5 char mp[maxn][maxn];
 6 int vis[maxn][maxn], dis[maxn][maxn];
 7 
 8 struct Node{
 9     int x, y;
10     Node(int x = 0, int y = 0) : x(x), y(y) {}
11 };
12 
13 int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
14 int sx, sy, ex, ey;
15 int n, m, k;
16 
17 void bfs(){
18     Node p1, p2;
19     queue<Node> q;
20     q.push(Node(sx, sy));
21     vis[sx][sy] = (1<<4) - 1;
22     dis[sx][sy] = 0;
23     while(!q.empty()){
24         p2 = q.front();
25         q.pop();
26         if(p2.x == ex && p2.y == ey) return ;
27         for(int i = 0; i < 4; i++){
28             for(int j = 1; j <= k; j++){
29                 int dx = p2.x + d[i][0] * j;
30                 int dy = p2.y + d[i][1] * j;
31                 if(dx < 0 || dx >= n || dy < 0 || dy >= m || mp[dx][dy] == #) break;
32                 if(vis[dx][dy] & (1<<i)) break;
33                 int ok = 0;
34                 if(!vis[dx][dy]) ok = 1;
35                 vis[dx][dy] |= (1<<i);
36                 if(ok){
37                     dis[dx][dy] = dis[p2.x][p2.y] + 1;
38                     q.push(Node(dx, dy));
39                 }
40             
41             }
42             
43         }
44     }
45     
46 }
47 
48 int main(){
49     //freopen("in.txt", "r", stdin);
50     memset(dis, -1, sizeof(dis));
51     memset(vis, 0, sizeof(vis));
52     scanf("%d %d %d", &n, &m, &k);
53     for(int i = 0; i < n; i++) scanf("%s", mp[i]);
54     scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
55     sx--; sy--; ex--; ey--;
56     bfs();
57     printf("%d\n", dis[ex][ey]);
58 }
View Code

 

日常训练17-10-25

标签:col   scan   bfs   its   std   target   bre   mes   closed   

原文地址:http://www.cnblogs.com/yijiull/p/7731280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!