当前位置:网站首页>All in one 1329 cells (breadth first search)
All in one 1329 cells (breadth first search)
2022-07-27 08:14:00 【Bamboo monk-】
Original link
http://ybt.ssoier.cn:8088/problem_show.php?pid=1329
【 Title Description 】
A rectangular array consists of numbers 0 To 9 form , Numbers 1 To 9 For cells , A cell is defined as the same cell along the cell number, up and down, left and right, or cell number , Find the number of cells in a given rectangular array . Such as :
array :
4 10 0234500067 1034560500 2045600671 0000000089
Yes 4 Cells .
【 Input 】
The first row is the row of the matrix n And column m;
Here's a n×m Matrix .
【 Output 】
Number of cells .
【 Enter a demo 】
4 10 0234500067 1034560500 2045600671 0000000089
【 Output demonstration 】
4
Examine the subject
Adjacent non-zero numbers are one cell . Count the number of cells
step
1. Defining variables
#include<iostream>
#include<queue>
using namespace std;
int a[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};
int n,m,res=0;
char s[1000][1000];
int mk[1000][1000];
struct node{ // Structure
int x;
int y;
};2. Define search function
void bfs(int x,int y)
{
queue<node>q;
q.push((node){x,y});
while(q.size())
{
node t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=t.x+a[i][0];
int yy=t.y+a[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&s[xx][yy]!='0'&& mk[xx][yy]==0)
{
mk[xx][yy]=1;
q.push((node){xx,yy});
} }
}
return;
}The main function :
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>s[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]!='0' && mk[i][j]==0)
{
res++;
mk[i][j]=1;
bfs(i,j);
}
}
}
cout<<res<<endl;
return 0;
}Complete code
#include<iostream>
#include<queue>
using namespace std;
int a[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};
int n,m,res=0;
char s[1000][1000];
int mk[1000][1000];
struct node{
int x;
int y;
};
void bfs(int x,int y)
{
queue<node>q;
q.push((node){x,y});
while(q.size())
{
node t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=t.x+a[i][0];
int yy=t.y+a[i][1];
if(xx>=1 && xx<=n && yy>=1&& yy<=m && s[xx][yy]!='0'&& mk[xx][yy]==0)
{
mk[xx][yy]=1;
q.push((node){xx,yy});
} }
}
return;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>s[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]!='0' && mk[i][j]==0)
{
res++;
mk[i][j]=1;
bfs(i,j);
}
}
}
cout<<res<<endl;
return 0;
}边栏推荐
- 代码接口自动化的有点
- ERP production operation control Huaxia
- Combined use of C WinForm form form event and delegate
- 2020 International Machine Translation Competition: Volcano translation won five championships
- idea远程调试
- You may need an additional loader to handle the result of these loaders.
- pytorch_demo1
- JS access cookie example
- Shell Scripts相关
- Solid smart contract development - 3.3-solid syntax control structure
猜你喜欢

"PHP Basics" tags in PHP
Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market

SSTI template injection
Development of three database general SQL code based on PG Oracle and MySQL

如何更新pip3?和Running pip as the ‘root‘ user can result in broken permissions and conflicting behaviour

"PHP Basics" uses echo statements to output information

QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform

Vcenter7.0 managing esxi7.0 hosts

Attack and defense World Lottery

如何在 60 秒内去分析和定位问题?
随机推荐
Plato farm is expected to further expand its ecosystem through elephant swap
物来顺应,未来不迎,当时不杂,既过不恋
Prevent cookies from modifying ID to cheat login
Lua迭代器
Containerd failed to pull private database image (kubelet)
代码接口自动化的有点
Notes in "PHP Basics" PHP
Design and development of GUI programming for fixed-point one click query
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
Local Oracle reported ora-12514: tns: the listener cannot recognize the requested service at present
[target detection] yolov6 theoretical interpretation + practical test visdrone data set
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
我用字符画出了一个谷爱凌!
What is a rebound shell? What's the use of bouncing shells?
2022/7/26 exam summary
[netding cup 2020 rosefinch group]nmap 1 two solutions
CommonTitleBar hide left right
How to play with the purchase of SAP variant materials? Look at this article and you will understand
软件调优方法有哪些?看看飞腾技术专家怎么说 | 龙蜥技术
I can't figure out why MySQL uses b+ trees for indexing?