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 }
代码君

 

posted @ 2014-08-18 08:45  AOQNRMGYXLMV  阅读(464)  评论(0编辑  收藏  举报