社区讨论
70pts秋涛
P114101迷宫参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mhizobc2
- 此快照首次捕获于
- 2025/11/03 18:20 4 个月前
- 此快照最后确认于
- 2025/11/03 18:20 4 个月前
CPP
//Source Code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <cctype>
#include <set>
#include <stack>
//using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
constexpr i64 N = 1e3 + 10;
constexpr i64 INF = 1e9 + 10;
constexpr i64 MOD = 998244353;
constexpr i64 P = 1e9 + 7;
i64 ans;
int _;
inline void solve ();
inline void ask ();
signed main(){
std :: ios :: sync_with_stdio(false);
std :: cin.tie(0) , std :: cout.tie(0);
//std :: cin >> _;
_ = 1;
while(_ --> 0) solve();
return (0 - 0);
}
/*
2 2
01
10
1 1
2 2
*/
using pii = std :: pair <int , int>;
int n , m;
int a[N][N];
std :: queue <pii> q;
bool vis[N][N];
int dx[] = {0 , 1 , 0 , -1};
int dy[] = {1 , 0 , -1 , 0};
int step;
inline void bfs (int x , int y) {
while (!q.empty ()) {
pii f = q.front ();
q.pop ();
int X = f.first , Y = f.second ;
if (vis[X][Y]) continue ;
vis[X][Y] = true;
step ++;
int color = a[X][Y];
for (int i = 0;i < 4;i ++) {
int fx = X + dx[i] , fy = Y + dy[i];
if (fx < 1 || fx > n || fy < 1 || fy > n || vis[fx][fy]) continue;
if ((a[fx][fy] == 0 && color == 1) || (a[fx][fy] == 1 && color == 0)) {
// std :: cout << step << '\n';
q.push ({fx , fy});
//bfs (fx , fy);
}
}
}
return ;
}
inline void ask () {
step = 0;
while (!q.empty ()) q.pop ();
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= n;j ++)
vis[i][j] = false;
int x , y;
std :: cin >> x >> y;
q.push ({x , y});
bfs (x , y);
std :: cout << step << '\n';
return ;
}
inline void solve(){
std :: cin >> n >> m;
for (int i = 1;i <= n;i ++) {
std :: string s;
std :: cin >> s;
for (int j = 0;j < (int)s.size ();j ++)
a[i][j + 1] = s[j] - '0';
}
while (m --) ask ();
return ;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...