当前位置:网站首页>[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;
}
边栏推荐
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- 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
- 全链路压测
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- MySQL and PostgreSQL methods to grab slow SQL
- CONDA common command summary
- [old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
- drools决策表的简单使用
- Tas (file d'attente prioritaire)
- Go学习笔记—基于Go的进程间通信
猜你喜欢
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Map and set
测试左移和右移
Deep understanding of P-R curve, ROC and AUC
中国交通标志检测数据集
Test shift left and right
Drools dynamically add, modify, and delete rules
ThreadLocal的简单理解
倍增 LCA(最近公共祖先)
Writing method of then part in drools
随机推荐
Leetcode922 sort array by parity II
drools决策表的简单使用
[I'm a mound pytorch tutorial] learning notes
使用Sqoop把ADS层数据导出到MySQL
倍增 LCA(最近公共祖先)
LeetCode—剑指 Offer 37、38
Docker compose configuration mysql, redis, mongodb
Openssh remote enumeration username vulnerability (cve-2018-15473)
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Simple use of drools decision table
Jenkins user rights management
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Go学习笔记—基于Go的进程间通信
子线程获取Request
CONDA common command summary
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Brush questions --- binary tree --2