java 輸入、輸出優化

P1141 01迷宮

import java.io.*;
import java.util.*;
class Node{
    int x, y;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Main{
    public static void main(String[] args) {
        //Scanner scan = new Scanner(System.in);
        myScanner scan = new myScanner(System.in);
        int[] stepx = {1, -1, 0, 0};
        int[] stepy = {0, 0, 1, -1};
        int n = scan.nextInt();
        int m = scan.nextInt();
        StringBuffer[] sb = new StringBuffer[n];
        //String[] strings = new String[n];
        char[][] chars = new char[n][n];
        for (int i = 0; i < n; i ++){
           // String str = scan.next();
            sb[i] = new StringBuffer();
            sb[i].append(scan.next());

            chars[i] = sb[i].toString().toCharArray();
        }
        int[][] result = new int[n][n];//存結果
        boolean[][] bb = new boolean[n][n];//
        Queue<Node> queue = new LinkedList<>();
        Queue<Node> queue2 = new LinkedList<>();
        for (int i = 0; i < n; i ++){
            for (int j = 0; j < n; j ++){
                int ans = 0;

                if (bb[i][j]) continue;
                bb[i][j] = true;
                queue.add(new Node(i, j));

                while (!queue.isEmpty()){
                    Node node = queue.remove();
                    queue2.add(node);
                    ans ++;
                    for (int k = 0; k < 4; k ++){
                        int x = node.x + stepx[k];
                        int y = node.y + stepy[k];

                        if (x >= 0 && x < n && y >= 0 && y < n && chars[x][y] != chars[node.x][node.y] && !bb[x][y] ){
                            bb[x][y] = true;
                            queue.add(new Node(x, y));
                        }
                    }
                }
                while (!queue2.isEmpty()){
                    Node node = queue2.remove();
                    result[node.x][node.y] = ans;
                   bb[node.x][node.y] = true;
                }
            }
        }
        //輸出優化
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        for (int t = 0; t < m; t ++){
            int x = scan.nextInt() - 1;
            int y = scan.nextInt() - 1;
            out.println(result[x][y]);
        }
        out.flush();

    }
    static class myScanner {//輸入優化
        public BufferedReader reader;
        public StringTokenizer st;

        myScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            st = null;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章