hdu1010 Tempter of the Bone(dfs) java
2017-10-29 21:24
387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
* 题意:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,
问:在指定的时间,是否能到达'门'的位置.
*/
* 题意:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,
问:在指定的时间,是否能到达'门'的位置.
*/
package dfs; import java.util.Scanner; public class hdu1010 { static int d[][] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; static int MAX_N=10; static char map[][]=new char[MAX_N][MAX_N]; static boolean vis[][]=new boolean[MAX_N][MAX_N]; static int n,m,t,dx,dy; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); while(scan.hasNext()){ n=scan.nextInt(); m=scan.nextInt(); t=scan.nextInt(); if(n+m+t==0){ break; } scan.nextLine(); int x=0,y=0; for(int i=0;i<n;i++){ String str=scan.nextLine(); char a[]=str.toCharArray(); for(int j=0;j<m;j++){ map[i][j]=a[j]; if(map[i][j]=='s'){ x=i;y=j; map[i][j]='.'; } if(map[i][j]=='D'){ dx=i; dy=j; } } } int step=0; init(); int res=dfs(x,y,step); if(res==0){ System.out.println("NO"); }else{ System.out.println("YES"); } } } private static int dfs(int x, int y, int step) { // TODO Auto-generated method stub if(step>t){ return 0; } if(map[x][y]=='D'){ return step; } for(int i=0;i<4;i++){ int sx=x+d[i][0]; int sy=y+d[i][1]; if(ok(sx,sy)&&!vis[sx][sy]&&map[sx][sy]!='X'){ vis[sx][sy]=true; dfs(sx,sy,step+1); vis[sx][sy]=false; } } return 0; } private static boolean ok(int sx, int sy) { // TODO Auto-generated method stub if(sx<0||sx>=n||sy<0||sy>=m){ return false; } return true; } private static void init() { // TODO Auto-generated method stub for(int i=0;i<MAX_N;i++){ for(int j=0;j<MAX_N;j++){ vis[i][j]=false; } } } }
相关文章推荐
- hdu1010 Tempter of the Bone(dfs)
- HDU1010:Tempter of the Bone(DFS)
- 【搜索-DFS】hdu1010 Tempter of the Bone
- 【hdu1010】Tempter of the Bone——dfs
- hdu1010 Tempter of the Bone(DFS)
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- HDU1010:Tempter of the Bone(DFS)
- HDU1010 Tempter of the Bone (DFS & 奇偶剪枝)
- HDU1010 Tempter of the Bone DFS
- hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- Tempter of the Bone(hdu1010,dfs)
- hdu1010 Tempter of the Bone (DFS)
- DFS+剪枝 hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone(dfs)
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- HDU1010 Tempter of the Bone (DFS)
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- HDU1010-Tempter of the Bone(DFS+各种剪枝)
- HDU1010 Tempter of the Bone (DFS入门)