Puzzle,ACMICPC World Finals 1993,UVa227

mac2022-06-30  116

题目:Puzzle

Description:有一个55的网格,其中恰好有一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(分别以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出"This puzzle has no final configuration.",例如,图执行ARRBBL0后,效果如图所示

源码如下:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int main() 6 { 7 char instruct[100]; 8 char excel[5][5]; 9 int startline=0,startlist; 10 for(int i=0;i<5;i++) 11 { 12 for(int j=0;j<6;j++) 13 { 14 char temp; 15 temp=getchar(); 16 if(temp==' ') 17 { 18 startline=i; 19 startlist=j; 20 excel[i][j]=temp; 21 } 22 else if(temp=='\n') 23 { 24 continue; 25 } 26 else excel[i][j]=temp; 27 } 28 } 29 // cin.clear(); 30 cin.getline(instruct,100); 31 int t=strlen(instruct); 32 int point=0; 33 for(int i=0;i<t;i++) 34 { 35 if(instruct[i]=='A') 36 { 37 if(startline-1<0) 38 { 39 cout<<"This puzzle has no final configuration"<<endl; 40 return 0; 41 } 42 else 43 { 44 swap(excel[startline][startlist],excel[startline-1][startlist]); 45 startline--; 46 } 47 } 48 else if(instruct[i]=='B') 49 { 50 if(startline+1>4) 51 { 52 cout<<"This puzzle has no final configuration"<<endl; 53 return 0; 54 } 55 else 56 { 57 swap(excel[startline][startlist],excel[startline+1][startlist]); 58 startline++; 59 } 60 } 61 else if(instruct[i]=='L') 62 { 63 if(startlist-1<0) 64 { 65 cout<<"This puzzle has no final configuration"<<endl; 66 return 0; 67 } 68 else 69 { 70 swap(excel[startline][startlist],excel[startline][startlist-1]); 71 startlist--; 72 } 73 } 74 else if(instruct[i]=='R') 75 { 76 if(startlist+1>4) 77 { 78 cout<<"This puzzle has no final configuration"<<endl; 79 return 0; 80 } 81 else 82 { 83 swap(excel[startline][startlist],excel[startline][startlist+1]); 84 startlist++; 85 } 86 } 87 else if(instruct[i]==' ')break; 88 } 89 //输出表格 90 for(int i=0;i<5;i++) 91 { 92 for(int j=0;j<5;j++) 93 { 94 cout<<excel[i][j]<<" "; 95 } 96 cout<<endl; 97 } 98 99 100 return 0; 101 }

 

转载于:https://www.cnblogs.com/agui521/p/7020305.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)