You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

2 years ago
/*
100
126
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 cnt = 1;
void dfs(int x, int y) {
a[x][y] = 2;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
if (a[tx][ty] == 1) {
cnt++;
dfs(tx, ty);
}
}
}
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;
}
dfs(x, y);
cout << cnt << endl;
return 0;
}