Grid Path CSES 问题可以做哪些进一步的优化?

问题描述

我正在尝试解决网格路径 CSES 问题 - link获取 7 个测试用例的 TLE - link

为了在所需的优化上取得一些领先,遵循了 William Lin 在 C++ 中的解决方案 - link。但即使在 Java 中实现了相同的功能,我还是得到了 TLE。想了解为什么会发生这种情况,以及如何解决这个问题。

问题的代码

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

class InputReader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer,bytesRead;

    public InputReader () {
        din = new DataInputStream(system.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public InputReader ( String file_name ) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine () throws IOException {
        byte[] buf = new byte[1000000]; // line length
        int cnt = 0,c;
        while (( c = read() ) != -1) {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf,cnt);
    }

    public int nextInt () throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = ( c == '-' );
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while (( c = read() ) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong () throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = ( c == '-' );
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while (( c = read() ) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble () throws IOException {
        double ret = 0,div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = ( c == '-' );
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while (( c = read() ) >= '0' && c <= '9');

        if (c == '.') {
            while (( c = read() ) >= '0' && c <= '9') {
                ret += ( c - '0' ) / ( div *= 10 );
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer () throws IOException {
        bytesRead = din.read(buffer,bufferPointer = 0,BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read () throws IOException {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close () throws IOException {
        if (din == null)
            return;
        din.close();
    }
}

public class Main {
    public static void main ( String[] args ) throws IOException {
        final InputReader reader = new InputReader();
        final String n = reader.readLine();
        System.out.println(new Solver().solve(n));
    }
}


class Solver {
    static int count = 0;

    public int solve ( String n ) {
        int row = 0;
        int col = 0;
        int index = 0;
        int maxGridSize = 7;
        int strLen = n.length();
        boolean[][] visited = new boolean[maxGridSize][maxGridSize];
        findGridpaths(index,row,col,n,maxGridSize,visited,strLen);
        return count;
    }

    private void findGridpaths ( int index,int row,int col,String n,int maxGridSize,boolean[][] visited,int strLen ) {
        
        if(index == strLen) {
           if( row == maxGridSize - 1 && col == 0) {
                count++;
            }
           return;
        }

        visited[row][col] = true;
        char direction = n.charat(index);
        if (direction == '?') {
            if (isValid(row+1,visited) &&!( !isValid(row + 2,visited) && isValid(row + 1,col - 1,col + 1,visited) )) {
                findGridpaths(index + 1,row + 1,strLen);
            }
            if (isValid(row-1,visited) &&!( !isValid(row - 2,visited) && isValid(row - 1,row - 1,strLen);
            }
            if (isValid(row,col+1,visited) &&!( !isValid(row,col + 2,col-1,col - 2,strLen);
            }
        } else {
            if (direction == 'D') {
                if (isValid(row+1,visited) && !( !isValid(row + 2,visited) )) {
                    findGridpaths(index + 1,strLen);
                }
            } else if (direction == 'U') {
                if (isValid(row-1,strLen);
                }
            } else if (direction == 'R') {
                if (isValid(row,strLen);
                }
            } else if (direction == 'L') {
                if (isValid(row,strLen);
                }
            }
        }
        visited[row][col] = false;
    }

    private boolean isValid ( int row,boolean[][] visited) {
        return row >= 0 && row < maxGridSize && col >= 0 && col < maxGridSize && !visited[row][col];
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)