- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- 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'}};
- int i=0;
- int a=2;
- int b=1;
- int d,e;
- for(d=0;d<=4;d++)
- {for(e=0;e<=4;e++)
- cout<<block[d][e];
- cout<<endl;
- }
- string s;
- cout<<"please input the string"<<endl;
- cin>>s;
- while(i<=s.length()-1)
- {
- if(s[i]=='A')
- {
- a=a-1;
- if(a<4&&a>0&&b<4&&b>0)
- {
- block[a+1][b]=block[a][b];
- block[a][b]=' ';
- }
- else {
- cout<<"wrong"<<endl;
- goto here;
- }
- }
- if(s[i]=='B')
- {
- a=a+1;
- if(a<4&&a>0&&b<4&&b>0)
- {
- block[a-1][b]=block[a][b];
- block[a][b]=' ';
- }
- else {
- cout<<"wrong"<<endl;
- goto here;
- }
- }
- if(s[i]=='L')
- {
- b=b-1;
- if(a<4&&a>0&&b<4&&b>0)
- {
- block[a][b+1]=block[a][b];
- block[a][b]=' ';
- }
- else {
- cout<<"wrong"<<endl;
- goto here;
- }
- }
- if(s[i]=='R')
- {
- b=b+1;
- if(a<4&&a>0&&b<4&&b>0)
- {
- block[a][b-1]=block[a][b];
- block[a][b]=' ';
- }
- else {
- cout<<"wrong"<<endl;
- goto here;
- }
- }
- i++;
- }
- for(d=0;d<=4;d++)
- {for(e=0;e<=4;e++)
- cout<<block[d][e];
- cout<<endl;
- }
- here:
- ;
- return 0;
- }
算法競賽入門經典3.5 謎題UVa455
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.