样例过了但全wa,求大佬调>_<!!!
查看原帖
样例过了但全wa,求大佬调>_<!!!
1101322
wuyuqi1028楼主2024/10/4 20:34

样例过了但全wa,求大佬调>_<!!!

#include<iostream>
#include<queue>
using namespace std;
int n,m,ans;
int v[505][505];
char mp[505][505];
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};
struct node {
	int x,y;
};
queue<node> st;
bool ch(int x,int y) {
	if(x<1||x>n||y<1||y>m) {
		return 0;
	}
	if(mp[x][y]=='*'||mp[x][y]=='0') {
		return 0;
	}
	return 1;
}
void bfs(int x,int y) {
	st.push(node {x,y});
	mp[x][y]=0;
	while(st.size()) {
		int hx=st.front().x;
		int hy=st.front().y;
		st.pop();
		for(int i=0; i<4; i++) {
			int nx=hx+dx[i];
			int ny=hy+dy[i];
			if(ch(nx,ny)) {
				mp[nx][ny]='0';
				ans--;
				st.push(node {nx,ny});
			}
		}
	}
}
int main() {
	cin>>n>>m;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			cin>>mp[i][j];
			if(mp[i][j]=='0') {
				ans++;
				mp[i][j]='1';
			} 
		}
	}
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			if(i==1||j==1||i==n||j==m)
				if(mp[i][j]=='1') 
					bfs(i,j);
		}
	}
	cout<<ans;
	return 0;
}
//4 5
//00000
//00*00
//0*0*0
//00*00
//5 5
//*****
//*0*0*
//**0**
//*0*0*
//*****
2024/10/4 20:34
加载中...