当前位置:网站首页>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

边栏推荐
- [从零开始学习FPGA编程-22]:进阶篇 - 架构 - FPGA内部硬件电路的设计与建模
- Delphi 10.4.2 release instructions and installation methods of three patches
- Workspace for ROS
- Super complete regular expressions
- Leetcode question 20
- [WSL2]限制WSL2可访问的硬件资源(CPU/内存)
- MySQL performance optimization
- [从零开始学习FPGA编程-21]:进阶篇 - 架构 - VerilogHDL编码规范
- Page optimization - Notes
- Plumber game
猜你喜欢

Startup, connection and stop of MySQL service

Quickly set the computer to turn off automatically

Should the audience choose observation mode or positioning mode?

Jeux de plombiers

4、 Improvement of warehousing management function

A DPU architecture without CPU: Hyperion

pringboot之restfull接口规范注解(二)

MySQL connection query

Devexpress implementation flow chart

TensorFlow2的Conv1D, Conv2D,Conv3D机器对应的MaxPooling详解
随机推荐
项目实训(十七)---个人工作总结
leetcode743. Network latency (medium, Dijkstra)
URI, URL and urn difference, relation and syntax diagram
Delphi Google API text to speech MP3 file
Introduction to common ROS commands
深度学习调参技巧详解
Devexpress implementation flow chart
路径字段是什么? ——竞价广告
Getting started with phaser 3
[wsl2]wsl2 migrate virtual disk file ext4 vhdx
Machine learning basic SVM (support vector machine)
Temporary objects and compilation optimization
TensorFlow 2. X multi graphics card distributed training
Detailed explanation of deep learning parameter adjustment skills
Golang context (context summary)
3、 Upload fabric photos to SQL server and provide name to display fabric photos
Installing pytorch geometric
Logical operation bit operation
Record the VMware installation process of VMware Tools and some problems encountered
谷歌加大型文字广告是什么?怎么用?