当前位置:网站首页>[ybtoj advanced training guidance] cross the river [BFS]
[ybtoj advanced training guidance] cross the river [BFS]
2022-07-02 12:33:00 【VL—MOESR】

Ideas :
We can layer by layer BFS, Every time you walk all the same terrain as the current one, you can't walk , If you encounter different terrain, you can judge whether to make a bamboo raft .
c o d e code code
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int dx[8]={
0, 1, 0, -1, 1, 1, -1, -1};
int dy[8]={
1, 0, -1, 0, 1, -1, 1, -1};
struct code
{
int x, y;
code(int a=0, int b=0)
{
x=a;
y=b;
}
};
int n, k;
int ma[1010][1010], f[1010][1010], ans[1010][1010];
queue<code> q, b;
void bfs(int x, int y)
{
b.push(code(x, y));
f[x][y]=2;
while(!b.empty())
{
code u=b.front();
b.pop();
for(int i=0; i<8; i++)
{
int xx=u.x+dx[i], yy=u.y+dy[i];
if(xx<0||xx>n+1||yy<0||yy>n+1)
continue;
if(ma[u.x][u.y]==ma[xx][yy])
{
if(f[xx][yy]==2)
continue;
f[xx][yy]=2;
ans[xx][yy]=ans[u.x][u.y];
b.push(code(xx, yy));
}
else
{
if(f[xx][yy])
continue;
f[xx][yy]=1;
q.push(code(xx, yy));
}
}
}
}
int main()
{
scanf("%d%d", &n, &k);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%1d", &ma[i][j]);
q.push(code(0, 0));
int last=0, sum=0;
while(!q.empty())
{
code u=q.front();
q.pop();
if(f[u.x][u.y]==2)
continue;
if(ma[u.x][u.y]!=last&&ma[u.x][u.y])
sum++;
ans[u.x][u.y]=sum;
bfs(u.x, u.y);
last=ma[u.x][u.y];
}
for(int i=1; i<=k; i++)
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d ", ans[x][y]);
}
return 0;
}
边栏推荐
- Is the neural network (pinn) with embedded physical knowledge a pit?
- CDH6之Sqoop添加数据库驱动
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- Sweetheart leader: Wang Xinling
- Adding database driver to sqoop of cdh6
- Differences between nodes and sharding in ES cluster
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- Sub thread get request
- The blink code based on Arduino and esp8266 runs successfully (including error analysis)
- Sse/avx instruction set and API of SIMD
猜你喜欢

MySQL and PostgreSQL methods to grab slow SQL

Go learning notes - multithreading

Drools dynamically add, modify, and delete rules

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students

Take you ten days to easily finish the finale of go micro services (distributed transactions)

(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.

drools中then部分的写法

Sort---

防抖 节流

倍增 LCA(最近公共祖先)
随机推荐
子线程获取Request
Leetcode209 长度最小的子数组
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
Drools dynamically add, modify, and delete rules
二分刷题记录(洛谷题单)区间的甄别
深拷貝 事件總線
Codeforces 771-div2 C (trouble, permutation is not very good)
Adding database driver to sqoop of cdh6
Initial JDBC programming
Leetcode - Sword finger offer 37, 38
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
MySQL indexes and transactions
Calculate the maximum path sum of binary tree
[I'm a mound pytorch tutorial] learning notes
Deep Copy Event bus
LeetCode—剑指 Offer 51. 数组中的逆序对
甜心教主:王心凌
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.