Advertisement
PhanNguyen

da sua

Apr 17th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.79 KB | None | 0 0
  1. //Copyright: Nguyen Duc Minh Khoi (email: ducminhkhoi@gmail.com) - March 2015 NEW
  2. //REMEMBER: Do not include any other library. Otherwise you will get 0 mark
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. char map[10][20]; //Variable stores map
  10. vector<string> step; //Variable stores steps
  11. int numberOfSteps = 0;
  12.  
  13. void readFiles(char *);
  14. void printCurrentMap(char [10][20]); //convenience for checking your map
  15.  
  16. int tien = 0;
  17.  
  18. char left( int toado_x , int toado_y);
  19. char right( int toado_x, int toado_y);
  20. char top( int toado_x,int toado_y);
  21. char bottom( int toado_x,int toado_y);
  22. void intoado(int toado_x,int toado_y);
  23. bool ngoaimap(int vitri_x,int vitri_y);
  24. int ktmap (int &x, int &y, int &tien);
  25. void fall (int &x, int &y );
  26. void LEFT_ ( int &x, int &y );
  27. void RIGHT ( int &x, int &y );
  28. void JUMP_ (int &x, int &y );
  29. void LEFTJUMP_ (int &x, int &y);
  30. void RIGHTJUMP_ (int &x, int &y);
  31.  
  32. int main(int argc, char* argv[]){
  33.     //for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases.
  34.         //REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
  35.     char * filename ="testcase/testcase_d.txt" ;
  36.        
  37.     readFiles(filename);
  38.         printCurrentMap(map);
  39.         /////////////////////////////////////////////
  40.         //TODO: Complete Your code after this line
  41.         int a,b;
  42.         int i;
  43.         int x,y;
  44.                
  45.         for( a=0; a<=9; a++)
  46.         {
  47.                 for( b=0; b<=19; b++)
  48.                 {
  49.                         if( map[9-a][b]=='@')
  50.                         {
  51.                                 x=b;
  52.                                 y=a;
  53.                         }
  54.                 }
  55.         }
  56.        
  57.             cout << "(" << x << "," << y <<")";
  58.         for (i = 0; i < step.size(); i++)
  59.                 {
  60.                         cout << step.at(i);
  61.                         if ( step.at(i) == "LEFT_" ) LEFT_(x,y);
  62.                         if ( step.at(i) == "RIGHT" ) RIGHT(x,y);
  63.                         if ( step.at(i) == "JUMP_" ) JUMP_(x,y);
  64.                         if ( step.at(i) == "RJUMP" ) RIGHTJUMP_(x,y);
  65.                         if ( step.at(i) == "LJUMP" ) LEFTJUMP_(x,y);
  66.                         if ( ktmap(x,y,tien) == 1 ) break;
  67.                         cout << endl;
  68.                 }
  69.                   cout << endl;
  70.                 if ( ktmap(x,y,tien) != 1 ) cout << -1;
  71.                 else cout << tien;
  72.                
  73.                
  74.                                 system("pause");
  75.         return 0;
  76.        
  77. }
  78. //end main()
  79. ////////////////////////////////////
  80.                 char left( int toado_x , int toado_y)
  81.                 {
  82.                         char vitri;
  83.                         vitri=map[9-toado_y][toado_x-1];
  84.                         return vitri;
  85.                 }
  86.  ////////////////////////////////////
  87.                 char right( int toado_x, int toado_y)
  88.                 {
  89.                         char vitri;
  90.                         vitri= map[9-toado_y][toado_x+1];
  91.                         return vitri;
  92.                 }
  93. ////////////////////////////////////
  94.                 char top( int toado_x,int toado_y)
  95.                 {
  96.                         char vitri;
  97.                         vitri=map[8-toado_y][toado_x];
  98.                         return vitri;
  99.                 }
  100. ////////////////////////////////////
  101.                 char bottom( int toado_x,int toado_y)
  102.                 {
  103.                         char vitri;
  104.                         vitri=map[10-toado_y][toado_x];
  105.                         return vitri;
  106.                 }
  107. ////////////////////////////////////
  108.                 void intoado(int toado_x,int toado_y)
  109.                 {
  110.                         cout<<" ("<<toado_x<<","<<toado_y<<")";
  111.                 }
  112. ////////////////////////////////////
  113.                 bool ngoaimap(int vitri_x,int vitri_y)
  114.                 {
  115.                         bool outside;
  116.                         outside=( vitri_x == -1 || vitri_x == 20 || vitri_y == -1 || vitri_y == 10 );
  117.                 return outside;
  118.                 }
  119. ////////////////////////////////////
  120.                // int tien=0;
  121.                 int ktmap (int &x, int &y, int &tien)
  122.                 {
  123.                 switch (map[9-y][x])
  124.                 {
  125.                 case '$' :
  126.                     tien += 1;
  127.                     map[9-y][x] = ' ';
  128.                     break;
  129.                 case '!' :
  130.                     return 1;
  131.                     break;
  132.                 default :
  133.                     return 0;
  134.  
  135.                 }
  136.                 /*{
  137.                     if (map[9-y][x]=='$')
  138.                         {
  139.                             tien += 1;
  140.                             map[9-y][x] = ' ';  
  141.                         }
  142.                     if (map[9-y][x] == '!')
  143.                     return 1;
  144.                     else return 0;
  145.                     }*/
  146.                 }
  147. ////////////////////////////////////
  148.                 void fall (int &x, int &y )
  149.                 {
  150.                     while (y > 0)
  151.                     {
  152.                         if (bottom(x,y) != '#' && !ngoaimap(x,y-1))
  153.                             {        
  154.                                 y -= 1;
  155.                                 intoado(x,y);
  156.                                 if (ktmap(x,y,tien) == 1)
  157.                                 return;
  158.                             }
  159.                         else return;
  160.                     }
  161.                 }
  162. ////////////////////////////////////
  163.                 void LEFT_ ( int &x, int &y )
  164.                 {
  165.                     if (ngoaimap(x-1,y))
  166.                         intoado(x,y);
  167.                     else
  168.                     {
  169.                         switch (left(x,y))
  170.                         {
  171.                             case '#' :
  172.                                 intoado(x,y);
  173.                                 break;
  174.                             default :
  175.                            
  176.                                     x -= 1;
  177.                                     intoado(x,y);
  178.                                     if (ktmap(x,y,tien) == 1)
  179.                                     return;
  180.                                     fall(x,y);
  181.                                                    
  182.                                 break;
  183.                         }
  184.                     }
  185.                 }
  186.                        
  187. ////////////////////////////////////
  188.                 void RIGHT ( int &x, int &y )
  189.                 {
  190.                     if(ngoaimap(x+1,y))
  191.                         intoado(x,y);
  192.                     else
  193.                     {
  194.                     switch (right(x,y))
  195.                     {
  196.                     case '#':
  197.                         intoado(x,y);
  198.                         break;
  199.                     default :
  200.                         {
  201.                             x += 1;
  202.                             intoado(x,y);
  203.                             if (ktmap(x,y,tien) == 1)
  204.                             return;
  205.                             fall(x,y);
  206.                         }
  207.                             break;
  208.                     }
  209.                     }
  210.  
  211.                     }
  212. ////////////////////////////////////
  213.                 void JUMP_ (int &x, int &y )
  214.                 {
  215.                     if (top(x,y) != '#' && !ngoaimap(x,y+1))
  216.                     {  
  217.                         y += 1;
  218.                         intoado(x,y);
  219.                         if (ktmap(x,y,tien) == 1)
  220.                         return;
  221.                     }
  222.                     else intoado(x,y);
  223.                     if (top(x,y) != '#' && !ngoaimap(x,y+1))
  224.                     {  
  225.                         y += 1;
  226.                         intoado(x,y);
  227.                         if (ktmap(x,y,tien) == 1)
  228.                         return;
  229.                     }
  230.                     fall(x,y);
  231.        
  232.                 }
  233. ////////////////////////////////////
  234. void LEFTJUMP_ (int &x, int &y)
  235. {  
  236.     if(top(x,y)!='#' && !ngoaimap(x,y+1))
  237.     {
  238.         y+=1;
  239.         intoado(x,y);
  240.         if (ktmap(x,y,tien)==1)
  241.             return;
  242.         if (top(x,y) !='#' && !ngoaimap(x,y+1))
  243.         {
  244.             y+=1;
  245.             intoado(x,y);
  246.             if(ktmap(x,y,tien)==1)
  247.                 return;
  248.             if ( left(x,y) != '#' && !ngoaimap(x-1,y))
  249.             {
  250.                 x-=1;
  251.                 intoado(x,y);
  252.                 if( ktmap(x,y,tien)==1)
  253.                 return;
  254.                 if( left(x,y) !='#' && !ngoaimap(x-1,y))
  255.                 {
  256.                     x-=1;
  257.                     intoado(x,y);
  258.                     if( ktmap(x,y,tien)==1)
  259.                         return;
  260.                     fall (x,y);
  261.                 }
  262.                 else fall(x,y);
  263.             }
  264.             else fall(x,y);
  265.         }
  266.         else
  267.         if( left(x,y)!='#' && !ngoaimap(x-1,y))
  268.         {
  269.             x-=1;
  270.             intoado(x,y);
  271.             if(ktmap(x,y,tien)==1)
  272.                 return;
  273.             fall(x,y);
  274.         }
  275.         else fall(x,y);
  276.     }
  277.     else intoado(x,y);
  278. return;
  279. }
  280. ////////////////////////////////////
  281. void RIGHTJUMP_ (int &x, int &y)
  282.  {
  283.      if(top(x,y)!='#'&&!ngoaimap(x,y+1))
  284.      {
  285.          y+=1;
  286.          intoado(x,y);
  287.          if(ktmap(x,y,tien)==1)
  288.              return;
  289.          if(top(x,y)!='#'&&!ngoaimap(x,y+1))
  290.          {
  291.              y+=1;
  292.              intoado(x,y);
  293.              if(ktmap(x,y,tien)==1)
  294.                  return;
  295.              if( right(x,y)!='#'&&!ngoaimap(x+1,y))
  296.              {
  297.                  x+=1;
  298.                  intoado(x,y);
  299.                  if( ktmap(x,y,tien)==1)
  300.                      return;
  301.                  if( right(x,y)!='#'&& !ngoaimap(x+1,y))
  302.                  {
  303.                      x+=1;
  304.                      intoado(x,y);
  305.                      if(ktmap(x,y,tien)==1)
  306.                          return;
  307.                      fall(x,y);
  308.                  }
  309.                  else fall(x,y);
  310.              }
  311.              else fall(x,y);
  312.          }
  313.          else
  314.          if(right(x,y)!='#'&&!ngoaimap(x+1,y))
  315.          {
  316.              x+=1;
  317.              intoado(x,y);
  318.              if( ktmap(x,y,tien)==1)
  319.                  return;
  320.                 fall(x,y);
  321.          }
  322.          else fall(x,y);
  323.      }
  324.      else intoado(x,y);
  325.      return;
  326. }
  327.  
  328.  
  329.        
  330.  
  331.  
  332.  
  333.         //Hint: You may find it easier to use functions to remove duplicate codes.
  334.         //      However, you can code anything else. Your input is read in 2 variables
  335.         //              char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)
  336.        
  337.        
  338.        
  339.         //END TODO
  340.     /////////////////////////////////////////////
  341.  
  342. //Description: print the current map for easier debugging
  343. //INPUT: the current map variable
  344. //OUTPUT: no output, but it will print the current map on screen
  345. void printCurrentMap(char current_map[10][20]){
  346.     cout << endl;
  347.     cout << "Current Map:" << endl;
  348.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  349.     for (int i = 0; i < 10; i++){
  350.         cout << 9 - i << "| ";
  351.         for (int j = 0; j < 20; j++){
  352.             cout << current_map[i][j] << " ";
  353.         }
  354.  
  355.         cout << "|" << endl;
  356.     }
  357.  
  358.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  359.     cout << "   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9   " << endl;
  360. }
  361.  
  362. //Description: read char map[10][20] and vector<string> step from file
  363. //INPUT: file name in char *
  364. //OUTPUT: no output, but it will change the global variables map[10][20] and vector step
  365. //--------Given to Students----------//
  366. void readFiles(char* filename){
  367.     string line;
  368.     ifstream myfile (filename);
  369.  
  370.     int i = 0;
  371.  
  372.     if (myfile.is_open()){
  373.         while ( getline (myfile, line) ){
  374.             if (i < 10){
  375.                 strcpy(map[i++], line.c_str());
  376.             } else if (i >= 10){
  377.                 for (int j = 0; j < line.length(); j+=6){
  378.                     numberOfSteps++;
  379.                     string word = line.substr(j, 5);
  380.                     step.push_back(word);
  381.                 }
  382.             }
  383.         }
  384.  
  385.         myfile.close();
  386.     } else {
  387.         cout << "Unable to open file";
  388.     }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement