算法競賽入門經典3.5 謎題UVa455

  1. #include <iostream> 
  2. #include <string>
  3. using namespace std; 
  4. int main()
  5. {  
  6. char block[5][5]={{'T','R','G','S','J'},{'X','D','O','K','I'},{'M',' ','V','L','N'},{'W','P','A','B','E'},{'U','Q','H','C','F'}};
  7. int i=0;
  8. int a=2;
  9. int b=1;
  10. int d,e;
  11. for(d=0;d<=4;d++)
  12.       {for(e=0;e<=4;e++)
  13.    cout<<block[d][e];
  14.        cout<<endl;
  15. }
  16. string s;
  17.     cout<<"please input the string"<<endl;
  18.     cin>>s;
  19. while(i<=s.length()-1)  
  20.        {
  21.          if(s[i]=='A')
  22.            {   
  23.   a=a-1;
  24.       if(a<4&&a>0&&b<4&&b>0)
  25.       {
  26.  block[a+1][b]=block[a][b];
  27.              block[a][b]=' '; 
  28.   }
  29.   else {
  30.      cout<<"wrong"<<endl;
  31.  goto here;
  32.        }       
  33.            }
  34.          if(s[i]=='B')
  35.            {   
  36.   a=a+1;
  37.       if(a<4&&a>0&&b<4&&b>0)
  38.       {
  39.               block[a-1][b]=block[a][b];
  40.               block[a][b]=' ';                
  41.       }
  42.      else {
  43.      cout<<"wrong"<<endl;
  44.  goto here;
  45.        }
  46.            }
  47.          if(s[i]=='L')
  48.            {   
  49.   b=b-1;
  50.       if(a<4&&a>0&&b<4&&b>0)
  51.       {
  52.              block[a][b+1]=block[a][b];
  53.              block[a][b]=' ';              
  54.       }
  55.      else {
  56.      cout<<"wrong"<<endl;
  57.  goto here;
  58.        }
  59.            }       
  60.          if(s[i]=='R')
  61.            {   
  62.   b=b+1;
  63.       if(a<4&&a>0&&b<4&&b>0)
  64.       {
  65.              block[a][b-1]=block[a][b];
  66.              block[a][b]=' ';            
  67.       }
  68.      else {
  69.      cout<<"wrong"<<endl;
  70.  goto here;
  71.        }
  72.  
  73.            }   
  74. i++;
  75.        }
  76. for(d=0;d<=4;d++)
  77.       {for(e=0;e<=4;e++)
  78.    cout<<block[d][e];
  79.        cout<<endl;
  80. }
  81.     here:
  82.  ;
  83.     return 0; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章