社区讨论
90分最后超时求调!!!
P114101迷宫参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mhjgyrej
- 此快照首次捕获于
- 2025/11/04 02:24 4 个月前
- 此快照最后确认于
- 2025/11/04 02:24 4 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
#define N 1010
int a[N][N];
int vis[N][N]={};
int bfs(int x,int y){
int s=1;
bool is[N][N]={};
vector<pair<int,int>> v;
is[x][y]=true;
v.push_back({x,y});
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty()){
pair<int,int> top=q.front();
q.pop();
if(top.second+1<n&&a[top.first][top.second]==!a[top.first][top.second+1])
if(is[top.first][top.second+1]==false){
q.push({top.first,top.second+1});
s++;
is[top.first][top.second+1]=true;
v.push_back({top.first,top.second+1});
}
if(top.first+1<n&&a[top.first][top.second]==!a[top.first+1][top.second])
if(is[top.first+1][top.second]==false){
q.push({top.first+1,top.second});
s++;
is[top.first+1][top.second]=true;
v.push_back({top.first+1,top.second});
}
if(top.second-1>=0&&a[top.first][top.second]==!a[top.first][top.second-1])
if(is[top.first][top.second-1]==false){
q.push({top.first,top.second-1});
s++;
is[top.first][top.second-1]=true;
v.push_back({top.first,top.second-1});
}
if(top.first-1>=0&&a[top.first][top.second]==!a[top.first-1][top.second])
if(is[top.first-1][top.second]==false){
q.push({top.first-1,top.second});
s++;
is[top.first-1][top.second]=true;
v.push_back({top.first-1,top.second});
}
}
for(int i=0;i<v.size();i++){
vis[v[i].first][v[i].second]=s;
}
return s;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
char ppppp;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>ppppp;a[i][j]=ppppp-'0';
}
}
int x,y;
while(m--){
cin>>x>>y;
x--,y--;
if(vis[x][y]!=0)cout<<vis[x][y]<<"\n";
else cout<<bfs(x,y)<<"\n";
}
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...