油田问题 bfs
2024-09-05 06:44:57
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <queue>
using namespace std;
int N=;
int ax[]={,,,-,,,-,-};
int ay[]={,-,,,-,,-,};
//bfs队列加对象,对象就是队列的类型,在这个题中就可以直接用x,y
//满足条件(为#)的对象插入队列
struct Point{
int x;
int y;
};
queue<Point> q;
//标记是否到达
int mark[][]={};
char data[][];
void bfs(int i,int j,int a,int b){
mark[i][j]=;
q.push({i,j});
N++;
while(!q.empty()){
Point first=q.front();
q.pop();
for (int k = ; k < ; ++k) {
int x=first.x+ax[k];
int y=first.y+ay[k];
if(x>= && x<a && y>= && y<b && data[x][y]=='@' && mark[x][y]==){
mark[x][y]=;
q.push({x,y});
} }
}
}
int main(){
int a,b;
while(cin>>a>>b) {
if(a== && b==){
break;
}
N=;
for (int i = ; i < a; ++i) {
for (int j = ; j < b; ++j) {
cin >> data[i][j]; } } for (int k = ; k < a; ++k) {
for (int l = ; l < b; ++l) {
//cout<<data[k][l]<<endl;
if (data[k][l] == '@' && mark[k][l]==) { bfs(k, l, a, b);
} } }
cout << N<<endl;
for (int i = ; i < a; ++i) {
for (int j = ; j < b; ++j) {
data[i][j]=; } } for (int m = ; m < ; ++m) {
for (int i = ; i < ; ++i) {
data[m][i]='';
mark[m][i]=; } } } }
最新文章
- 慕课网-Java入门第一季-6-10 练习题
- Maven学习(三)生命周期
- iOS摄像头和相册-UIImagePickerController常用操作
- 用jquery追加的元素不能触发treeview事件
- os.walk获取同级目录具有随机性
- JS判断RadioButtonList是否有选中项
- Play Apple(博弈)
- debian(wheezy) chrome beta 38.0.2x.xxx Shockwave Flash was crashed 该解决方案崩溃.
- Canvas 阴影效果
- PostgreSql问题:ERROR: column ";1"; does not exist
- winscp工具和xshell连接linux机器时切换到root账户
- Win7 VS2017 NASM编译FFMPEG(2018.12.22)
- myeclipse部署报错报空指针异常
- C#实现焦点变色
- python web框架介绍对比
- ORACLE问题定位基本方法
- php检测iis环境是否支持htaccess
- (转)CreateThread与_beginthread,内存泄漏为何因(原帖排版有些不好 ,所以我稍微整理下)
- LeetCode之Regular Expression Matching
- test_bdc