1.24

P4961 小埋与扫雷 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • 本题#17报RE错,是因为栈溢出,查询了资料之后发现,java主线程分配的栈块太小,递归深度太大就会发生这种问题,我们写爆搜不可避免要在Tle和爆栈边缘徘徊,这也是和c++相比一个比较弱势的点,那么有没有方法解决这一弱点呢?朋友别慌,有的,都有的
  • 笔者查了下资料,发现我们可以直接开一个子线程,然后自己设置栈的空间大小,这一问题就解决啦
  • 上代码
package shuati;

import java.io.*;
import java.util.*;

public class Main implements Runnable{
    static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n, m;
    static final int N = (int) (1e3 + 10);
    static int[][] g = new int[N][N];
    /*
    cnt = 0 空格
    cnt = 数字 雷
     */
    static int[][] cnt = new int[N][N];
    static boolean[][] str = new boolean[N][N];
    static int[] dx = {1, 1, 0, -1, -1, -1, 0, 1};
    static int[] dy = {0, 1, 1, 1, 0, -1, -1, -1};
    static int ans;

    public static void dfs(int x, int y) {
       for (int i = 0; i < 8; i++) {
          int nowx = dx[i] + x;
          int nowy = dy[i] + y;
          if(nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m) {
             if(!str[nowx][nowy] && cnt[nowx][nowy] == 0 && g[nowx][nowy] == 0) {
                str[nowx][nowy] = true;
                dfs(nowx, nowy);
             }
          }
       }
    }

    /*
    是否统计进去, 统计周围8格全是数字或者雷的格子
     */
    public static int solve(int x, int y) {
       int num = 0;
       for (int i = 0; i < 8; i++) {
          int nowx = x + dx[i];
          int nowy = y + dy[i];
          if(nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m) {
             if(cnt[nowx][nowy] == 0 && g[nowx][nowy] == 0){
                num++;
                break;
             }
          }
       }

       if(num == 1) return 0;
       else return 1;
    }

    public static void main(String[] args) throws IOException {
       new Thread(null, new Main(), "", 1 << 29).start();
    }

    @Override
    public void run() {

        try {
            cin.nextToken();
          n = (int) cin.nval;
          cin.nextToken();
          m = (int) cin.nval;

          for(int i = 1; i <= n; i++) {
             for(int j = 1; j <= m; j++) {
                cin.nextToken();
                g[i][j] = (int) cin.nval;
             }
          }

          for(int i = 1; i <= n; i++) {
             for(int j = 1; j <= m; j++) {
                if(g[i][j] == 0) {
                   int num = 0;
                   for (int k = 0; k < 8; k++) {
                      int nowx = i + dx[k];
                      int nowy = j + dy[k];
                      if(nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m) {
                         if(g[nowx][nowy] == 1) num++;
                      }
                   }
                   cnt[i][j] = num;
                }
             }
          }

          for(int i = 1; i <= n; i++) {
             for(int j = 1; j <= m; j++) {
                if(g[i][j] == 1) continue;
                if(str[i][j]) continue;
                if(cnt[i][j] == 0) {
                   str[i][j] = true;
                   dfs(i, j);
                   ans++;
                   continue;
                }
                int a = solve(i, j);
                ans += a;
             }
          }

          cout.println(ans);
          cout.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

Labyrinth - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;

public class Main implements Runnable {

    static Scanner scanner = new Scanner(System.in);
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n, m, r, c, x, y;
    static final int N = (int) (2e3 + 10);
    static int[][] g = new int[N][N];
    static Deque<Point> queue = new ArrayDeque<>();
    static int[] dx = {0, 1, 0, -1, 0};
    static int[] dy = {0, 0, 1, 0, -1};
    static int[][] dist = new int[N][N];

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }

    public static int judge(int x) {
        if(x == 4) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void bfs() {
        while (!queue.isEmpty()) {
            Point point = queue.pollFirst();
            int x_ = point.x;
            int y_ = point.y;
            for (int i = 1; i <= 4; i++) {
                int nowx = x_ + dx[i];
                int nowy = y_ + dy[i];
                if (nowx < 1 || nowx > n || nowy < 1 || nowy > m || g[nowx][nowy] == 1) continue;
                if (dist[x_][y_] + judge(i) >= dist[nowx][nowy] && dist[nowx][nowy] != -1) continue;

                dist[nowx][nowy] = dist[x_][y_] + judge(i);

                if (i == 4) {
                    queue.addLast(new Point(nowx, nowy));
                } else {
                    queue.addFirst(new Point(nowx, nowy));
                }
            }
        }
    }

    @Override
    public void run() {
        for(int i = 0; i < N; i++) {
            Arrays.fill(dist[i], -1);
        }
        n = scanner.nextInt();
        m = scanner.nextInt();
        r = scanner.nextInt();
        c = scanner.nextInt();
        x = scanner.nextInt();
        y = scanner.nextInt();

        for (int i = 1; i <= n; i++) {
            String s = scanner.next();
            for (int j = 0; j < m; j++) {
                if (s.charAt(j) == '*') g[i][j + 1] = 1;
            }
        }

        queue.addFirst(new Point(r, c));
        dist[r][c] = 0;
        bfs();


        int ans = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if(dist[i][j] == -1) continue;
                int a = dist[i][j];
                int b = j - c + a;
                if(a <= x && b <= y) ans++;
            }
        }

        cout.println(ans);
        cout.flush();
    }


    static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = 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;
        }
    }
}

P1294 高手去散步 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • 没啥特别的,唯一特别的可能就是无向图的搜索??
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;

public class Main implements Runnable{

    static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n, m;
    static Vector<Integer>[] node = new Vector[25];
    static int[][] dist = new int[25][25];
    static boolean[] str = new boolean[25];
    static int ans;

    public static int nextInt() throws IOException {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static void dfs(int num, int dis) {
        ans = Math.max(ans, dis);

        for (Integer i : node[num]) {
            if(!str[i]) {
                str[i] = true;
                dfs(i, dis + dist[num][i]);
                str[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }
    @Override
    public void run() {
        try {
            for(int i = 0; i < 25; i++) {
                node[i] = new Vector<>();
            }
            n = nextInt();
            m = nextInt();
            for(int i = 1; i <= m; i++) {
                int x = nextInt();
                int y = nextInt();
                int c = nextInt();
                node[x].add(y);
                node[y].add(x);
                dist[x][y] = c;
                dist[y][x] = c;
            }

            for(int i = 1; i <= n; i++) {
                str[i] = true;
                dfs(i, 0);
                str[i] = false;
            }

            cout.println(ans);
            cout.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
posted @ 2025-01-24 19:22  Mikkeykarl  阅读(36)  评论(0)    收藏  举报