fork(3) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string a[1000][1000]; //-- represent the input grid
  4. string f[1000][1000]; //-- represent the array used for memoization
  5. bool calculated[1000][1000]; //-- false if we have not calculate the value before, and true if we have
  6. int n,m;
  7.  
  8. //-- sort the string str and return it
  9. string srt(string &str){
  10. sort(str.begin(), str.end());
  11. return str;
  12. }
  13.  
  14.  
  15. //-- return the smallest of x and y
  16. string smallest(string & x, string &y){
  17. for (int i = 0; i < x.size(); i++){
  18. if (x[i] < y[i]) return x;
  19. if (x[i] > y[i]) return y;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. string solve(int i, int j,int n,int m){
  26. if (i == 1 && j == 1) return a[i][j]; //-- if we have reached the buttom right cell (I assumed the array is 1-indexed
  27. if (calculated[i][j]) return f[i][j]; //-- if we have calculated this before
  28. string ans;
  29. if (i == n)
  30. {
  31. string rtt=a[i][j] + solve(i, j + 1);
  32. ans = srt(rtt);
  33. }//-- if we are at the buttom boundary
  34. else if (j == m)
  35. {
  36. string rtt=a[i][j] + solve(i + 1, j);
  37. ans = srt(rtt);
  38. }
  39. //-- if we are at the right boundary
  40. else
  41. {
  42. string s11=solve(i, j + 1);
  43. string s22=solve(i + 1, j);
  44. string rtt=a[i][j] + smallest(s11,s22 );
  45. ans = srt(rtt);
  46. }
  47. calculated[i][j] = true; //-- to fetch the calculated result in future calls
  48. f[i][j] = ans;
  49. return ans;
  50. }
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56. int n,m;
  57. cin>>n>>m;
  58. for(int i=0;i<n;i++)
  59. for(int j=0;j<m;j++)
  60. cin>>a[i][j];
  61.  
  62. cout<<solve(n,m,n,m);
  63. return 0;
  64. }
Compilation error #stdin compilation error #stdout 0s 20128KB
stdin
2 2
4 2
3 1
compilation info
prog.cpp: In function 'std::string solve(int, int, int, int)':
prog.cpp:31:41: error: too few arguments to function 'std::string solve(int, int, int, int)'
      string rtt=a[i][j] + solve(i, j + 1);
                                         ^
prog.cpp:25:8: note: declared here
 string solve(int i, int j,int n,int m){
        ^
prog.cpp:36:41: error: too few arguments to function 'std::string solve(int, int, int, int)'
      string rtt=a[i][j] + solve(i + 1, j);
                                         ^
prog.cpp:25:8: note: declared here
 string solve(int i, int j,int n,int m){
        ^
prog.cpp:42:31: error: too few arguments to function 'std::string solve(int, int, int, int)'
      string s11=solve(i, j + 1);
                               ^
prog.cpp:25:8: note: declared here
 string solve(int i, int j,int n,int m){
        ^
prog.cpp:43:31: error: too few arguments to function 'std::string solve(int, int, int, int)'
      string s22=solve(i + 1, j);
                               ^
prog.cpp:25:8: note: declared here
 string solve(int i, int j,int n,int m){
        ^
stdout
Standard output is empty