HDU 1241 Oil Deposits bfs

题目链接:Oil Deposits

这种水题还来写我是拒绝的。但是。记录吧。陷入对于编程无感的状态、

 1 /*
 2   首先它水 然后做过了。但是就是没有一点思路应该怎么做。被王sir嫌弃 好失落。
 3   然后。我大腿启示我。
 4   直接搜呗。从所有可能的点随便找一个,搜。走过的路径就标记上,然后,再从剩下的找。直到所有的点都标记完就可以了、
 5   我T_T、总是不能自己想出思路来、
 6  */
 7 
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <iostream>
11 #include <queue>
12 using namespace std;
13 
14 char mp[210][210];
15 int vis[210][210];
16 int ans;
17 int n, m;
18 
19 struct Node {
20     int x, y;
21 }node[2100], now, temp;
22 
23 bool check() {
24     for (int i=0; i<n; ++i) {
25         for (int j=0; j<m; ++j) {
26             if (mp[i][j] == '@' && vis[i][j] == 0)
27                 return false;
28         }
29     }
30     return true;
31 }
32 
33 int dir[8][2] = {1, 0,  -1, 0,  0, 1,  0, -1,  -1, -1,  -1, 1,  1, -1,  1, 1};
34 queue<Node> que;
35 
36 bool checkIt(int i, int j) {
37     if (i>=0 && i<n && j>=0 && j<m && vis[i][j] == 0)
38         return true;
39     return false;
40 }
41 
42 
43 void bfs() {
44     while(!check()) {
45         bool flag = true;
46         for (int i=0; i<n; ++i) {
47             for (int j=0; j<m; ++j) {
48                 if (mp[i][j] == '@' && vis[i][j] == 0) {
49                     now.x = i, now.y = j;
50                     vis[i][j] = 1;
51                     que.push(now);
52                     ans += 1;
53                     flag = false;
54                     break;
55                 }
56             }
57             if (flag == false) break;
58         }
59         while(!que.empty()) {
60             now = que.front();
61             que.pop();
62             for (int i=0; i<8; ++i) {
63                 temp.x = now.x + dir[i][0];
64                 temp.y = now.y + dir[i][1];
65 
66                 if (!vis[temp.x][temp.y] && checkIt(temp.x, temp.y) && mp[temp.x][temp.y] == '@') {
67                     vis[temp.x][temp.y] = 1;
68                     que.push(temp);
69                 }
70             }
71         }
72     }
73 }
74 
75 int main() {
76     while (cin >> n >> m) {
77         if(m == 0) break;
78         ans = 0;
79         memset(vis, 0, sizeof(vis));
80         while(!que.empty()) {
81             que.pop();
82         }
83 
84         for (int i=0; i<n; ++i) {
85             for (int j=0; j<m; ++j) {
86                 cin >> mp[i][j];
87             }
88         }
89 
90         bfs();
91         cout << ans << endl;
92     }
93     return 0;
94 }
View Code
原文地址:https://www.cnblogs.com/icode-girl/p/5167690.html