bfs找油田问题= =

Problem L

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 10
Font: Times New Roman | Verdana | Georgia
Font Size:  

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

0
1
2
2


思路:果断bfs之,在以前写过的bfs代码上面稍作修改即可得到正确结果。


#include <stdio.h>
#include <string.h>
struct queuenode{								//建结构
	int a, b;
	}queue[100000];

int count, tx, ty, head = 0,tail = 0;			//定义
int a, b, n, temp;
int dir[8][2] = { {1,0} , {0,1} , {-1,0} , {0,-1} , {1,1} , {-1,-1} , {1,-1} , {-1,1} };
bool map[110][110];
int m, k, i, j;
int inarea(int x,int y)					//判断是否在区域内
{
	return x >= 0 && y >= 0 && x < a && y < b;
}

void bfs(  )									//bfs
{
	count++;
	tail = head;
	queue[head].a = i;							//初始化队列头部
	queue[head].b = j;
	map[i][j] = 1;
	while(head <= tail){	    				//在队列中有数据的情况下进行循环
		for(k = 0;k < 8; k++){				//八个方向了~!
			tx = queue[head].a + dir[k][0];
			ty = queue[head].b + dir[k][1];
			if(inarea(tx, ty) && map[tx][ty]== 0){
				tail++;
				queue[tail].a = tx;
				queue[tail].b = ty;
				map[tx][ty] = 1;
			}
		}
		head++;
	}
	return;
}
		
int main(void)									//主函数
{
	while(scanf("%d%d",&a,&b)==2&&a!=0){
		memset(map, 1, sizeof(map));
		while(scanf(" ")||scanf("\n")) ;
		for(i = 0;i < a; i++){					//扫描地图
			for(j = 0;j < b; j++){
				scanf("%c",&temp);
				if (temp == '*') map[i][j] = 1;
				else if (temp == '@') map[i][j] = 0;
				else {
					j--;
					continue;
				}
			}
		}
		
		count = 0;								//初始化计数
		for(i = 0;i < a;i ++){
			for(j = 0;j < b;j ++){
				if (map[i][j] == 0){			//从找到油田开始进入bfs
					bfs();
				}
			}
		}
		printf("%d\n",count);
	}
	return 0;
}
	
	



你提供的信息: ``` PS D:\py> & C:/Users/nima5/python/python.exe d:/py/算法实习。py.py ==== 小规模 BFS 测试 ==== ``` 看起来你的程序运行后输出了“小规模 BFS 测试”这个提示语,但没有给出完整的错误或结果输出。为了更好地帮助你解决问题,我将假设你在实现一个 **Breadth-First Search(BFS)广度优先搜索算法** 来处理图结构,并且你想测试它在小规模数据上的行为。 --- ### ✅ 问题描述 你可能正在编写一个图的遍历算法,使用 BFS 来访问节点或者寻路径。你遇到了一些问题,比如无法正确遍历、死循环、节点未被访问等。 --- ### ✅ 解决方案:Python 实现 BFS 广度优先搜索算法 以下是一个完整的小规模图 BFS 示例代码,适用于邻接表表示的图: ```python from collections import deque def bfs(graph, start): """ 广度优先搜索算法 参数: graph: 图的邻接表表示,例如 {0: [1, 2], 1: [2], 2: [0]} start: 起始节点编号 返回: visited: 按访问顺序排列的节点列表 """ visited = [] # 记录已访问的节点 queue = deque([start]) # 使用双端队列进行 BFS visited_set = set() # 快速判断是否已访问 while queue: node = queue.popleft() if node not in visited_set: visited.append(node) visited_set.add(node) # 将当前节点的所有邻居加入队列(未访问过的) for neighbor in graph[node]: if neighbor not in visited_set: queue.append(neighbor) return visited # ==== 小规模测试用例 ==== if __name__ == "__main__": # 定义一个简单的无向图 graph = { 0: [1, 2], 1: [0, 3], 2: [0, 4], 3: [1], 4: [2] } print("==== 小规模 BFS 测试 ====") result = bfs(graph, start=0) print("BFS 遍历结果:", result) ``` #### 输出示例: ``` ==== 小规模 BFS 测试 ==== BFS 遍历结果: [0, 1, 2, 3, 4] ``` --- ### ✅ 代码解释 - `graph`: 使用字典表示图,每个节点对应其相邻节点列表。 - `queue`: 使用 `deque` 提高效率,先进先出。 - `visited_set`: 用于避免重复访问节点。 - `bfs()` 函数实现了标准的 BFS 算法逻辑。 --- ### 🔍 常见调试建议 如果你遇到如下问题: - BFS 没有访问所有节点? - 检查图是否是连通的;如果不是,请对每个未访问节点分别调用 BFS。 - 死循环? - 检查是否漏掉了 `visited` 判断。 - 输出顺序不正确? - 检查是否误用了栈(DFS)而不是队列(BFS)。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值