Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

fire_bot's blog

By fire_bot, 13 years ago, translation, In English

Good day,

Yesterday I was solving a simple UVA problem 11906 — Knight in a War Grid and got TLE > 1.000s using DFS. I removed recursion, reduced code to primitives and arrays. Even changed BufferedReader + split to Scanner but it didn't help to pass the limit. Reworked it in С++ and got AC for 0.038 ms.. Can anyone tell me where is the problem?

---------- Java ----------


import java.util.*; import java.lang.*; import java.io.*; public class Main { private static int[][] grid = new int[101][101]; private static boolean[][] visited = new boolean [101][101]; private static int[] xStack = new int[1000000]; private static int[] yStack = new int[1000000]; private static int i, j; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int T, W, R, C, M, N, caseNr = 1, even, odd, newX, newY, gridVal, stackPointer, tx, ty, isize; T = Integer.valueOf(reader.readLine()); String[] line = null; int[][] inc = null; while(T-- != 0) { line = reader.readLine().split("\\s+"); R = Integer.valueOf(line[0]); C = Integer.valueOf(line[1]); M = Integer.valueOf(line[2]); N = Integer.valueOf(line[3]); W = Integer.valueOf(reader.readLine()); if (N == M) { inc = new int[][] { { N, M }, { N, -M}, {-N, M}, {-N, -M} }; } else if (N == 0 || M == 0) { inc = new int[][] { { N, M }, { N, -M}, {M, N}, {-M, N} }; } else { inc = new int[][] { { N, M }, { N, -M}, {-N, M}, {-N, -M}, {M, N}, {M, -N}, {-M, N} , {-M, -N} }; } while(W-- != 0) { line = reader.readLine().split("\\s+"); grid[Integer.valueOf(line[0])][Integer.valueOf(line[1])] = -1; } even = odd = stackPointer = 0; xStack[stackPointer] = 0; yStack[stackPointer] = 0; isize = inc.length; while(stackPointer != -1) { tx = xStack[stackPointer]; ty = yStack[stackPointer]; stackPointer--; if (!visited[tx][ty]) { visited[tx][ty] = true; for(i = 0; i < isize; i++) { newX = tx + inc[i][0]; newY = ty + inc[i][1]; if (newX >= 0 && newX < R && newY >= 0 && newY < C && grid[newX][newY] != -1) { grid[newX][newY]++; stackPointer++; xStack[stackPointer] = newX; yStack[stackPointer] = newY; } } } } for(i = 0; i < R; i++) { for(j = 0; j < C; j++) { gridVal = grid[i][j]; if (gridVal > 0 || (i == 0 && j == 0)) { if (gridVal % 2 == 0) { even++; } else { odd++; } } grid[i][j] = 0; visited[i][j] = false; } } System.out.printf("Case %d: %d %d%n", caseNr++, even, odd); } } }

---------- C++ ----------


#include <cstdio> #include <cstring> #include <vector> #include <set> #include <map> using namespace std; int grid[101][101]; bool visited[101][101]; int xStack[1000000]; int yStack[1000000]; int i, j; int main() { int T, W, R, C, M, N, caseNr = 1, even, odd, newX, newY, gridVal, stackPointer, tx, ty; scanf("%d", &T); set<pair<int, int> > inc; while(T-- != 0) { scanf("%d %d %d %d %d", &R, &C, &M, &N, &W); inc.clear(); inc.insert(make_pair(N, M)); inc.insert(make_pair(N, -M)); inc.insert(make_pair(-N, M)); inc.insert(make_pair(-N, -M)); inc.insert(make_pair(M, N)); inc.insert(make_pair(M, -N)); inc.insert(make_pair(-M, N)); inc.insert(make_pair(-M, -N)); while(W-- != 0) { scanf("%d %d", &i, &j); grid[i][j] = -1; } even = odd = stackPointer = 0; xStack[stackPointer] = 0; yStack[stackPointer] = 0; //printf("%d\n", inc.size()); set<pair<int, int> >::iterator it; while(stackPointer != -1) { tx = xStack[stackPointer]; ty = yStack[stackPointer]; stackPointer--; if (!visited[tx][ty]) { visited[tx][ty] = true; for (it = inc.begin(); it != inc.end(); ++it) { newX = tx + (*it).first; newY = ty + (*it).second; if (newX >= 0 && newX < R && newY >= 0 && newY < C && grid[newX][newY] != -1) { grid[newX][newY]++; stackPointer++; xStack[stackPointer] = newX; yStack[stackPointer] = newY; } } } } for(i = 0; i < R; i++) { for(j = 0; j < C; j++) { gridVal = grid[i][j]; if (gridVal > 0 || (i == 0 && j == 0)) { if (gridVal % 2 == 0) { even++; } else { odd++; } } grid[i][j] = 0; visited[i][j] = false; } } printf("Case %d: %d %d\n", caseNr++, even, odd); } return 0; }
  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Almost solutions writen in C++ are much faster than in Java.

  • »
    »
    13 years ago, hide # ^ |
     
    Vote: I like it +17 Vote: I do not like it

    25 times faster?

    • »
      »
      »
      13 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      "- Надо починить телегу. - Сделаем. - А когда? - За пару часов управлюсь. - А за день? - Сделаю. - А за два дня? - Можно. - А за неделю? - Ну, барин, ты задачки ставишь! Тут помощник нужен!"

»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

StringTokenizer is faster than String.split() but I'am not sure if this can help you to pass TLE.

»
13 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

In my experiences, Java often 1.5 times slower than C++. I often read input using BufferedReader and StringTokenizer and write output by PrintWriter. Why don't you give it a try?

»
13 years ago, hide # |
← Rev. 4  
Vote: I like it 0 Vote: I do not like it

I used your java solution and changed the input method using the same BufferedReader but for reading the string I use StringTokenizer , it is faster than your solution using String.split() and it passes the sample test but gives wrong answer in final one(0.602 sec).Use the below link

code in java

  • »
    »
    13 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Yes you are right. I tried it myself. Got Accepted JAVA 0.669. Though also tried used BufferedOutputStream in conjunction.

    Also about WA this is a fix:


    else if (N == 0) { inc = new int[][] { { N, M }, { N, -M}, {M, N}, {-M, N}}; } else if (M == 0) { inc = new int[][] { { N, M }, {-N, M}, {M, N}, {M, -N} }; }
    • »
      »
      »
      13 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I did not saw the question even.I just changed the input method .But it is good that from next time your java code will not TL due to input output.Coder here help people very much.

»
13 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

I guess case is SOLVED. :) Thanks everyone who commented and helped me on this! It was a great learning curve :)

»
13 years ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

Typical Java