文章
8
粉丝
14
获赞
1
访问
7.2k
其实这个题不需要用vis数组,访问过的直接用'*'把'#'填了就好了
//AC代码
#include<bits/stdc++.h>
using namespace std;
int m, n;
char mp[105][105];
vector<vector<int>> direction {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
int ans;
void dfs(int x, int y) {
if(mp[x][y] == '*' || !mp[x][y])
return;
//用*把#给填了
mp[x][y] = '*';
for(int i = 0; i < 8; i++) {
int nx = x + direction[i][0];
int ny = y + direction[i][1];
dfs(nx, ny);
}
}
int main()
{
while(cin >> m >> n) {
if(m == 0 && n == 0)
break;
ans = 0;
memset(mp, 0, sizeof(mp));
for(int i = 1; i <= m; i++) {
string s;
cin >> s;
for(int j = 1; j <= n; j++) {
mp[i][j] = s[j-1];
}
}
//把每个点都访问一遍,被访问过的都用'*'填上了
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
if(mp[i][j] == '@') {
dfs(i, j);
++ans;
}
cout << ans << endl;
...
登录后发布评论
暂无评论,来抢沙发