red and black 杭电1312 java深搜
来源:互联网 发布:扫码软件 编辑:程序博客网 时间:2024/05/21 11:33
```//将'.'看做是通路 将'#'看做是墙, 按顺时针方向,利用深度优先搜索递归```import java.util.*;public class Main { private static int count = 0; private static int [] dx = {0,1,0,-1} ; //顺时针移动 private static int [] dy = {1,0,-1,0} ; private static int w ; private static int h ; private static char[][] array = new char[21][21] ; public static void main(String [] args){ Scanner input = new Scanner(System.in); while(input.hasNext()){ h = input.nextInt() ; // 读入列数 w = input.nextInt() ; //读入行数 String s = input.nextLine() ; // 读取第一行剩下的空格 if(h == 0 || w == 0){ break ; } else{ int x = 0, y =0 ; for(int i = 0 ; i < w ; i++){ s = input.nextLine() ; int index ; if((index = s.indexOf("@")) >= 0){ x = i ; y = index ; } array[i] = s.toCharArray() ; } dfs(array,x,y) ; System.out.println(count); count = 0 ; } } } public static void dfs(char [][] array , int x , int y){ if(x >= w || y >= h || x < 0 || y < 0){//越界,回溯 return ; }else if(array[x][y] == '#' ){ // 遇到墙 ,回溯 return ; }else if(array[x][y] == '.'|| array[x][y] =='@'){ count++; array[x][y]='#' ; for(int i = 0 ; i < 4 ; i++){ x += dx[i] ; y += dy[i] ; dfs(array,x,y); x -= dx[i] ; y -= dy[i] ; } } }}
0 0
- red and black 杭电1312 java深搜
- 杭电 hdu 1312 Red and Black
- 杭电 1312 Red and Black
- 杭电 1312 red and black
- 杭电1312 red and black
- 杭电1312 red and black
- hdu 杭电 1312 Red and Black
- 【杭电】[1312]Red and Black
- Red and Black【杭电1312】
- 杭电 1312 Red and black 搜索题
- 杭电1312 Red and Black(搜索入门)
- 杭电1312 Red and Black【递归搜索】
- 杭电 1312 Red and Black 递归 附翻译
- 杭电(hdu)ACM 1312 Red and Black
- 杭电-1312 Red and Black(DFS)
- 杭电 1312 Red and Black BFS 搜索
- 杭电Red and Black。。。。水题
- Red and Black(杭电oj1312)
- 欢迎使用CSDN-markdown编辑器
- java中Random类和Math类中的random方法的联系和区别
- 数据库事务笔记
- h264_aac合成flv的个人经验之谈
- vbscript实现 为文件夹创建快捷方式 lnk
- red and black 杭电1312 java深搜
- Python环境变量配置(win8)
- Java反射机制的简介
- << 电子电路 >>电子电路 Mysql
- quartz的配置文件job.xml自动提示
- OSI七层理解
- java多线程典型例子及分析
- Oracle Database 11g Express Edition 如何创建数据库
- Git Rebase教程: 用Git Rebase让时光倒流