关于实验二的补充(面向对象的程序设计)
#include<iostream> #include<string> #include<cstring> using std::cin; using std::cout; using std::endl; using std::string; void next(string & str,int n,string * store,int &m); void swap(string & str,int location1,int location2); void show(string *str,int num); void sort(string * str,int num); void swap(string *str,int m,int n); bool isend(string & str,int n,int num); int main() { cout << "请输入中间格局的个数:" << endl; int num; cin >> num; string *chessboard=new string[num]; string store[10]; int *digit=new int[num]; cout << "输入滑块数和中间格局状态:" << endl; for (int i = 0; i < num; i++) { cin >> digit[i]; cin >> chessboard[i]; cin.get(); } int amount; for(int i=0;i<num;i++) { amount=0; cout << "输出" <<i+1<<":"<<endl; if(isend(chessboard[i],strlen(chessboard[i].c_str()),digit[i])) cout << chessboard[i]; else { next(chessboard[i],2*digit[i]+1,store,amount); sort(store,amount); show(store,amount); } } } void next(string & str,int n,string * store,int &m) { int i; for(i=0;i<n;i++) { if(str[i]=='E') break; } for(int k=i-1;k>=0&&k>i-4;k--) { string s=str; swap(s,i,k); store[m]=s; m++; } for(int k=i+1;k<n&&k<i+4;k++) { string s=str; swap(s,i,k); store[m]=s; m++; } } void swap(string & str,int location1,int location2) { char c; c=str[location1]; str[location1]=str[location2]; str[location2]=c; } void show(string * str,int num) { for(int i=0;i<num;i++) cout << str[i] << endl; } void sort(string * str,int num) { for(int i=0;i<num-1;i++) { int k=i; int j; for(j=i+1;j<num;j++) { if(strcmp(str[j].c_str(),str[k].c_str())<0) k=j; } if(k!=i) swap(str,k,i); } } void swap(string *str,int m,int n) { string s; s=str[m]; str[m]=str[n]; str[n]=s; } bool isend(string &str,int n,int num) { int count=0; for(int i=0;i<n;i++) { if(str[i]=='W') count++; if(str[i]=='B'&&count<num) return false; } return true; }
作者:LightAc
出处:https://www.cnblogs.com/lightac/
联系:
Email: dzz@stu.ouc.edu.cn
QQ: 1171613053
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。