1 solutions
-
0
C++ :
#include<bits/stdc++.h> using namespace std; int N,x,y; string s[1005],bg,en; int m[1005][1005]; bool cmp(string a, string b){ int c=0; for (int i=0; i<a.size(); i++) if (a[i]!=b[i]) c++; return c==1; } int BFS( int cu, int ob); int main(){ cin >> bg >> en; cin >> N; int endi=-1; for (int i=1; i<=N; i++){ cin >> s[i]; if (s[i]==en) endi=i; } if (endi==-1) { cout << 0; return 0; } s[0]=bg; for (int i=0; i<N; i++) for (int j=i+1; j<N+1; j++) if (cmp(s[i],s[j])) m[i][j]=m[j][i]=1; int ans=BFS(0,endi); cout << ans; return 0; } int vis[1010], qu[1010]; int BFS( int cu, int ob){ int head,tail; head=tail=0; qu[0]=cu; vis[head]=1; // step ; for (; head<=tail ; head++){ int v=qu[head]; if (v==ob) return vis[v]; //找到 for (int q=0;q<=N ; q++) if (m[v][q]==1 && vis[q]==0){ qu[++tail] = q; vis[q] = vis[v]+1; // step } } return 0; }
- 1
Information
- ID
- 935
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By