#include<iostream>#include<stdio.h>#include<map>#include<string>using namespace std;map<string,int> maps;int fa[200];int find(int number){ if(fa[number]==number) return number; else { fa[number]=find(fa[number]); return fa[number]; }}int build(char path[],int& count){ int index; if(maps.find(string(path))==maps.end()) { maps[string(path)]=++count; index=count; fa[count]=count; } else index=maps[string(path)]; return index;}int main(){ int n; int number=1; while(scanf("%d",&n)&&n) { int i,p; char starting[15],ending[15]; for(i=0;i<n;i++) { scanf("%d",&p); starting[i]=p+'0'; } starting[n]=0; for(i=0;i<n;i++) { scanf("%d",&p); ending[i]=p+'0'; } ending[n]=0; int count=0; maps.clear(); char path[15]; while(scanf("%d",&p)) { if(p==-1) break; int start,end; path[0]=p+'0'; for(i=1;i<n;i++) { scanf("%d",&p); path[i]=p+'0'; } path[n]=0; start=build(path,count); for(i=0;i<n;i++) { scanf("%d",&p); path[i]=p+'0'; } path[n]=0; end=build(path,count); int h1=find(start); int h2=find(end); if(h1!=h2) fa[h1]=h2; } int possible=1; if(maps.find(string(starting))==maps.end()||maps.find(string(ending))==maps.end()) possible=0; else { int start=maps[string(starting)]; int end=maps[string(ending)]; int h1=find(start); int h2=find(end); if(h1!=h2) possible=0; } if(possible) printf("Maze #%d can be travelledn",number++); else printf("Maze #%d cannot be travelledn",number++); }return 0;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)