算法:
简单的一个dfs就能过
ac:
#include "iostream"
#include "algorithm"
#include "cstring"
#include "queue"
using std::cin;
using std::cout;
using std::endl;
#define N 110
char a[N][N];
int abook[N][N];
int n, m;
int next[8][2] = { {1,0},{-1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,1},{1,-1} };
void dfs(int x, int y) {
abook[x][y] = 1;
for (int i = 0; i <= 7; i++) {
int x1 = x + next[i][0];
int y1 = y + next[i][1];
if (x1<1 || x1>n || y1<1 || y1>m || abook[x1][y1]||a[x1][y1]=='.')continue;
dfs(x1, y1);
}
}
int main() {
int ans=0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cin >> a[i][j];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 'W' && abook[i][j] == 0) {
ans++;
dfs(i, j);
}
}
cout << ans;
}
java:
数组:
package com.itheima.demo1;
import java.util.Scanner;
public class demo6 {
public static void main(String[] args) {
int a[] = {1, 2, 3};
int[] b = new int[]{1, 3, 4};
Scanner sc = new Scanner(System.in);
for (int i = 3; i < 10; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < 10; i++) {
System.out.println(a[i]);
}
//x.fori:创建for循环0-4
//数组长度:数组名.length
//可以:数组名.fori,直接生成
//int a[]=new int[]{12,23,34,1,2,4,5};
//数组动态初始化:
//数据类型 []数组名=new 数据类型 [数组长度]
//例如: int []arr=new int[3];
}
}
package com.itheima.demo1;
import java.util.Scanner;
public class demo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//x.fori:创建for循环0-4
//数组长度:数组名.length
//可以:数组名.fori,直接生成
//int a[]=new int[]{12,23,34,1,2,4,5};
//数组动态初始化:
//数据类型 []数组名=new 数据类型 [数组长度]
//例如: int []arr=new int[3];
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
package com.itheima.demo1;
import java.util.Random;
import java.util.Scanner;
public class demo8 {
public static void main(String[] args) {
//生成随机数:类Random
Scanner sc = new Scanner(System.in);
Random r = new Random();
//生成随机数:int number = r.nextInt(随机数的范围);
int number = r.nextInt(100); //0-99
System.out.println(number);
for (int i = 0; i < 10; i++) {
number = r.nextInt(9)+7; //7-15
System.out.print(number+" ");
//重点:把ln删掉就不会换行了
}
System.out.println("\n");
//foreach循环:自动迭代数组的每个元素
//格式: for(type 形参:实参),type:数据类型
// {
// System.out.println(形参);
// }
int []book=new int[]{1,2,3,4,5};
for(int b:book){
System.out.println(b);
}
}
}