#include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<cmath> #include<iostream> #include<vector> #include<map> using namespace std; int main() { //freopen("input.txt","r",stdin); int test,y=1; scanf("%d",&test); getchar(); getchar(); while(test--) { if(y>1)printf("\n"); y=3; map<string,int>word; vector<string>s; int i,j,k,l,m,n,c=1; string s1,s2,s3; while(cin >> s1 && s1!="*") { word[s1]=c++; s.push_back(s1); } sort(s.begin(),s.end()); int graph[s.size()][s.size()]; for(i=0;i<s.size();i++) { for(j=0;j<s.size();j++)graph[i][j]=0; } for(i=0;i<c-1;i++) { for(j=0;j<c-1;j++) { if(s[i].length()==s[j].length()&&i!=j) { int count=0; for(k=0;k<s[i].length();k++) { if(s[i][k]!=s[j][k])count++; } if(count==1) { m=word[s[i]]-1; n=word[s[j]]-1; graph[m][n]=1; //graph[n][m]=1; } } } } char temporary[50]; int len; char *p; cin.getline(temporary, sizeof(temporary)); // eat new line while (cin.getline(temporary, sizeof(temporary))){ len = strlen(temporary); if (len == 0) break; p = strtok(temporary, " \n\t"); string start(p); p = strtok(NULL, " \n\t"); string end(p); int color[c],d[c]; for(i=0;i<c;i++) { color[i]=d[i]=0; } queue<int>q; int source=word[start]-1; int destination=word[end]-1; //printf("%d %d\n",source,destination); q.push(source); color[source]=1; while(!q.empty()) { int u=q.front(); q.pop(); if(u==destination)break; for(i=0;i<c-1;i++) { if(graph[u][i]==1&&color[i]==0) { color[i]=1; q.push(i); d[i]=d[u]+1; } } } cout << start << " " << end << " " << d[destination] << endl; } } return 0; }
Friday, August 29, 2014
uva : 429 - Word Transformation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment