-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path1241.cpp
120 lines (117 loc) · 2.1 KB
/
1241.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
char map[110][110];
int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{-1,1},{1,-1}};
int vis[110][110];
int next_x,next_y;
void dfs(int x,int y)
{
map[x][y]='*'; //标志一下,避免重复计数
for(int k=0;k<8;k++)
{
next_x=x+dir[k][0];
next_y=y+dir[k][1];
if(map[next_x][next_y]=='@')
{
dfs(next_x,next_y);
}
}
}
int main()
{
int n,m;
while(cin>>n>>m,m&&n)
{
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>map[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='@')
{
count++;
dfs(i,j);
}
}
}
printf("%d\n",count);
}
return 0;
}
--------------------------------
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
char a[100][100];
int sum;
int dir[8][2]={1,0,1,1,1,-1,0,1,0,-1,-1,0,-1,1,-1,-1}; //8个方向
struct node
{
int x;
int y;
};
int OK(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return true;
return false;
}
void bfs(int x,int y)
{
int i;
node now,next;
queue<node> q;
now.x=x;
now.y=y;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<8;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(OK(next.x,next.y)&&a[next.x][next.y]=='@') //是油田哦
{
a[next.x][next.y]='*'; //油田群遍历,标记
q.push(next);
}
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&m)
{
sum=0;
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]=='@') //找到油田
{
sum++;
bfs(i,j);
}
}
}
printf("%d\n",sum);
}
return 0;
}