Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Tuesday, November 13, 2012

uva 168 - Theseus and the Minotaur


/*
Problem link
Type: Graph, graph traversal
Algorithm: 
 Just go but aware of the order of the vertex before reaching it.
 Never go back to the previous edge because that's where Thesus stand
 Every k vertex, lit it up
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = 30;
//----------------------------------
bool lit[maxn];
string adj[maxn];
int fa[maxn];
int k,mi,th;
//----------------------------------
bool readfile() {
 string line;
 getline(cin,line);
 if (line=="#") return(false);
 int i=0;
 k = 0;
 while (i<line.length()) 
 {
  if (line[i]==':' || i==1)
  {
   int u = int(line[i-1])-64;
   string tmp = "";
   int j = i+1;
   while (line[j]>='A' && line[j]<='Z')
   {
    tmp = tmp + line[j];
    j++;
   }
   adj[u] = tmp;
   if (line[j]!='.') i = j+1;
   else i = j;
  }
  else if (line[i]=='.')
  {
   mi = int(line[i+2])-64;
   th = int(line[i+4])-64;
   i++;
  }
  else if (line[i]>='0' && line[i]<='9') 
  {
   k = k*10 + int(line[i])-48;
   i++;
  }
  else i++;
 }
 return(true);
}
//----------------------------------
void go(int u, int i) {
 int v = maxn;
 if (adj[u].length()!=0)
  for (int j=0; j<adj[u].length(); j++)
  {
   int jj = int(adj[u][j])-64;
   if (lit[jj]==false && jj!=fa[u]) 
   {
    v = jj;
    break;
   }
  }
 if (v!=maxn) 
 {
  if (i==k) 
  {
   lit[u] = true;
   i = 0;
   cout << char(u+64) << " ";
  }
  fa[v] = u;
  go(v,i+1);
 }
 else cout << "/" << char(u+64) << endl;
}
//----------------------------------
int main() {
 while (readfile())
 {
  memset(lit,false,sizeof(lit));
  fa[mi] = th;
  fa[th] = -1;
  go(mi,1);
  for (int i=1; i<maxn; i++) adj[i] = "";
 }
 return 0;
}
//----------------------------------


No comments:

Post a Comment