zoukankan      html  css  js  c++  java
  • HDU 1241 Oil Deposits

    题目链接:HDU 1241 Oil Deposits

    题目大意:
    GeoSurvComp地质调查公司负责探测地下石油储藏。GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。

    题解:
    逐行寻找“@”,若找到执行DFS把与此“@”相连的“@”(包括此“@”)全部置为“*”。

    #include <iostream>
    #include <cstring>
    using namespace std;
    #define ms(a, b) memset(a, b, sizeof(a))
    
    int num, n, m;
    char mat[510][510];
    
    void dfs(int x, int y) {
    	if (x < 0 || y < 0 || x > n || y > m)	return;
    	mat[x][y] = '*';
    	if (mat[x + 1][y + 1] == '@')	dfs(x + 1, y + 1);
    	if (mat[x - 1][y - 1] == '@')	dfs(x - 1, y - 1);
    	if (mat[x + 1][y - 1] == '@')	dfs(x + 1, y - 1);
    	if (mat[x - 1][y + 1] == '@')	dfs(x - 1, y + 1);
    	if (mat[x][y + 1] == '@') 	dfs(x, y + 1);
    	if (mat[x][y - 1] == '@')	dfs(x, y - 1);
    	if (mat[x + 1][y] == '@')	dfs(x + 1, y);
    	if (mat[x - 1][y] == '@')	dfs(x - 1, y);
    }
    
    int main() {
    	while (cin >> n >> m) {
    		if (!n && !m) {
    			break;
    		}
    		ms(mat, 0);
    		num = 0;
    		for (int i = 1; i <= n; ++i) {
    			for (int j = 1; j <= m; ++j) {
    				cin >> mat[i][j];
    			}
    		}
    		for (int i = 1; i <= n; ++i) {
    			for (int j = 1; j <= m; ++j) {
    				if (mat[i][j] == '@') {
    					++num;
    					dfs(i, j);
    				}
    			}
    		}
    		cout << num << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Beta 冲刺 (3/7)
    软件产品案例分析(团队)
    Beta 冲刺 (2/7)
    Beta 冲刺1
    第七次作业
    第六次作业(计算器第四步)
    课堂作业二 PAT1025 反转链表
    第五次作业(计算器第三步)
    课堂作业一(16/05/04)
    Calculator(补)
  • 原文地址:https://www.cnblogs.com/IzumiSagiri/p/13771157.html
Copyright © 2011-2022 走看看