10503:The dominoes solitaire

問題文
http://uva.onlinejudge.org/external/105/10503.html

無向グラフが与えられる。スタートからゴールまでちょうどn回のステップで行くことができるか判定せよ。
































全探索した。

#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#define f first
#define s second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()

using namespace std;

typedef pair<int,int> P;

int goal;
vector<P>p;
bool fg[15];

bool rec(int now,int n){

  if(n<=0)return goal==now;

  bool res=false;
  for(int i=0;i<p.size();i++){
    if(!fg[i]){
      if(p[i].f==now)fg[i]=true,res|=rec(p[i].s,n-1),fg[i]=false;
      else if(p[i].s==now)fg[i]=true,res|=rec(p[i].f,n-1),fg[i]=false;
    }
  }
  return res;
}

int main(void){

  int n,m;
  while(cin >> n,n){
    cin >> m;
    p.clear();
    p.resize(m);

    int a,st;
    cin >> a >> st;
    cin >> goal >> a;

    rep(i,m)cin >> p[i].f >> p[i].s;
    fill(fg,fg+15,false);

    if(rec(st,n))cout << "YES" << endl;
    else cout << "NO" << endl;
  }
  return 0;
}