不认识的单词:
grid : 网格
plot:阴谋、一小块地图
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. 一个plot有油 == pocket, 如果两个pocket连着,他们是同一个油储量。 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
Sample Output 0 1 2 2 |
#include <iostream>
using namespace std;
char maze[101][101]; // maze:迷宫
bool mark[101][101];
int n, m;
int cnt; //连通分量的个数
bool isInMap(int y, int x) {
if ( y >= 1 && y <= n && x >= 1 && x <= m) {
return true;
}
return false;
}
void dfs(int y, int x) {
//cout << "y and x:" << y << " " << x << endl;;
mark[y][x] = 1; // 先把自己标记了
//上下左右 对角线 8个方向连着的,mark了
int nx, ny;
//下:
ny = y + 1;
nx = x;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
// ↙
ny = y + 1;
nx = x - 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
// ↘
ny = y + 1;
nx = x + 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
//上:
ny = y - 1;
nx = x;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
//↖
ny = y - 1;
nx = x - 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
//↗
ny = y - 1;
nx = x + 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
//左
ny = y;
nx = x - 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
//右
ny = y;
nx = x + 1;
if (isInMap(ny, nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny, nx);
}
}
int main() {
while (cin >> n >> m && n != 0 && m != 0) {
cnt = 0;
// 输入
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j++) {
cin >> maze[i][j];
}
}
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j++) {
mark[i][j] = 0; // 0:没访问过
}
}
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j++) {
if (maze[i][j] == '@' && mark[i][j] == 0) {
dfs(i, j); //把周围的mark了
cnt ++ ;
}
}
}
cout << cnt << endl;
}
}
#include <iostream>
using namespace std;
char maze[101][101];
bool mark[101][101];
int xMax,yMax;
int dir[8][2] = {
{1,0},
{-1,0},
{0,-1},
{0,1},
{1,-1},
{1,1},
{-1,-1},
{-1,1}
};
bool isInMap(int y,int x) {
if (x >= 1 && x <= xMax && y >=1 && y <= yMax) {
return 1;
}
return 0;
}
void dfs(int y,int x) {
mark[y][x] = 1;
// cout << "test : " << y <<x << endl;
for (int i = 0; i <= 7; i++) {
int ny = y + dir[i][0];
int nx = x + dir[i][1];
if (isInMap(ny,nx) && maze[ny][nx] == '@' && mark[ny][nx] == 0) {
dfs(ny,nx);
}
}
}
int getCnt() {
int cnt = 0;
for (int i = 1; i <= yMax ; i++) {
for (int j = 1; j <= xMax ; j ++) {
if (maze[i][j] == '@' && mark[i][j] == 0) {
cnt++;
dfs(i,j);
}
}
}
return cnt;
}
int main() {
while(cin >> yMax >> xMax) {
if (yMax == 0 && xMax == 0) {
return 0;
}
for (int i = 1; i <= yMax; i++) {
for (int j = 1; j <= xMax; j++) {
cin >> maze[i][j];
mark[i][j] = 0;
}
}
int cnt = getCnt();
cout << cnt << endl;
}
}