Uva-572 - Oil Deposits(DFS)
2015-09-01 17:19
459 查看
Oil Deposits |
Input
The input file contains one or more grids. Each grid begins with a line containing m and n,the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise and .Following this are mlines of n characters each (not counting the end-of-linecharacters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing anoil pocket.Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain morethan 100 pockets.Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
#include <cstdio>#include <cstring>using namespace std;#define BG 105struct point{char c;int st;};point square[BG][BG];int m,n;void set_p(int i,int j){if(square[i][j].c=='@'&&square[i][j].st==0){square[i][j].st=1;if(i+1<m)//don't forget the diagonal{set_p(i+1,j);if(j+1<n)set_p(i+1,j+1);if(j-1>=0)set_p(i+1,j-1);}if(i-1>=0){set_p(i-1,j);if(j+1<n)set_p(i-1,j+1);if(j-1>=0)set_p(i-1,j-1);}if(j+1<n)set_p(i,j+1);if(j-1>=0)//8 types of directions in totalset_p(i,j-1);}}int main(){while(scanf("%d%d",&m,&n)==2&&m!=0){getchar();int sum=0;memset(square,0,sizeof(point)*BG*BG);int i,j;for(i=0;i<m;i++){for(j=0;j<n;j++)scanf("%c",&square[i][j].c);getchar();}for(i=0;i<m;i++)for(j=0;j<n;j++)if(square[i][j].c=='@'&&square[i][j].st==0){sum++;set_p(i,j);}printf("%d\n",sum);}}
相关文章推荐
- 明星软件工程师的10种特质
- Windows 10 应用隐私声明——MVA字幕修改器
- Android (Linux) Suspend流程
- Highcharts多个Y轴动态显示时,yAxis坐标的双重显示
- Node.js开发入门—HelloWorld再分析
- 26.the app reference non-public selectors in payload.... userEmail
- 27.代理 Delegate写法
- java中图片自适应
- IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
- 欢迎使用CSDN-markdown编辑器
- python 基于dns 轮询的业务检测
- Javascript模块化编程(二):AMD规范
- lua类对象创建继承和使用
- 25.VRGCalendar一款支持标记自定义日期的日历选择器
- source /build/envsetup.sh和lunch)
- LPCScrypt, DFUSec : USB FLASH download, programming, and security tool, LPC-Link 2 Configuration tool, Firmware Programming
- ovirt-engine安装
- js参数传递分析
- 每日python(5)
- 关闭Linux里边的selinux