为什么我在c++里运行#1是对的但评测时就是错的???
查看原帖
为什么我在c++里运行#1是对的但评测时就是错的???
727649
sheryang_s_son楼主2023/6/9 21:50

评测结果

Code:

#include<bits/stdc++.h>
using namespace std;
int ans=0,tempx,tempy;
bool a[101][101],b[101][101],s[101][101];
int xx[5]={0,1,-1,0,0};
int yy[5]={0,0,0,1,-1};
char temp;
int n,m;
void dfs(int x,int y){
	if(x==0 || x>=n+1 || y==0 || y>=m+1) return ;
	for(int i=1;i<=4;i++){ 
		tempx=x+xx[i]; tempy=y+yy[i];
		if(tempx<=0 || tempy<=0 || tempx>n || tempy>m){
			continue;
		}
		if(a[tempx][tempy] && s[tempx][tempy]){
			s[x][y]=false;
			b[tempx][tempy]=false;
			dfs(tempx,tempy);
			s[x][y]=true;
		}
	}
}
int main(){
	cin>>n>>m;
	getchar();
	memset(b,true,sizeof(b));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			temp=getchar();
			a[i][j]= temp=='0' ? false : true ;
		}
		getchar();
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			
			if(b[i][j] && a[i][j]){
				b[i][j]=false;
				memset(s,true,sizeof(s));
				dfs(i,j);
				ans++;
			}
			
		}
	}
	cout<<ans;
	return 0;
}
2023/6/9 21:50
加载中...