'commit'
This commit is contained in:
53
TangDou/LanQiaoBei/LanQiao12GuoSai/BC6_bfs.cpp
Normal file
53
TangDou/LanQiaoBei/LanQiao12GuoSai/BC6_bfs.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
第六题:摘苹果(100分)
|
||||
果园里有两种水果,苹果(1)和梨子(2),起点(6)也是苹果。
|
||||
从起点开始,不能碰到梨子,求最多能摘到多少个苹果。
|
||||
输入:
|
||||
3 4
|
||||
2 1 2 1
|
||||
1 6 1 2
|
||||
1 1 1 2
|
||||
输出:
|
||||
7
|
||||
*/
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
typedef pair<int, int> PII;
|
||||
int a[N][N];
|
||||
int n, m;
|
||||
int x, y;
|
||||
int dx[] = {-1, 0, 1, 0}; //上右下左
|
||||
int dy[] = {0, 1, 0, -1}; //上右下左
|
||||
int bfs(int x, int y) {
|
||||
queue<PII> q;
|
||||
int cnt = 1;
|
||||
q.push({x, y});
|
||||
a[x][y] = 2;
|
||||
|
||||
while (q.size()) {
|
||||
auto t = q.front();
|
||||
q.pop();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int tx = t.first + dx[i], ty = t.second + dy[i];
|
||||
if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
|
||||
if (a[tx][ty] == 1) {
|
||||
q.push({tx, ty});
|
||||
a[tx][ty] = 2;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= m; j++) {
|
||||
cin >> a[i][j];
|
||||
if (a[i][j] == 6) x = i, y = j;
|
||||
}
|
||||
cout << bfs(x, y) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user