Skip to content

Instantly share code, notes, and snippets.

@D0tty
Created March 19, 2016 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D0tty/c4a62cff63df251154b0 to your computer and use it in GitHub Desktop.
Save D0tty/c4a62cff63df251154b0 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Player
{
static void Main(string[] args)
{
string[] inputs;
inputs = Console.ReadLine().Split(' ');
int N = int.Parse(inputs[0]); // the total number of nodes in the level, including the gateways
int L = int.Parse(inputs[1]); // the number of links
int E = int.Parse(inputs[2]); // the number of exit gateways
Tuple<int, bool>[,] ra = new Tuple<int, bool>[N, N]; //Static Graph Tuple<int,bool> int = node bool=gateway 1=link 0=nolink
List<int> ls = new List<int>(); //List of Gateways
#region Init Graph
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
ra[i, j] = new Tuple<int, bool>(0, false);
}
}
#endregion
for (int i = 0; i < L; i++)
{
inputs = Console.ReadLine().Split(' ');
int n1 = int.Parse(inputs[0]); // N1 and N2 defines a link between these nodes
int n2 = int.Parse(inputs[1]);
ra[n1, n2] = new Tuple<int, bool>(1, false); //create link n1 -> n2
ra[n2, n1] = new Tuple<int, bool>(1, false); //create link in both ways n2 -> n1
}
for (int i = 0; i < E; i++)
{
int G = int.Parse(Console.ReadLine()); // the index of a gateway node
ls.Add(G); //add Gate way to my gateway list
ra[G, G] = new Tuple<int, bool>(ra[G, G].Item1, true); //add gateway in the graph tupple int,bool with bool at true
}
// game loop
while (true)
{
int SI = int.Parse(Console.ReadLine()); // The index of the node on which the Skynet agent is positioned this turn
int rep = isLinkedToGateWay(ra,ls,SI);
if (rep != -1) //if this node is linked to a Gateway
{
string txt = deleteLink(ra, SI, rep);
Console.WriteLine(txt);
}
else
{
string txt = isolateGateWay(ra, ls);
Console.WriteLine(txt);
}
}
}
//Return -1 if not linked or gatewaynumber if linked to a gateway
static int isLinkedToGateWay(Tuple<int, bool>[,] ra, List<int> ls, int node)
{
int ret = -1;
bool go = true;
int N = ra.GetLength(0);
int k=0,i=0;
for (k = 0; k < ls.Count && go; k++)
{
i = 0;
while (go && i < N)
{
if (ra[i,node].Item1==1)
{
go = i != ls[k]; //is i a gateway ? invert true false
}
i += go ? 1 : 0;
}
}
if (!go)
{
ret = i;
}
return ret;
}
//Retrun the link to destroy near the first gateway in the list if there is a Gateway or Emptystring
static string isolateGateWay(Tuple<int, bool>[,] ra, List<int> ls)
{
string ret = String.Empty;
int N = ra.GetLength(0);
int node = -1;
int i = 0;
if (ls.Count > 0)
{
while (i < N && node == -1)
{
node = ra[i,ls[0]].Item1!=0 ? i:-1;
i += node != -1 ? 0 : 1;
}
}
if(node != -1)
{
ret = node+" " + ls[0];
ra[i, ls[0]] = new Tuple<int, bool>(0,false);
ra[ls[0], i] = new Tuple<int, bool>(0, false);
}
return ret;
}
//Delete link between skynet and the given gatevay
static string deleteLink(Tuple<int, bool>[,] ra, int node, int gateway)
{
ra[node,gateway] = new Tuple<int, bool>(0,false);
ra[gateway,node] = new Tuple<int, bool>(0,false);
return node+" "+gateway;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment