双指针与BFS

双指针与BFS

在这里插入图片描述

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.Comparator;

public class Main {
	
	static int[] cnt = new int[100000 + 5];
	static boolean[] st = new boolean[100000 + 5];
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int d = in.nextInt();
		int k = in.nextInt();
		int x, y;
		ArrayList<Node> ls = new ArrayList<Node>();
		for (int i = 0; i < n; i ++) {
			x = in.nextInt();
			y = in.nextInt();
			ls.add(new Node(x, y));
		}
		Collections.sort(ls, new cmp());
		for (int i = 0, j = 0; i < n; i ++) {
			int id = ls.get(i).y;
			cnt[id] ++;
			while (ls.get(i).x - ls.get(j).x >= d) {
				cnt[ls.get(j).y] --;
				j ++;
			}
			if (cnt[id] >= k) st[id] = true;
		}
		for (int i = 0; i <= 100000; i ++) {
			if (st[i]) System.out.println(i);
		}
	}
	
	static class cmp implements Comparator<Node> {
		public int compare(Node a, Node b) {
			if (a.x < b.x) return -1;
			else if (a.x > b.x) return 1;
			else return 0;
		}
	}
	
	static class Node {
		int x, y;
		public Node(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

}

在这里插入图片描述

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		while (T -- > 0) {
			n = in.nextInt();
			m = in.nextInt();
			in.nextLine();
			maze = new char[n + 5][m + 5];
			for (int i = 0; i < n; i ++) {
				maze[i] = in.nextLine().toCharArray();
			}
			for (int i = 0; i < n; i ++) {
				for (int j = 0; j < m; j ++) {
					if (maze[i][j] == 'S') {
						sx = i;
						sy = j;
					}
					if (maze[i][j] == 'E') {
						ex = i;
						ey = j;
					}
				}
			}
			int ans = bfs();
			if (ans == -1) System.out.println("oop!");
			else System.out.println(ans);
		}
		
	}
	
	static int n, m;
	static int sx, sy, ex, ey;
	static char[][] maze;
	static boolean[][] vis;
	static int[] dx = new int[] {-1, 0, 1, 0};
	static int[] dy = new int[] {0, -1, 0, 1};
	
	static int bfs() {
		Queue<Node> q = new LinkedList<Node>();
		vis = new boolean[n + 5][m + 5];
		vis[sx][sy] = true;
		q.offer(new Node(sx, sy, 0));
		while (!q.isEmpty()) {
			Node now = q.poll();
			if (now.x == ex && now.y == ey) return now.step;
			for (int i = 0; i < 4; i ++) {
				int tx = now.x + dx[i];
				int ty = now.y + dy[i];
				if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
				if (maze[tx][ty] == '#') continue;
				if (vis[tx][ty]) continue;
				if (!vis[tx][ty]) {
					vis[tx][ty] = true;
					q.offer(new Node(tx, ty, now.step + 1));
				}
			}
		}
		return -1;
	}
	
	static class Node {
		int x, y, step;
		public Node(int x, int y, int step) {
			this.x = x;
			this.y = y;
			this.step = step;
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值