本文主要是【算法】——蓝桥杯练习题(十二)的文章,如果有什么需要改进的地方还请大佬指出??
🎬作者简介:大家好,我是听风与他🥇
??博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static class node{
int x;
int y;
String path;
public node(int x,int y,String path) {
this.x = x;
this.y = y;
this.path = path;
}
}
static int n=30;
static int m=50;
static char[][] a=new char[n][m];
static char[] direct = {'D','L','R','U'};
static int dir[][] = {{1,0},{0,-1},{0,1},{-1,0}};
static boolean visit[][]=new boolean[n][m];
static String ans;
public static void main(String[] args) {
// TODO Auto-generated method stub
read();
bfs();
}
public static void bfs() {
LinkedList<node> q = new LinkedList<>();
q.add(new node(0, 0, ""));//把起点放入队列
visit[0][0]=true;
String shunxv="";//记录最短路径
while(!q.isEmpty()) {
node t = q.poll();
int x = t.x;
int y = t.y;
String str1 = t.path;
//判断是否走到终点
if (x==n-1&&y==m-1) {
shunxv=str1;
break;
}
for(int i=0;i<4;i++) {
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(x1>=0&&x1<=n-1&&y1>=0&&y1<=m-1&&!visit[x1][y1]&&a[x1][y1]=='0') {
q.add(new node(x1, y1, str1+direct[i]));
visit[x1][y1]=true;
}
}
}
System.out.println(shunxv);
}
public static void read() {
Scanner sc = new Scanner(System.in);
for(int i=0;i<n;i++) {
String s = sc.nextLine();
for(int j=0;j<m;j++) {
a[i][j]=s.charAt(j);
}
}
sc.close();
}
}
package 蓝桥杯第十二次;
import java.util.Scanner;
public class 全球变暖 {
static boolean visit[][];
static char a[][];
static int n;
static boolean flag;
static int[][] dir= {{1,0},{0,1},{-1,0},{0,-1}};
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = Integer.valueOf(sc.nextLine());
a = new char[n][n];
int ans=0;
visit = new boolean[n][n];
for(int i=0;i<n;i++) {
String s = sc.nextLine();
for(int j=0;j<n;j++) {
a[i][j]=s.charAt(j);
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(a[i][j]=='#'&&!visit[i][j]) {
flag = false;
dfs(i, j);
if (flag==false) {
ans++;
}
}
}
}
System.out.println(ans);
}
public static void dfs(int x,int y) {
visit[x][y]=true;
if(a[x+1][y]=='#'&&a[x][y+1]=='#'&&a[x-1][y]=='#'&&a[x][y-1]=='#') {
flag=true;
}
for(int i=0;i<4;i++) {
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(a[x1][y1]=='#'&&!visit[x1][y1]) {
dfs(x1, y1);
}
}
}
}
package 蓝桥杯第十二次;
import java.util.Scanner;
public class 岛屿数量 {
/*
11110
11010
11000
00000
1
11000
11000
00000
00011
3
*/
static int n=4;
static int m=5;
static boolean visit[][]=new boolean[n][m];
static char a[][] = new char[4][5];
static int dir[][]= {{1,0},{0,1},{-1,0},{0,-1}};
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int ans = 0;
for(int i=0;i<n;i++) {
String s=sc.nextLine();
for(int j=0;j<m;j++) {
a[i][j]=s.charAt(j);
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(a[i][j]=='1'&&!visit[i][j]) {
dfs(i, j);
ans++;
}
}
}
System.out.println(ans);
}
public static void dfs(int x,int y) {
visit[x][y]=true;
if(x<0||y<0||x>=n||y>=m||a[x][y]=='0') {
return;
}
for(int i=0;i<4;i++) {
int x1 = x+dir[i][0];
int y1 = y+dir[i][1];
if (x1>=0&&y1>=0&&x1<n&&y1<m&&a[x1][y1]=='1'&&!visit[x1][y1]) {
dfs(x1, y1);
}
}
}
}