算法竞赛入门经典3.5 谜题UVa455

本文介绍了一个简单的C++程序,该程序实现了一个5x5字符矩阵的显示,并允许用户输入字符串来改变矩阵中特定位置的字符。具体操作包括上下左右移动空格位置,并确保移动在矩阵边界内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值