PTA 拯救007(bfs)"/>
PTA 拯救007(bfs)
标题
在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)
设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要告诉他是否有可能逃出生天。
输入格式:
首先第一行给出两个正整数:鳄鱼数量 N(≤100)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 (x,y) 坐标。注意:不会有两条鳄鱼待在同一个点上。
输出格式:
如果007有可能逃脱,就在一行中输出"Yes",否则输出"No"。
- 输入样例 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
- 输出样例 1:
Yes
- 输入样例 2:
4 13
-12 12
12 12
-12 -12
12 -12
- 输出样例 2:
No
题解
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;/*** @author akuya* @create 2023-11-02-17:08*/
public class Main {static boolean flag;static int pond[][]=new int[105][105];static int n;static int d;static boolean mry[][]=new boolean[105][105];static Queue<crocodile> queue=new LinkedList<>();public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String nd[]=br.readLine().split(" ");n=Integer.parseInt(nd[0]);d=Integer.parseInt(nd[1]);while(n--!=0){String xy[]=br.readLine().split(" ");int x=Integer.parseInt(xy[0])+50;int y=Integer.parseInt(xy[1])+50;pond[x][y]=1;double dis=Math.sqrt((x-50)*(x-50)+(y-50)*(y-50));if(dis<=15+d){queue.add(new crocodile(x,y));mry[x][y]=true;}}while(!queue.isEmpty()){crocodile tc=queue.poll();if(tc.getX()+d>=100||tc.getX()-d<=0||tc.getY()+d>=100||tc.getY()-d<=0){flag=true;break;}int lx=tc.getX()-d;int rx=tc.getX()+d;int uy=tc.getY()+d;int dy=tc.getY()-d;if(lx<0){lx=0;}if(rx>100){rx=100;}if(uy>100){uy=100;}if(dy<0){dy=0;}for(int i=lx;i<=rx;i++){for(int j=dy;j<=uy;j++){if(pond[i][j]==1&&!mry[i][j]){queue.add(new crocodile(i,j));mry[i][j]=true;}}}}System.out.println(flag?"Yes":"No");}
}class crocodile{private int x;public crocodile(int x, int y) {this.x = x;this.y = y;}private int y;public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}
}
思路
根据题目要求,如果在当前位置不能逃脱,就需要寻找更接近的落脚点,那么bfs就满足这一点要求,查看数据要求,时间复杂度也同样满足。则提前存储好岛的位置并进行广搜即可。
更多推荐
PTA 拯救007(bfs)
发布评论