Tyvj 1030 乳草的入侵

以一个简单的BFS对基础搜索做一个收尾好了。

给一个草,然后以这棵草为九宫格的中心,每周向周围八个方向扩散,问多少个星期能把这个农场占满。

遍历整个map,最后一个出队列的对应的星期数就是所求。

 

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 
 8 struct Point
 9 {
10     int x, y;
11     int days;
12 }start;
13 
14 char map[105][105];
15 int r, c, ans;
16 int dir[8][2] = {1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
17 
18 bool islegal(int x, int y)
19 {
20     return (x>=0 && x<r && y>=0 && y<c &&map[x][y]=='.');
21 }
22 
23 void BFS(void)
24 {
25     queue<Point> qu;
26     Point cur, next;
27     start.days = 0;
28     qu.push(start);
29     while(!qu.empty())
30     {
31         cur = qu.front();
32         qu.pop();
33         for(int i = 0; i < 8; ++i)
34         {
35             next = cur;
36             next.x += dir[i][0], next.y += dir[i][1];
37             if(islegal(next.x, next.y))
38             {
39                 next.days = ans = cur.days + 1;
40                 map[next.x][next.y] = '*';
41                 qu.push(next);
42             }
43         }
44     }
45 }
46 
47 int main(void)
48 {
49     #ifdef LOCAL
50         freopen("1030in.txt", "r", stdin);
51     #endif
52 
53     scanf("%d%d%d%d", &c, &r, &start.y, &start.x);
54     --start.x, --start.y; 
55     for(int i = r - 1; i >= 0; --i)
56         scanf("%s", map[i]);
57     map[start.x][start.y] = '*';
58     ans = 0;
59     BFS();
60     printf("%d\n", ans);
61     return 0;
62 }
代码君

 

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/3918718.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值