dfs-求连通块
2024-08-27 22:23:07
状态:若为W则继续搜索
import java.util.Scanner; public class Main {
static int n,m;
static char[][] field; static void dfs(int row, int col){
field[row][col] = '@';
for(int dx = -1; dx <= 1; dx++){
for(int dy = -1; dy <= 1; dy++){
int newRow = row + dx;
int newCol = col + dy;
if(newRow >= 0 && newRow < n && newCol >=0 && newCol < m && field[newRow][newCol]=='W'){
dfs(newRow,newCol);
}
}
} } public static void main(String[] args) { Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
field = new char[n][m]; for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)
field[i][j] = in.next().charAt(0);
} int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){ if(field[i][j]=='W'){
dfs(i,j);
ans++;
} }
} System.out.println(ans); } }
最新文章
- Critical: Update Your Windows Secure Channel (cve-2014-6321,MS14-066)
- 【Android Studio】android Internal HTTP server disabled 解决
- 在CentOS 6.7中安装 latex2html, 实现 latex 自动转化为 word
- css3 background
- Box2D淌坑日记: 如何正确的设置角度
- C语言位操作(转)
- html插入视频
- 利用kvc对UITabBar上的UITabBarButton的尝试修改.md
- Indri中的动态文档索引技术
- 定时显示提示控件 TToolTip
- 新浪微博授权时出现";关注 *** 的微博";
- Yii中常用路径(转)
- CentOS: make menuconfig error: curses.h: No such file or directory
- php 租房子(练习题)
- 201521123009 《Java程序设计》第13周学习总结
- 8.nginx防DDOS
- Java面试题之对static的理解
- springMVC设计模式和javaWeb三层框架
- Java charAt() 方法
- [过程记录]Centos7 下 Hadoop分布式集群搭建
热门文章
- MyTools
- jquery 事件:2
- EditPlus设置html和js文件的注释快捷键
- Jquery判断其中任意一个文本框的值是否被修改
- Ioc(控制反转)、DI(依赖注入)
- .net中数据缓存使用
- win10 环境下 MinGW-w64安装
- jQuery 实现观察者模式
- iOS_16_控制器切换_modal_storyboard拖线的方式
- mysql之replicate_do_table/replicate_ingore_table/replicate_wide_do_table/replicate_wide_ingore_table