为什么样例4我输出的就是正确答案16,却不给过?
  • 板块题目总版
  • 楼主lymeng
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/12/7 18:10
  • 上次更新2024/12/7 21:13:08
查看原帖
为什么样例4我输出的就是正确答案16,却不给过?
556564
lymeng楼主2024/12/7 18:10
#include<iostream>
#include<cstdio>//文件操作头文件
#include<cstring>
#include <iomanip>
#include<string>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<cctype>
#include<stack>//栈
#include<queue>//队列
#include<deque>//双端队列
using namespace std;
#define endl "\n"//防止调试崩溃
int n,res;
int dx[10]={-1,0,1,0},dy[10]={0,1,0,-1};
int g[500][500],llen[500];
int st[500][500];
string s;

struct node{
	int x;
	int y;
}; 
queue<node> q;

void bfs(int sstart,int eend){
	q.push(node{sstart,eend});
	while(!q.empty()){
		node temp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int xx=dx[i]+temp.x,yy=dy[i]+temp.y;
			if(st[xx][yy]==1){
				continue;
			}
			if(g[xx][yy]==0){
				continue;
			}
			if(xx<0 || yy<0 || xx>=n || yy>=llen[xx]){
				continue;
			}
			st[xx][yy]=1;
			q.push(node{xx,yy});
		}
	}
}
int main(){
	cin>>n;
	s="\n";
	getline(cin,s);
	for(int i=0;i<n;i++){
		getline(cin,s);
		//预处理
		llen[i]=s.length();
		for(int j=0;j<llen[i];j++){
			if(s[j]=='*' || s[j]==' '){
				g[i][j]=0;
			}else{
				g[i][j]=1;
			}
		}
		s.clear();
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<llen[i];j++){
			if(st[i][j]==0 && g[i][j]==1){
				st[i][j]=1;
				bfs(i,j);
				res++;
			}
		}
	}
	cout<<res;
	return 0;
}

2024/12/7 18:10
加载中...