Oil Deposits
来源:互联网 发布:linux cdn搭建 编辑:程序博客网 时间:2024/05/17 08:59
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
0122
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;}
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; }
阅读全文
0 0
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- oil deposits
- Oil Deposits
- Oil Deposits
- Arrays.copy()和ArrayList.clone()的使用区别
- 18 个 PHP 开源内容管理系统(CMS)
- CTF实验吧-上传绕过【0x00截断】
- HDU 1281 棋盘游戏 二分匹配
- 逻辑与
- Oil Deposits
- Stream:java1.8新特性
- Understanding Marked Sub-Expressions and Captures
- PX4log文件分析工具
- JUnit4单元测试入门
- 474. Ones and Zeroes
- PHP常用函数
- 【学习笔记】初识JavaScript
- AC自动机1——适用于utf-8编码的Trie树