rohantrix's blog

By rohantrix, history, 3 years ago, In English

In today's Codeforces Round (#763) problem B, I tried finding a pair object in a HashSet. Even after overriding equals() and hashcode() methods, I still ended up with WA on test 4. Here is the link to my solutions:

1) WA Submission using HashSet

2) Working Solution using 2D matrix

Please let me know of a good fix for this.

vinam SecondThread

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By rohantrix, history, 3 years ago, In English

[SOLVED] Both the codes in Java and Python TLE on CSES for same test cases (7-10). Please help me resolve this issue.

Java: [WORKING NOW]

    static int endi, endj;
 
    static void solve() throws IOException{
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        char mat[][] = new char[n][m];
        char dir[][] = new char[n][m]; 
        fill2D(dir, '#');
        int si = 0, sj = 0;
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            mat[i] = st.nextToken().toCharArray();
            for (int j = 0; j < m; j++) {
                if (mat[i][j] == 'A') {
                    si = i;
                    sj = j;
                } else if (mat[i][j] == 'B') {
                    endi = i;
                    endj = j;
                }
            }
            
        }
        
        boolean f = false;
        ArrayDeque<int[]> q = new ArrayDeque<>();
        q.offerLast(new int[]{si, sj});
        while (!q.isEmpty()) {
            int[] tmp = q.pollFirst();
            // System.out.println(tmp);
            if (tmp[0] == endi && tmp[1] == endj) {
                f = true;
                break;
            }
            
 
            int nx, ny;
            // Left
 
            nx = tmp[0];
            ny = tmp[1] - 1;
            if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
                q.offerLast(new int[]{nx,ny});
                dir[nx][ny] = 'L';
                mat[nx][ny] = '#';
 
            }
 
            // Right
 
            nx = tmp[0];
            ny = tmp[1] + 1;
            if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
                q.offerLast(new int[]{nx,ny});
                dir[nx][ny] = 'R';
                mat[nx][ny] = '#';
            }
 
            // Up
            nx = tmp[0] - 1;
            ny = tmp[1];
            if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
                q.offerLast(new int[]{nx,ny});
                dir[nx][ny] = 'U';
                mat[nx][ny] = '#';
            }
            // Down
            nx = tmp[0] + 1;
            ny = tmp[1];
            if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
                q.offerLast(new int[]{nx,ny});
                dir[nx][ny] = 'D';
                mat[nx][ny] = '#';
            }
            // System.out.println(q);
        }
        StringBuilder s = new StringBuilder();
        if (!f)
        System.out.println("NO");
        else {
            int nx = endi, ny = endj;
            while(nx!=si || ny!=sj)
            {
                s.append(dir[nx][ny]);
                switch(dir[nx][ny])
                {
                    case 'L':{ny++;break;}
                    case 'R':{ny--;break;}
                    case 'U':{nx++;break;}
                    case 'D':{nx--;break;}
                }
            }
            s.reverse();
            output.write("YES\n");
            output.write(s.length() + "\n");
            output.write(s.toString());
        }
        output.flush();

 
    }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By rohantrix, history, 4 years ago, In English

I am maintaining a repository which contains everything related to Competitive Programming in Java

Github Link: Here

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it