同样复杂度的代码在Java中执行,但在C++中超过了时间限制。

huangapple go评论130阅读模式
英文:

same complexity code executes in Java but time limit exceeds in C++

问题

The C++ code you provided seems to be similar to the Java code for finding all possible paths of a knight on a chessboard. However, you mentioned that the C++ code encounters a time limit issue while the Java code runs fine. One possible reason for this discrepancy could be related to input and output handling.

In the C++ code, you are using cin for input and cout for output. Input and output operations in C++ can be relatively slower than in Java. To potentially improve the performance of your C++ code, you can try the following:

  1. Optimize Input/Output: Use std::ios::sync_with_stdio(false); at the beginning of your C++ main function to disable synchronization between C and C++ standard streams, which can speed up input and output operations. Here's how you can do it:

    1. int main() {
    2. std::ios::sync_with_stdio(false);
    3. int n; cin >> n;
    4. int r; cin >> r;
    5. int c; cin >> c;
    6. vector<vector<int>> path(n, vector<int>(n, 0));
    7. solve(r, c, path, 1, n);
    8. return 0;
    9. }
  2. Use printf and scanf: In C++, you can also use printf for output and scanf for input, which may be faster than cin and cout. Here's an example:

    1. int main() {
    2. int n, r, c;
    3. scanf("%d %d %d", &n, &r, &c);
    4. vector<vector<int>> path(n, vector<int>(n, 0));
    5. solve(r, c, path, 1, n);
    6. return 0;
    7. }

These optimizations might help improve the performance of your C++ code and reduce the time limit issue. If the problem persists, consider profiling your code to identify specific bottlenecks that could be causing the performance difference between Java and C++.

英文:

The following is the code for finding all possible path of a knight on a chessboard of variable size
where the initial position is given.
The java code for this problem runs fine but for C++ The time limit exceeds.

JAVA CODE

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int r = sc.nextInt();
  8. int c = sc.nextInt();
  9. int [][] path = new int[n][n];
  10. solve(r,c,path,1,n);
  11. }
  12. public static void solve(int r , int c , int[][] path , int move , int n) {
  13. if(r&lt;0 || r&gt;=n || c&lt;0 || c&gt;=n || path[r][c]&gt;0)
  14. return;
  15. if(move == n*n)
  16. {
  17. path[r][c] = n*n;
  18. display (path,n);
  19. path[r][c] = 0;
  20. return;
  21. }
  22. path[r][c] = move;
  23. solve(r-2,c+1,path,move+1,n);
  24. solve(r-1,c+2,path,move+1,n);
  25. solve(r+1,c+2,path,move+1,n);
  26. solve(r+2,c+1,path,move+1,n);
  27. solve(r+2,c-1,path,move+1,n);
  28. solve(r+1,c-2,path,move+1,n);
  29. solve(r-1,c-2,path,move+1,n);
  30. solve(r-2,c-1,path,move+1,n);
  31. path[r][c] = 0 ;
  32. }
  33. public static void display(int [][] path , int n)
  34. {
  35. for(int i=0;i&lt;n;i++)
  36. {
  37. for(int j=0;j&lt;n;j++)
  38. {
  39. System.out.print(path[i][j]+&quot; &quot;);
  40. }
  41. System.out.print(&quot;\n&quot;);
  42. }
  43. System.out.print(&quot;\n&quot;);
  44. }
  45. }

C++ code

  1. #include&lt;bits/stdc++.h&gt;
  2. using namespace std;
  3. void display( vector&lt;vector&lt;int&gt;&gt; path , int n)
  4. {
  5. for(int i=0;i&lt;n;i++)
  6. {
  7. for(int j=0;j&lt;n;j++)
  8. {
  9. cout&lt;&lt;path[i][j]&lt;&lt;&quot; &quot;;
  10. }
  11. cout&lt;&lt;(&quot;\n&quot;);
  12. }
  13. cout&lt;&lt;(&quot;\n&quot;);
  14. }
  15. void solve(int r , int c ,vector&lt;vector&lt;int&gt;&gt; path , int move , int n) {
  16. if(r&lt;0 || r&gt;=n || c&lt;0 || c&gt;=n || path[r][c]&gt;0)
  17. return;
  18. if(move == n*n)
  19. {
  20. path[r][c] = n*n;
  21. display (path,n);
  22. path[r][c] = 0;
  23. return;
  24. }
  25. path[r][c] = move;
  26. solve(r-2,c+1,path,move+1,n);
  27. solve(r-1,c+2,path,move+1,n);
  28. solve(r+1,c+2,path,move+1,n);
  29. solve(r+2,c+1,path,move+1,n);
  30. solve(r+2,c-1,path,move+1,n);
  31. solve(r+1,c-2,path,move+1,n);
  32. solve(r-1,c-2,path,move+1,n);
  33. solve(r-2,c-1,path,move+1,n);
  34. path[r][c] = 0 ;
  35. }
  36. int main (){
  37. int n; cin&gt;&gt;n;
  38. int r; cin&gt;&gt;r;
  39. int c; cin&gt;&gt;c;
  40. vector&lt;vector&lt;int&gt;&gt; path(n,vector&lt;int&gt;(n,0));
  41. solve(r,c,path,1,n);
  42. return 0;
  43. }

please help me to sort out why is this so??

答案1

得分: 1

在你的Java代码中,数组没有被复制,而是传递了它的引用。

另一方面,在你的C++代码中,数组在每次函数调用时都被复制。

为了避免这种情况,你应该在函数参数中使用引用 vector<vector<int>>& path(添加 &)。

英文:

In your Java code, the array is not copied and its reference is passed.

On the other hand, in your C++ code, the array is copied on every function calls.

To avoid this, you should use reference vector&lt;vector&lt;int&gt;&gt;&amp; path (add &amp;) for your function arguments.

huangapple
  • 本文由 发表于 2020年8月1日 15:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定