编写一个程序实现字梯游戏。其中,单字母替换的值为1,而单字母的删除或添加的值p>0(p值由使用者指定)。
#include#include #include #include #include using namespace std;const int INFINTY = 99999;struct Vertex{ vector adj; vector weight; bool known; int dist; string name; int path;};void printPath(int vIndex,vector V){ if(vIndex>=0&&V[vIndex].path>-1) { printPath(V[vIndex].path,V); cout<<" to "; } cout< & Vertices){ int smallestDist; int smallestVertex; Vertex v,s,t; int n=Vertices.size(); Vertices[sIndex].dist=0; for(;;) { smallestDist= INFINITY; smallestVertex=-1; for(int i=0;i readWords(istream & in){ string oneLine; vector v; Vertex w; while(in>>oneLine) { w.name=oneLine; w.known=false; w.path=-1; w.dist=INFINITY; v.push_back(w); } return v;}int oneCharOff(const string & word1,const string & word2,int p){ string big,small,shrink; int cost; if(abs((int)(word.length()-word2.length()))>1) return 0; else if(word1.length()==word2.length()) { int diffs=0; for(int i=0;i 1) return 0; if(diffs==1) return 1; } if(word2.length()>word1.length()) { big=word2; small=word1; } else { big=word1; small=word2; } for(int i=0;i & words,int p){ int cost; for(int i=0;i 0) { words[i].adj.push_back(j); words[i].weight.push_back(cost); words[j].adj.push_back(i); words[j].weight.push_back(cost); } } }}int main(){ int p; ifstream fin("dict.txt"); string w1,w2; int w1Index,w2Index; cout<<"What is the cost of single char deletions: "; cin>>p; Vector words=readWords(fin); do { cout<<"Enter two words in the dictionary: "; cin>>w1>>w2; for(w1Index=0;w1Index =words.size()||w2Index>=words.size()); fillAdjacencies(words,p); dijkstra(w1Index,w2Index,words); cout<
为使本文得到斧正和提问,转载请注明出处: