Oil Deposits
来源:互联网 发布:蜜蜂软件 编辑:程序博客网 时间:2024/05/29 08:50
Problem Description
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.
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 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or
@’, representing an oil 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 more than 100 pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
典型的dfs深度搜索题
import java.util.Scanner;public class OilDeposits { static String [][][] b = new String[50][100][100]; static int[] m = new int[50]; static int[] n = new int[50]; static int[][] move = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};//初始化 右左上下 右上 左下 右下 左上 static void dfs(int p,int j,int k){//核心部分 int nextX,nextY,q; b[p][j][k]="*";//特殊化标记,省去了标记判断 for( q = 0;q < 8;q++) {//八方向搜索 nextX = j + move[q][0]; nextY = k + move[q][1]; if(nextX >= 0 && nextX< m[p] && nextY >= 0 && nextY< n[p] ) {//check后在当前方向bfs if( b[p][nextX][nextY].equals("@")) dfs(p, nextX, nextY); } } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int[] result=new int[50]; int i=0; String [][] a = new String[50][100]; m[i] = sc.nextInt(); n[i] = sc.nextInt(); while (m[i] != 0 && n[i] != 0){ for(int j = 0;j < m[i];j++) a[i][j]=sc.next(); i++; m[i] = sc.nextInt(); n[i] = sc.nextInt(); }//end input for(int p = 0;p < i;p++) for(int j = 0;j < m[p]; j++) for(int k = 0; k < n[p];k++) { b[p][j][k] = a[p][j].substring(k, k + 1);//转换输入为字符串数组 //System.out.println(b[p][j][k]); }//end change for(int p = 0;p < i;p++) for(int j = 0;j < m[p]; j++) for(int k = 0; k < n[p];k++){ if( b[p][j][k].equals("@")) { dfs(p,j,k); result[p]++; } } for(int p = 0;p < i;p++) System.out.println(result[p]); }}
本来想昨天写的,实在是写不下去,上午的那java题目真的有毒,就不吐槽了,只能说学院各种不支持,花了一上午ac两个水题,后面的一个Timelimit,优化不来,一个wronganswer。。还有一个最不会的图的题,直接放弃。总体而言,不开心,感觉这方面差距贼大。。虽然比赛是结束了,但是练习并没有结束,以后持续更新。
好了,就这,希望上面的做法能对有疑惑的小伙伴提供帮助。
- 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
- static的使用
- url,href,src的区别
- 折中查找
- threejs知识点:1.模型分析
- 卡尔曼滤波--学习笔记
- Oil Deposits
- java操作mongodb——查询数据
- anroid4.4textview莫名崩溃
- SpringBoot poi 导出Excel
- Java版腾讯云短信验证码获取
- Java线程(四):Delayed、ScheduledFuture、RunnableScheduledFuture
- Linux下的Notepad++编辑器——Notepadqq
- JSON字符串转换为JSON对象,JSON对象转化为JSON字符串
- GOsUgar, GoHackathon 项目复盘 + 一些对 Hackathon的思考