Oil Deposits
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
DFS:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX= 1e3;
char s[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int fx[8] = {0,0,-1,1,-1,1,-1,1},fy[8] = {-1,1,0,0,-1,-1,1,1}//8个方向
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i = 0;i < 8;i++)
{
int xx=fx[i]+x;
int yy=fy[i]+y;
if(xx >= 0&&y >= 0&&xx < n&&yy < m&&! vis[xx][yy]&&s[xx][yy] == '@')
{
dfs(xx,yy);
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
memset(vis,0,sizeof(vis));
for(int i = 0;i < n; i++)
{
scanf("%s",s[i]);
}
int ans=0;
for(int i = 0;i < n; i++)
for(int j = 0;j < m; j++)
{
if( !vis[i][j]&& s[i][j]=='@')
{
ans++;
dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX= 1e3;
char s[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int fx[8] = {0,0,-1,1,-1,1,-1,1},fy[8] = {-1,1,0,0,-1,-1,1,1}//8个方向
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i = 0;i < 8;i++)
{
int xx=fx[i]+x;
int yy=fy[i]+y;
if(xx >= 0&&y >= 0&&xx < n&&yy < m&&! vis[xx][yy]&&s[xx][yy] == '@')
{
dfs(xx,yy);
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
memset(vis,0,sizeof(vis));
for(int i = 0;i < n; i++)
{
scanf("%s",s[i]);
}
int ans=0;
for(int i = 0;i < n; i++)
for(int j = 0;j < m; j++)
{
if( !vis[i][j]&& s[i][j]=='@')
{
ans++;
dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
BFS:
数组不要开的带大
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=1e3;//不要太大,超时
int vis[MAX][MAX];
char s[MAX][MAX];
int n,m,ans;
int fx[8]={0,0,-1,1,-1,1,-1,1},fy[8]={-1,1,0,0,-1,-1,1,1};
struct node
{
int x,y;
}st,pt;
void bfs(int x,int y)
{
vis[x][y]=1;
st.x=x;
st.y=y;
queue <node> q;
q.push(st);
while (!q.empty())
{
st=q.front();
q.pop();
for(int i=0;i<8;i++)
{
pt.x=st.x+fx[i];
pt.y=st.y+fy[i];
if(pt.x>=0&& pt.x< n&&pt.y>=0&&pt.y<m&&!vis[pt.x][pt.y]&&s[pt.x][pt.y]=='@')
{
q.push(pt);
vis[pt.x][pt.y]=1;
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
memset(vis,0,sizeof(vis));
ans=0;
for(int i =0 ; i < n; i++)
{
scanf("%s",&s[i]);
}
for(int i=0 ; i < n ; i ++)
for ( int j =0 ; j< m;j++)
{
if(s[i][j]=='@'&&!vis[i][j])
{
ans++;
bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=1e3;//不要太大,超时
int vis[MAX][MAX];
char s[MAX][MAX];
int n,m,ans;
int fx[8]={0,0,-1,1,-1,1,-1,1},fy[8]={-1,1,0,0,-1,-1,1,1};
struct node
{
int x,y;
}st,pt;
void bfs(int x,int y)
{
vis[x][y]=1;
st.x=x;
st.y=y;
queue <node> q;
q.push(st);
while (!q.empty())
{
st=q.front();
q.pop();
for(int i=0;i<8;i++)
{
pt.x=st.x+fx[i];
pt.y=st.y+fy[i];
if(pt.x>=0&& pt.x< n&&pt.y>=0&&pt.y<m&&!vis[pt.x][pt.y]&&s[pt.x][pt.y]=='@')
{
q.push(pt);
vis[pt.x][pt.y]=1;
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
memset(vis,0,sizeof(vis));
ans=0;
for(int i =0 ; i < n; i++)
{
scanf("%s",&s[i]);
}
for(int i=0 ; i < n ; i ++)
for ( int j =0 ; j< m;j++)
{
if(s[i][j]=='@'&&!vis[i][j])
{
ans++;
bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}