状态:若为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); } }

最新文章

  1. Critical: Update Your Windows Secure Channel (cve-2014-6321,MS14-066)
  2. 【Android Studio】android Internal HTTP server disabled 解决
  3. 在CentOS 6.7中安装 latex2html, 实现 latex 自动转化为 word
  4. css3 background
  5. Box2D淌坑日记: 如何正确的设置角度
  6. C语言位操作(转)
  7. html插入视频
  8. 利用kvc对UITabBar上的UITabBarButton的尝试修改.md
  9. Indri中的动态文档索引技术
  10. 定时显示提示控件 TToolTip
  11. 新浪微博授权时出现&quot;关注 *** 的微博&quot;
  12. Yii中常用路径(转)
  13. CentOS: make menuconfig error: curses.h: No such file or directory
  14. php 租房子(练习题)
  15. 201521123009 《Java程序设计》第13周学习总结
  16. 8.nginx防DDOS
  17. Java面试题之对static的理解
  18. springMVC设计模式和javaWeb三层框架
  19. Java charAt() 方法
  20. [过程记录]Centos7 下 Hadoop分布式集群搭建

热门文章

  1. MyTools
  2. jquery 事件:2
  3. EditPlus设置html和js文件的注释快捷键
  4. Jquery判断其中任意一个文本框的值是否被修改
  5. Ioc(控制反转)、DI(依赖注入)
  6. .net中数据缓存使用
  7. win10 环境下 MinGW-w64安装
  8. jQuery 实现观察者模式
  9. iOS_16_控制器切换_modal_storyboard拖线的方式
  10. mysql之replicate_do_table/replicate_ingore_table/replicate_wide_do_table/replicate_wide_ingore_table