求助
查看原帖
求助
1023793
yaodiguoan楼主2023/10/21 07:19

这个代码他只会遍历到他自己

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
int n,a[110][110];
int dx[5]={0,0,1,0,-1};
int dy[6]={0,1,0,-1,0};
bool vis[110][110];
bool ifs(int tx,int ty,int x,int y){
	if(tx<=n and tx>=1 and ty<=n and ty>=1 and a[tx][ty]>a[x][y] and ((abs(tx-x)==1 and abs(ty-y)>1) or (abs(ty-y)==1 and abs(tx-x)>1))) return true;
	return false;
}
struct csp{
	int x,y;
};
void bfs(int x,int y){
	queue<csp> q;
	q.push((csp){x,y});
	while(!q.empty()){
		x=q.front().x;
		y=q.front().y;
		q.pop();
		if(vis[x][y]) continue;
		vis[x][y]=true;
		for(int i=1;i<=n;i++){
			int tx=x+dx[i];
			int ty=y+dy[i];
			if(ifs(tx,ty,x,y)) q.push((csp){tx,ty});
		}
	}
}
int main(){
	int x,y,cnt=0;
	cin>>n>>x>>y;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)	
			cin>>a[i][j];
	memset(vis,0,sizeof(vis));
	bfs(x,y);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(vis[i][j]) cnt++;
	cout<<cnt;
	return 0;
}

2023/10/21 07:19
加载中...