题意:给定起点和终点的n为坐标及一系列n为坐标,问是否能从起点走到终点;
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define maxn 200010
#define mol 1000000007
map<int,vector<int> >Map;//注意>>中间要有空格,不然会CE
set<int>Set;
int n,st,et;
int inprin(int &num,int n)
{
num=0;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(x==-1) return 0;
num=num*10+x;//亮点,,,把不同的n维坐标用num分开
}
return 1;
}
int dfs(int st,int et)
{
if(st==et) return 1;
for(int i=0;i<Map[st].size ();i++)
{
int v=Map[st][i];
if(!Set.count (v))//Set里有v返回1,没有返回0
{
Set.insert (v);//插入
if(dfs(v,et)) return 1;
}
}
return 0;
}
int main()
{
int n,t=1;
while(scanf("%d",&n)&&n)
{
Map.clear ();Set.clear ();
int s,e;
inprin(st,n);
inprin(et,n);
while(inprin(s,n))
{
inprin(e,n);
Map[s].push_back (e);
Map[e].push_back (s);
}
if(dfs(st,et))
printf("Maze #%d can be travelled\n",t++);
else
printf("Maze #%d cannot be travelled\n",t++);
}
return 0;
}