当前位置:网站首页>DFS and BFS to solve Treasure Island exploration
DFS and BFS to solve Treasure Island exploration
2022-06-13 01:44:00 【halisi7】
bfs Treasure Island exploration
Problem description

After figuring out the problem . You will find that it is actually from (6,8) Start breadth first search . Each time, it needs to expand up, down, left and right , When the extended point is greater than 0 Join the queue when , Until the queue is extended . The total number of points added to the queue is the area of the island . Assume that the size of the map does not exceed 50*50.
The code implementation is as follows :
#include <stdio.h>
struct note {
int x;
int y;
};
int main() {
int i, j, n, m, startx, starty,head,tail,tx,ty,k,sum=1;
int a[51][51];// Map
int book[51][51] = {
0};// Backup map for marking
struct note que[2501];// most 50*50 A little bit
int next[4][2] = {
{
1,0},{
0,1},{
-1,0},{
0,-1} };// Four directions
printf(" Please enter the map with several rows and columns you want to enter :");
scanf_s("%d %d", &n, &m);
printf(" Please enter a map :\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf_s("%d", &a[i][j]);
printf(" Please enter the initial position :");
scanf_s("%d %d", &startx, &starty);
// Initial queue
head = 1;
tail = 1;
// Assignment initial position
que[tail].x = startx;
que[tail].y = starty;
// Mark the initial position
book[startx][starty] = 1;
// A party ++
tail++;
// When the queue is not empty
while (head < tail) {
// Enumerate four directions
for (k = 0; k <= 3; k++) {
tx = que[head].x + next[k][0];
ty = que[head].y + next[k][1];
// Judge whether you crossed the line
if (tx<1 || tx>n || ty<1 || ty>m)
continue;
// Joining operation
if (a[tx][ty] > 0 && book[tx][ty] == 0) {
book[tx][ty] = 1;// Mark this point
sum++;// Increase in area
que[tail].x = tx;
que[tail].y = ty;
tail++;
}
}
head++;
}
printf(" The size of the island is :%d", sum);
getchar(); getchar();
return 0;
}
Input
Please enter the map with several rows and columns you want to enter :10 10
Please enter a map :
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
Please enter the initial position :6 8
Output
The size of the island is :38
dfs Treasure Island exploration
Problem description

After figuring out the problem . You will find that it is actually from (6,8) Start breadth first search . Each time, it needs to expand up, down, left and right , When the extended point is greater than 0 Join the queue when , Until the queue is extended . The total number of points added to the queue is the area of the island . Assume that the size of the map does not exceed 50*50.
The code implementation is as follows :
#include <stdio.h>
int book[51][51];
int a[51][51];
int n, m ,sum=1;
void dfs(int x,int y) {
int next[4][2] = {
{
1,0},{
0,-1},{
-1,0},{
0,1} };
int tx,k, ty;
for (k = 0; k <= 3; k++) {
tx = x + next[k][0];
ty = y + next[k][1];
if (tx<1 || tx>n || ty<1 || ty>m)
continue;
if (book[tx][ty] == 0 && a[tx][ty] >= 1) {
book[tx][ty] = 1;
sum++;
dfs(tx, ty);
}
}
return;
}
int main() {
int i, j, startx, starty;
printf(" Please enter the map with several rows and columns you want to enter :");
scanf_s("%d %d", &n, &m);
printf(" Please enter a map :\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf_s("%d", &a[i][j]);
printf(" Please enter your starting position :");
scanf_s("%d %d", &startx, &starty);
book[startx][starty] = 1;
dfs(startx, starty);
printf(" The area of the island is :%d", sum);
getchar(); getchar();
return 0;
}
Input
Please enter the map with several rows and columns you want to enter :10 10
Please enter a map :
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
Please enter the initial position :6 8
Output
The area of the island is :38
The number of small islands
Problem description

Code implementation
#include <stdio.h>
int book[51][51];
int a[51][51];
int n, m, sum = 1 , num;
void dfs(int x, int y,int num) {
int next[4][2] = {
{
1,0},{
0,-1},{
-1,0},{
0,1} };
int tx, k, ty;
for (k = 0; k <= 3; k++) {
tx = x + next[k][0];
ty = y + next[k][1];
if (tx<1 || tx>n || ty<1 || ty>m)
continue;
if (book[tx][ty] == 0 && a[tx][ty] >= 1) {
book[tx][ty] = 1;
a[tx][ty] = num;
//sum++;
dfs(tx, ty,num);
}
}
return;
}
int main() {
int i, j, startx, starty;
printf(" Please enter the map with several rows and columns you want to enter :");
scanf_s("%d %d", &n, &m);
printf(" Please enter a map :\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf_s("%d", &a[i][j]);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
if (a[i][j] > 0) {
num--;
a[i][j] = num;
dfs(i, j, num);
}
}
printf(" The dyed island map is :\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
printf("%3d", a[i][j]);
}
printf("\n");
}
printf(" The number of islands is :%d", -num);
getchar(); getchar();
return 0;
}
Input
Please enter the map with several rows and columns you want to enter :10 10
Please enter a map :
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
Output
The dyed island map is :
-1 -1 -1 0 0 0 0 0 -2 -2
-1 0 -1 0 -3 -3 -3 0 -2 -2
-1 0 -1 0 -3 -3 -3 -3 0 -2
-1 -1 0 0 0 -3 -3 -3 0 0
0 0 0 0 0 0 -3 -3 -3 0
0 -3 -3 -3 0 -3 -3 -3 -3 0
0 -3 -3 -3 -3 -3 -3 -3 -3 0
0 0 -3 -3 -3 -3 -3 -3 0 0
0 0 0 -3 -3 -3 -3 0 -4 -4
0 0 0 0 0 0 0 0 -4 0
The number of islands is :4
summary

边栏推荐
- STM32 3*3矩阵按键(寄存器版本)
- What is solid angle
- Stack and queue practice (C language): Demon King's language
- 六、出库管理功能的实现
- Torch. Distributions. Normal
- Tweets movement description and chart display
- [official document summary] writing standards for academic dissertations of National University of science and technology
- 白噪声的详细理解
- Copy (copy) constructors and assignment overloaded operators=
- Mysql database listening -canal
猜你喜欢

四、入库管理功能的完善

关于tkinter.Canvas 不显示图片的问题

MySQL related summary

Run Presto under docker to access redis and Bi presentation

如何通过受众群体定位解决实际问题?

谷歌加大型文字广告是什么?怎么用?

Quickly set the computer to turn off automatically

What is the path field—— Competitive advertising

What is solid angle

What is Google plus large text ads? How to use it?
随机推荐
How to solve the problems when using TV focusable to package APK in uni app
6、 Implementation of warehouse out management function
谷歌的受众群体是如何发挥作用的?
[learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
The interviewer only asked me five questions and the interview was over
Detailed understanding of white noise
Service creation and operation example of ROS
四、入库管理功能的完善
On February 26, 2022, the latest news of national oil price adjustment today
How many times does the constructor execute?
详细受众特征详细解释
#pragma comment(lib,“urlmon.lib“)
Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
Wsl2 + vcxsrv + opengl3.3 configuration
What is Google plus large text ads? How to use it?
Delphi Google API text to speech MP3 file
FSOs forest simulation optimization model learning notes
三、上传织物图片至SQL Server并提供name进行展示织物照片
Devaxpress Chinese description --tcximagelist (enhanced image list control)
路径字段是什么? ——竞价广告