首页 > 其他 > 详细

HDU1728

时间:2020-04-18 00:25:38      阅读:67      评论:0      收藏:0      [点我收藏+]

题解:

/*
bfs:
     WA了n次,主要是考虑策略时想法错了。
     其实每到一个点时就应该把改点以及该方向的点全都遍历一次,这样才能保证
    到达改点时是最优策略,其次,因为交叉关系,vis[][]应该放在while判断里面。
*/
import
java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static int row,column; public static int x2,y2; public static String[] map; public static boolean[][] mark; public static Queue<Block> queue; public static int[][] fx={{0,1},{1,0},{-1,0},{0,-1}}; public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int x1,y1; row = sc.nextInt(); column = sc.nextInt(); map = new String[row]; mark = new boolean[row][column]; queue=new LinkedList<>(); for (int i = 0; i < row; i++) { map[i]=sc.next(); } int time=sc.nextInt(); y1=sc.nextInt()-1; x1=sc.nextInt()-1; y2=sc.nextInt()-1; x2=sc.nextInt()-1; queue.offer(new Block(x1,y1,time+1)); mark[x1][y1]=true; if(bfs()){ System.out.println("yes"); }else{ System.out.println("no"); } } } private static boolean bfs() { while(!queue.isEmpty()){ Block b = queue.poll(); if(b.i==x2&&b.j==y2){ return true; } if(b.step==0){ continue; } b.step--; for (int i = 0; i < 4; i++) { int xx=b.i+fx[i][0]; int yy=b.j+fx[i][1]; while(check(xx,yy)){ if(!mark[xx][yy]){ queue.offer(new Block(xx,yy,b.step)); mark[xx][yy]=true; } xx+=fx[i][0]; yy+=fx[i][1]; } } } return false; } public static boolean check(int x,int y){ if(x>=0&&x<row&&y>=0&&y<column&&map[x].charAt(y)!=‘*‘) return true; return false; } private static class Block{ int i; int j; int step; public Block(int i, int j, int step) { this.i = i; this.j = j; this.step = step; } } }

 

HDU1728

原文:https://www.cnblogs.com/lijiahui-123/p/12723491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!