洛谷官方精选题单 | Java | 搜索

P1219 八皇后

import java.util.Scanner;

public class P1219 {
    public static int n, count;
    public static int[] line, row, left, right;
    public static boolean check(int x, int y) {
        return line[y] == 0 && row[x] == 0 && left[x - y + n] == 0 && right[x + y] == 0;
    }
    public static void queen(int x) {
        if (x == n + 1) {
            count += 1;
            if (count <= 3) {
                for (int i = 1; i <= n; i += 1) {
                    System.out.print(row[i] + " ");
                }
                System.out.println();
            }
        }
        for (int y = 1; y <= n; y += 1) {
            if (check(x, y)) {
                line[y] = 1;
                row[x] = y;
                left[x - y + n] = 1;
                right[x + y] = 1;
                queen(x + 1);
                line[y] = 0;
                row[x] = 0;
                left[x - y + n] = 0;
                right[x + y] = 0;
            }
        }
    }
    public static void main(String[] args) {
        count = 0;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        line = new int[n + 1];
        row = new int[n + 1];
        left = new int[2 * n + 1];//x - y + n [1, 2n - 1]
        right = new int[2 * n + 1];//x + y [0, 2n]
        queen(1);
        System.out.print(count);
    }
}

P2392 考前临时抱佛脚

P1443 马的遍历

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static class Pair {
        int x;
        int y;
        public Pair(int xi, int yi) {
            x = xi;
            y = yi;
        }
    }
    private static int n, m;
    private static int[][] directions = {{1, 2}, {1, -2}, {-1, -2}, {-1, 2},
            {2, 1}, {2, -1}, {-2, -1}, {-2, 1}};
    private static Queue<Pair> queue;
    private static int[][] steps;
    private static boolean check(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= m && steps[x][y] == -1;
    }
    private static void inQueue(int x, int y, int count) {
        for (int i = 0; i < 8; i += 1) {
            int newX = x + directions[i][0], newY = y + directions[i][1];
            if (check(newX, newY)) {
                queue.offer(new Pair(newX, newY));
                steps[newX][newY] = count;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x, y;
        n = in.nextInt();m = in.nextInt();
        x = in.nextInt();y = in.nextInt();
        queue = new ArrayDeque<>();
        steps = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i += 1) {
            Arrays.fill(steps[i], -1);
        }
        steps[x][y] = 0;
        queue.offer(new Pair(x, y));
        int count = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i += 1) {
                Pair cur = queue.poll();
                inQueue(cur.x, cur.y, count);
            }
            count += 1;
        }
        for (int i = 1; i <= n; i += 1) {
            for (int j = 1; j <= m; j += 1) {
                System.out.print(steps[i][j] + " ");
            }
            System.out.println();
        }
    }
}

P1135 奇怪的电梯

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static int n, A, B, ans;
    public static int[] K;
    public static int[] visited, shortest;
    public static boolean flag;

    public static void dfs(int cur, int count) {
        if (count > ans || count > 2 * n) {
            return;
        }
        if (cur == B) {
            ans = Math.min(ans, count);
            flag = true;
            return;
        }
        if (visited[cur] == 0 && count < shortest[cur]) {
            shortest[cur] = count;
            if (cur + K[cur] <= n) {
                visited[cur] = 1;
                dfs(cur + K[cur], count + 1);
                visited[cur] = 0;
            }
            if (cur - K[cur] >= 0) {
                visited[cur] = 1;
                dfs(cur - K[cur], count + 1);
                visited[cur] = 0;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt(); A = in.nextInt(); B = in.nextInt();
        K = new int[n + 1];
        visited = new int[n + 1];
        shortest = new int[n + 1];
        for (int i = 1; i <= n; i += 1) {
            K[i] = in.nextInt();
        }
        ans = (int) Math.pow(10, 9);
        Arrays.fill(shortest, ans);
        dfs(A, 0);
        if (flag) {
            System.out.print(ans);
        } else {
            System.out.print(-1);
        }
    }
}

P2895 Meteor Shower S

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

public class Main {
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static StreamTokenizer tokenizer = new StreamTokenizer(br);

    private static int nextInt() throws Exception {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }

    public static class Pair {
        int x;
        int y;

        public Pair(int xi, int yi) {
            x = xi;
            y = yi;
        }
    }
    public static class star implements Comparable <star>{
        int x;
        int y;
        int t;
        public star(int xi, int yi, int ti) {
            x = xi;
            y = yi;
            t = ti;
        }

        @Override
        public int compareTo(star o) {
            return t - o.t;
        }
    }

    public static int m, count;
    public static int[][] farm = new int[305][305];
    public static int[][] temp = new int[305][305];
    public static int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static Queue<Pair> queue;
    public static star[] data;

    public static boolean check(int x, int y) {
        return x >= 0 && x < 305 && y >= 0 && y < 305;
    }

    public static boolean bfs() {
        int i = 0;//是遍历流星的指针
        //一次循环是一个时间单位
        while (!queue.isEmpty()) {
            //流星入坑
            while (i < m && data[i].t == count) {
                farm[data[i].x][data[i].y] = 1;
                for (int j = 0; j < 4; j += 1) {
                    int newX = data[i].x + directions[j][0];
                    int newY = data[i].y + directions[j][1];
                    if (check(newX, newY)) {
                        farm[newX][newY] = 1;
                    }
                }
                i += 1;
            }
            //出队当前所有的并入队周围的四个
            int size = queue.size();
            for (int k = 0; k < size; k += 1) {
                Pair cur = queue.poll();
                if (temp[cur.x][cur.y] == 0) {
                    return true;
                }
                for (int j = 0; j < 4; j += 1) {
                    int newX = cur.x + directions[j][0];
                    int newY = cur.y + directions[j][1];
                    if (check(newX, newY) && farm[newX][newY] == 0) {
                        farm[newX][newY] = 1;
                        queue.offer(new Pair(newX, newY));
                    }
                }
            }
            count += 1;
        }
        return false;
    }

    public static void main(String[] args) throws Exception {
        count = 1;
        m = nextInt();
        data = new star[m];
        for (int i = 0; i < m; i += 1) {
            data[i] = new star(nextInt(), nextInt(), nextInt());
            temp[data[i].x][data[i].y] = 1;
            for (int j = 0; j < 4; j += 1) {
                int newX = data[i].x + directions[j][0];
                int newY = data[i].y + directions[j][1];
                if (check(newX, newY)) {
                    temp[newX][newY] = 1;
                }
            }
        }
        Arrays.sort(data);
        queue = new ArrayDeque<>();
        queue.offer(new Pair(0, 0));
        boolean flag = bfs();
        if (flag) {
            System.out.print(count - 1);
        } else {
            System.out.print(-1);
        }
    }
}

 P1036 选数


P2036 PERKET 

import java.util.Scanner;

public class Main {
    public static int n, ans;
    public static int[] sour, bitter, visited;
    public static void dfs(int s, int b, int count) {
        if (count != 1) {
            ans = Math.min(ans, Math.abs(s - b));
        }
        if (count > n) {
            return;
        }
        for (int i = 1; i <= n; i += 1) {
            if (visited[i] == 0) {

                visited[i] = 1;
                s *= sour[i];
                b += bitter[i];

                dfs(s, b, count + 1);

                s /= sour[i];
                b -= bitter[i];
                visited[i] = 0;
            }
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        sour = new int[n + 1];
        bitter = new int[n + 1];
        visited = new int[n + 1];
        for (int i = 1; i <= n; i += 1) {
            sour[i] = in.nextInt();
            bitter[i] = in.nextInt();
        }
        ans = 0x7fffffff;
        dfs(1, 0, 1);
        System.out.print(ans);
    }
}

 P1605 迷宫


 

import java.util.Scanner;

public class Main {
    public static int n, m, t, sx, sy, fx, fy, count;
    public static int[][] maze;
    public static int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static boolean check(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= m && maze[x][y] == 0;
    }
    public static void dfs(int x, int y) {
        if (x == fx && y == fy) {
            count += 1;
            return;
        }
        for (int i = 0; i < 4; i += 1) {
            int newX = x + directions[i][0];
            int newY = y + directions[i][1];
            if (check(newX, newY)) {
                maze[newX][newY] = 1;
                dfs(newX, newY);
                maze[newX][newY] = 0;
            }
        }
    }
    public static void main(String[] args) {
        count = 0;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();  m = in.nextInt();
        t = in.nextInt();
        sx = in.nextInt();  sy = in.nextInt();
        fx = in.nextInt();  fy = in.nextInt();
        maze = new int[n + 1][m + 1];
        for (int i = 0; i < t; i += 1) {
            maze[in.nextInt()][in.nextInt()] = 1;
        }
        maze[sx][sy] = 1;
        dfs(sx, sy);
        System.out.print(count);
    }
}

P1019 单词接龙 

import java.util.Scanner;

public class Main {
    public static String[] words;
    public static StringBuilder begin;
    public static int n, ans = 0;
    public static int[] visited;
    //check if s2 can be added to the tail of s1
    //note that one cannot cover the other
    public static boolean check(StringBuilder s1, String s2, int cover) {
        int len1 = s1.length(), len2 = s2.length();
        if (cover > len1 || cover > len2) {
            return false;
        }
        for (int i = 0; i < cover; i += 1) {
            if (s1.charAt(len1 - cover + i) != s2.charAt(i)) {
                return false;
            }
        }
        return true;
    }
    public static void add(StringBuilder s1, String s2, int cover) {
        for (int i = cover; i < s2.length(); i += 1) {
            s1.append(s2.charAt(i));
        }
    }
    public static void dfs(StringBuilder begin) {
        ans = Math.max(ans, begin.length());
        //travel through the words
        for (int i = 0; i < n; i += 1) {
            if (visited[i] >= 2) {
                continue;
            }
            //iterate through the length of cover
            for (int j = 1; j < words[i].length(); j += 1) {
                StringBuilder temp = new StringBuilder(begin);
                if (check(begin, words[i], j)) {
                    add(temp, words[i], j);
                    visited[i] += 1;
                    dfs(temp);
                    visited[i] -= 1;
                }
            }
        }
    }
    public static void main(String[] args) {
        //read in
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        in.nextLine();
        words = new String[n];
        visited = new int[n];
        for (int i = 0; i < n; i += 1) {
            words[i] = new String();
            words[i] = in.nextLine();
        }
        begin = new StringBuilder(in.nextLine());
        dfs(begin);
        System.out.print(ans);

    }

}

P2404 自然数的拆分问题

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static int n;
    public static Stack<Integer> stack;
    public static void dfs(int num) {
        if (num == 0) {
            int size = stack.size();
            Stack<Integer> temp = (Stack<Integer>) stack.clone();
            int[] p = new int[size];
            for (int i = 0; i < size; i += 1) {
                p[size - 1 - i] = temp.pop();
            }
            for (int i = 0; i < size; i += 1) {
                if (i == size - 1) {
                    System.out.print(p[i]);
                } else {
                    System.out.print(p[i] + "+");
                }
            }
            System.out.println();
            return;
        }
        int start = 1;
        if (!stack.isEmpty()) {
            start = stack.peek();
        }
        for (int i = start; i <= num && i != n; i += 1) {
            stack.push(i);
            dfs (num - i);
            stack.pop();
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        stack = new Stack<>();
        dfs(n);
    }
}

P1162 填涂颜色

import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;

public class P1162 {
    public static int n;
    public static boolean[][] visited;
    public static int[][] maze;
    public static int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static StreamTokenizer t = new StreamTokenizer(br);
    public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static PrintWriter out = new PrintWriter(bw);
    public static int nextInt() throws Exception {
        t.nextToken();
        return (int) t.nval;
    }
    public static class Pair {
        int x;
        int y;
        public Pair(int xi, int yi) {
            x = xi;
            y = yi;
        }
    }
    public static boolean check(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= n && !visited[x][y] && maze[x][y] == 0;
    }
    public static void bfs (int x, int y) {
        Queue<Pair>queue = new ArrayDeque<>();
        queue.offer(new Pair(x, y));
        visited[x][y] = true;
        maze[x][y] = 2;
        while(!queue.isEmpty()) {
            for (int cnt = 0; cnt < queue.size(); cnt += 1) {
                Pair cur = queue.poll();
                for (int i = 0; i < 4; i += 1) {
                    int newX = cur.x + dir[i][0];
                    int newY = cur.y + dir[i][1];
                    if (check(newX, newY)) {
                        queue.offer(new Pair(newX, newY));
                        visited[newX][newY] = true;
                        maze[newX][newY] = 2;
                    }
                }
            }

        }
    }
    public static void main(String[] args) throws Exception {
        n = nextInt();
        visited = new boolean[n + 1][n + 1];
        maze = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i += 1) {
            for (int j = 1; j <= n; j += 1) {
                maze[i][j] = nextInt();
            }
        }
        for (int i = 1; i <= n; i += 1) {
            if (check(1, i)) {
                bfs(1, i);
            }
            if (check(n, i)) {
                bfs(n, i);
            }
            if (check(i, 1)) {
                bfs(i, 1);
            }
            if (check(i, n)) {
                bfs(i, n);
            }
        }
        for (int i = 1; i <= n; i += 1) {
            for (int j = 1; j <= n; j += 1) {
                if (maze[i][j] == 0) {
                    out.print(2 + " ");
                } else if (maze[i][j] == 2){
                    out.print(0 + " ");
                } else {
                    out.print(maze[i][j] + " ");
                }
            }
            if (i != n) {
                out.println();
            }
        }
        out.flush();
    }
}

 P1825 Corn Maze S

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static StreamTokenizer t = new StreamTokenizer(br);
    public static int nextInt() throws Exception {
        t.nextToken();
        return (int) t.nval;
    }
    public static int n, m, time;
    public static char[][] maze;
    public static boolean[][] visited;
    public static class Pair {
        int x;
        int y;
        public Pair(int xi, int yi) {
            x = xi;
            y = yi;
        }
    }
    public static Pair[][] trans = new Pair[26][2];
    public static int[] count;//总结传送门的状态
    public static Queue<Pair> queue = new ArrayDeque<>();
    public static int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static boolean check(int x, int y) {
        return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] != '#' && !visited[x][y];
    }
    public static void bfs() {
        time = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i += 1) {
                Pair cur = queue.poll();
                //special judge
                if (maze[cur.x][cur.y] == '=') {
                    return;
                }
                if (maze[cur.x][cur.y] >= 'A' && maze[cur.x][cur.y] <= 'Z') {
                    int alphabet = maze[cur.x][cur.y] - 'A';
                    visited[cur.x][cur.y] = true;
                    int x0 = trans[alphabet][0].x;  int x1 = trans[alphabet][1].x;
                    int y0 = trans[alphabet][0].y;  int y1 = trans[alphabet][1].y;
                    if (cur.x == x0 && cur.y == y0) {
                        cur.x = x1;
                        cur.y = y1;

                    } else if (cur.x == x1 && cur.y == y1) {
                        cur.x = x0;
                        cur.y = y0;
                    }
                }
                for (int j = 0; j < 4; j += 1) {
                    int newX = cur.x + directions[j][0];
                    int newY = cur.y + directions[j][1];
                    if (check(newX, newY)) {
                        visited[newX][newY] = true;
                        queue.offer(new Pair(newX, newY));
                    }
                }
            }
            time += 1;
        }
    }
    public static void main(String[] args) throws Exception {
        n = nextInt();
        m = nextInt();
        maze = new char[n][m];
        visited = new boolean[n][m];
        count = new int[26];
        for (int i = 0; i < n; i += 1) {
            String temp = br.readLine();
            for (int j = 0; j < m; j += 1) {
                maze[i][j] = temp.charAt(j);
                if (maze[i][j] == '@') {
                    queue.offer(new Pair(i, j));
                    visited[i][j] = true;
                } else if (maze[i][j] >= 'A' && maze[i][j] <= 'Z') {
                    int alphabet = maze[i][j] - 'A';
                    trans[alphabet][count[alphabet]] = new Pair(i, j);
                    count[alphabet] += 1;
                }
            }
        }
        for (int i = 0; i < 26; i += 1) {
            if(count[i] % 2 == 1) {
                int x = trans[i][0].x, y = trans[i][0].y;
                maze[x][y] = '#';
            }
        }
        bfs();
        System.out.print(time);
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值