public override void Solve() { var pparser = new Pparser(FpatIn); int cletter, ccol, crow; pparser.Fetch(out cletter, out ccol, out crow); var rgletter = new Letter[cletter]; for (int i = 0; i < cletter; i++) { rgletter[i] = new Letter(pparser.Fetch <string>(), pparser.FetchN <string>(crow).ToArray()); } var bmpSrc = (Bitmap)Image.FromFile(FpatIn.Replace(".in", ".png")); var mp = ReadLetters(bmpSrc, rgletter); using (Output) foreach (var letter in rgletter) { Solwrt.WriteLine(letter.Ch + " " + mp[letter]); } }
public override void Solve() { var pparser = new Pparser(FpatIn); var x = pparser.Fetch <List <int> >(); var c = pparser.Fetch <List <int> >(); var rgA = pparser.Fetch <int[]>(); var rgB = pparser.Fetch <int[]>(); var fAWins = FWins(x, rgA); var fBWins = FWins(x, rgB); var i = 7; for (; !fAWins && !fBWins; i++) { x.Add((x[7] * c[0] + x[6] * c[1] + x[5] * c[2] + x[4] * c[3] + x[3] * c[4] + x[2] * c[5] + x[1] * c[6] + x[0] * c[7]) % 1000); x.RemoveAt(0); fAWins = FWins(x, rgA); fBWins = FWins(x, rgB); } using (Output) { Solwrt.WriteLine(fAWins ? "A wins at {0}." : "B wins at {0}.", i); } }
public override void Solve() { var pparser = new Pparser(FpatIn); int cactor, cfilm; pparser.Fetch(out cactor, out cfilm); var rgprice = pparser.FetchN <int>(cactor).ToArray(); var mplayed = new bool[cactor, cactor]; for (int ifilm = 0; ifilm < cfilm; ifilm++) { var rgactor = pparser.Fetch <int[]>().Skip(1).ToArray(); for (int i = 0; i < rgactor.Length; i++) { for (int j = 0; j < rgactor.Length; j++) { if (i == j) { continue; } //1 based mplayed[rgactor[i] - 1, rgactor[j] - 1] = true; } } } int priceMin = int.MaxValue; for (int iactor = 0; iactor < cactor; iactor++) { for (int jactor = 0; jactor < cactor; jactor++) { if (iactor == jactor || mplayed[iactor, jactor]) { continue; } var price = rgprice[iactor] + rgprice[jactor]; if (price < priceMin) { priceMin = price; } } } using (Output) { if (priceMin == int.MaxValue) { Solwrt.WriteLine("No solution."); } else { Solwrt.WriteLine(priceMin); } } }
public override void Solve() { var pp = new Pparser(FpatIn); var n = pp.Fetch <int>(); var rgnode = pp.FetchN <Node>(n).ToArray(); rgnode = rgnode.OrderBy(node => node.dtStart).ToArray(); for (int inode = 0; inode < n; inode++) { rgnode[inode].inode = inode; } var m = 0; for (int i = 0; i < rgnode.Length; i++) { m = Math.Max(m, MaxV(rgnode[i], rgnode)); } Console.WriteLine(m); }
public override void Solve() { var pparser = new Pparser(FpatIn); int n, l; pparser.Fetch(out n, out l); var rgnode = pparser.FetchN <Node2>(n); rgnode.Add(new Node2("X")); n++; double inf = n * n * n; var tsp = new TSP(); tsp.n = n; tsp.rgst = rgnode.Select(node => node.st).ToArray(); tsp.cost = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j || !Node2.FMatch(rgnode[i], rgnode[j])) { tsp.cost[i, j] = inf; } else { tsp.cost[i, j] = 1; } } } tsp.solve(); }
public ExerciseB(string fileName) { var pparser = new Pparser(fileName); int[] ints = pparser.Fetch <int[]>(); KnigtsNumber = ints[0]; RequirementsNumber = ints[1]; for (int i = 0; i < RequirementsNumber; i++) { char knight; var line = pparser.StLineNext(); knight = line[0]; if (line.IndexOf("hates") != -1) { line = line.Substring(8); string[] fragments = line.Split(' '); for (int j = 0; j < fragments.Length; j++) { if (fragments[j].Length == 1) { hatesMap.AddToList(knight, fragments[j][0]); } } } else if (line.IndexOf("needs") != -1) { line = line.Substring(8); string[] fragments = line.Split(' '); for (int j = 0; j < fragments.Length; j++) { if (fragments[j].Length == 1) { needsMap.AddToList(knight, fragments[j][0]); } } } else { Debug.Assert(false); } } }
public override void Solve() { var pparser = new Pparser(FpatIn); int ccity, cwizard; pparser.Fetch(out ccity, out cwizard); var rgcity = pparser.FetchN <Nod>(ccity); var rgwizard = pparser.FetchN <Nod>(cwizard); var graph = new CustomGraph(); var mpnodByinod = new Dictionary <long, Nod>(); foreach (var wizard in rgwizard) { wizard.kind = Kind.Wizard; wizard.node = graph.AddNode(); mpnodByinod[wizard.node.Id] = wizard; } foreach (var city in rgcity) { city.kind = Kind.City; city.node = graph.AddNode(); mpnodByinod[city.node.Id] = city; } foreach (var wizard in rgwizard) { foreach (var city in rgcity) { if (Dist(wizard, city) <= 50) { graph.AddArc(wizard.node, city.node, Directedness.Directed); } } } var mm = new MaximumMatching(graph, node => mpnodByinod[node.Id].kind == Kind.Wizard); mm.Run(); using (Output) { Solwrt.WriteLine(mm.Matching.ArcCount()); } }
public override void Solve() { var pparser = new Pparser(FpatIn); int n, l; pparser.Fetch(out n, out l); var rgnode = pparser.FetchN <string>(n); rgnode.Insert(0, "X"); var model = new RoutingModel(n, 1); model.SetFirstSolutionStrategy(RoutingModel.ROUTING_GLOBAL_CHEAPEST_ARC); //model.SetMetaheuristic(RoutingModel.ROUTING_TABU_SEARCH); model.SetCost(new NodeEval(rgnode.ToArray())); model.SetDepot(0); for (int i = 0; i < n; i++) { var varI = model.NextVar(i); for (int j = 0; j < n; j++) { if (i == j) { continue; } if (!NodeEval.FMatch(rgnode[i], rgnode[j])) { varI.RemoveValue(j); } } } Console.WriteLine("solving"); Assignment solution = model.Solve(); model.UpdateTimeLimit(1000 * 60 * 3); if (solution != null) { // Solution cost. Console.WriteLine("Cost = {0}", solution.ObjectiveValue()); for (var inode = (int)model.Start(0); !model.IsEnd(inode); inode = (int)solution.Value(model.NextVar(inode))) { Console.WriteLine(rgnode[inode]); } Console.WriteLine("0"); } }
public override void Solve() { int cpart, cch; Pparser.Fetch(out cpart, out cch); var rgst = Pparser.FetchN <string>(cpart).ToArray(); var mpnodeByst = new Dictionary <string, Node>(); foreach (var st in rgst) { var stA = st.Substring(0, 5); var stB = st.Substring(st.Length - 5); if (!mpnodeByst.ContainsKey(stA)) { mpnodeByst[stA] = new Node(stA); } if (!mpnodeByst.ContainsKey(stB)) { mpnodeByst[stB] = new Node(stB); } var edge = new Edge(st, mpnodeByst[stA], mpnodeByst[stB]); mpnodeByst[stA].rgedgeOut.Add(edge); mpnodeByst[stB].rgedgeIn.Add(edge); } var rgedge = Euler(new HashSet <Node>(mpnodeByst.Values)).ToArray(); Sanity(rgedge, rgst); string stResult = ""; stResult = rgedge[0].st; for (int i = 1; i < rgedge.Length; i++) { stResult += rgedge[i].st.Substring(5); } using (Output) { Solwrt.Write(stResult); } }
public override void Solve() { var pparser = new Pparser(FpatIn); var l = pparser.Fetch <int>(); var mp = new Dictionary <int, SortedDictionary <string, string> >(); Foo(l, mp); using (Output) { var rgst = Enst(mp, l).ToList(); rgst.Sort(StringComparer.Ordinal); foreach (var st in rgst) { // Console.WriteLine(st); Solwrt.WriteLine(st); } } }
private void InitFromFile() { Pparser pparser = new Pparser(mFilenamebase); mShowCount = pparser.Fetch <int>(); mGraph = new Graph(); for (int i = 0; i < mShowCount; i++) { var line = pparser.Fetch <string>(); Time t1 = Time.Parse(line.Substring(0, 5)); Time t2 = Time.Parse(line.Substring(6, 5)); if (t2 < t1) { t2.Hour += 24; } line = line.Substring(12); int profit = int.Parse(line); Vertex v1 = mpTimeVertex[t1] as Vertex; if (v1 == null) { v1 = new Vertex(t1); mpTimeVertex[t1] = v1; mGraph.AddVertex(v1); } Vertex v2 = mpTimeVertex[t2] as Vertex; if (v2 == null) { v2 = new Vertex(t2); mpTimeVertex[t2] = v2; mGraph.AddVertex(v2); } Debug.Assert(v1 != v2); mGraph.AddEdge(v1, v2, profit); } }
public override void Solve() { var pparser = new Pparser(FpatIn); rgx = pparser.Fetch <int[]>(); rgc = pparser.Fetch <int[]>(); rgGoalA = pparser.Fetch <int[]>(); rgGoalB = pparser.Fetch <int[]>(); time = 7; Winner winner = Winner.Nil; while (winner == Winner.Nil) { winner = Step(); } string result = winner.ToString() + " wins at " + time.ToString() + "."; using (Output) Solwrt.WriteLine(result); }
public override void Solve() { var pparser = new Pparser(FpatIn); var g = new Graph(); var cvertex = pparser.Fetch <int>(); var rgvertex = new Vertex[cvertex]; for (int ivertex = 0; ivertex < cvertex; ivertex++) { double x, y; pparser.Fetch(out x, out y); var vertex = new Vertex(x, y, ivertex); rgvertex[ivertex] = vertex; g.AddVertex(vertex); } var cedge = pparser.Fetch <int>(); for (int iedge = 0; iedge < cedge; iedge++) { int ivertexFrom, ivertexTo; pparser.Fetch(out ivertexFrom, out ivertexTo); g.AddEdge(new Edge <Vertex>(rgvertex[ivertexFrom], rgvertex[ivertexTo])); } var bitmap = new Bitmap(1024, 1024); var graphics = Graphics.FromImage(bitmap); graphics.FillRectangle(Brushes.Blue, new Rectangle(0, 0, 1024, 1024)); foreach (var country in EncountryGet(g)) { country.DrawCountry(graphics); } bitmap.Save(FpatOut); }
public override void Solve() { var pparser = new Pparser(FpatIn); int n, l; pparser.Fetch(out n, out l); var rgnode = pparser.FetchN <Node>(n); rgnode.Add(new Node("X")); n++; var solver = Google.OrTools.LinearSolver.Solver.CreateSolver("IntegerProgramming", "CBC_MIXED_INTEGER_PROGRAMMING"); for (int i = 0; i < n; i++) { rgnode[i].varU = solver.MakeIntVar(0, n, "U{0}".StFormat(i)); } var mpnodeByVarout = new Dictionary <Variable, Node>(); var rgvarx = new List <Variable>(); for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { if (i == j) { continue; } if (Node.FMatch(rgnode[i], rgnode[j])) { var varXij = solver.MakeIntVar(0, 1, "x{0}_{1}".StFormat(i, j)); rgvarx.Add(varXij); rgnode[i].rgvarOut.Add(varXij); rgnode[j].rgvarIn.Add(varXij); mpnodeByVarout[varXij] = rgnode[j]; if (i > 0 && j > 0) { solver.Add(rgnode[i].varU - rgnode[j].varU + varXij * n <= n - 1); } } } } foreach (var node in rgnode) { solver.Add(new SumVarArray(node.rgvarOut.ToArray()) == 1); solver.Add(new SumVarArray(node.rgvarIn.ToArray()) == 1); } solver.Maximize(new SumVarArray(rgvarx.ToArray())); solver.SetTimeLimit(60 * 1000); var resultStatus = solver.Solve(); double min, max; if (resultStatus == Google.OrTools.LinearSolver.Solver.OPTIMAL) { min = max = solver.Objective().Value(); var nodeCur = rgnode.Last(); int s = 0; var rgst = new List <string>(); while (nodeCur != null) { Console.WriteLine("{0}\t{1}", ++s, nodeCur.st); rgst.Add(nodeCur.st); var varOut = nodeCur.rgvarOut.SingleOrDefault(var => var.SolutionValue() == 1); if (varOut != null) { nodeCur = mpnodeByVarout[varOut]; } else { nodeCur = null; } if (nodeCur == rgnode.Last()) { break; } } rgst = rgst.Skip(1).ToList(); string stResult = ""; stResult = rgst[0]; for (int i = 1; i < rgst.Count; i++) { stResult += rgst[i].Substring(5); } using (Output) { Solwrt.Write(stResult); } } else { min = solver.Objective().Value(); max = solver.Objective().BestBound(); } Console.WriteLine(min); Console.WriteLine(max); }
public override void Solve() { var pparser = new Pparser(FpatIn); int cknight, cconstraint; pparser.Fetch(out cknight, out cconstraint); var knightLim = knightFirst + cknight; var mprgknightNeedsByknight = new Dictionary <char, List <char> >(); var mprgknightHatesByknight = new Dictionary <char, List <char> >(); for (var knight = knightFirst; knight < knightLim; knight++) { mprgknightHatesByknight[knight] = new List <char>(); mprgknightNeedsByknight[knight] = new List <char>(); } for (var i = 0; i < cconstraint; i++) { var rgst = pparser.StLineNext().Split(' '); char knight = rgst[0][0]; var rgknight = rgst[1] == "needs" ? mprgknightNeedsByknight[knight] : mprgknightHatesByknight[knight]; for (int l = 2; l < rgst.Length; l += 2) { rgknight.Add(rgst[l][0]); } } var rgknightNotSeen = new List <char>(); for (var knight = knightFirst; knight < knightLim; knight++) { rgknightNotSeen.Add(knight); } using (Output) { foreach (var knights in Enknights(rgknightNotSeen, null, null, mprgknightNeedsByknight, mprgknightHatesByknight)) { if (!FOk(knights[cknight - 1], knights[0], knights[1], mprgknightNeedsByknight, mprgknightHatesByknight)) { continue; } if (!FOk(knights[cknight - 2], knights[cknight - 1], knights[0], mprgknightNeedsByknight, mprgknightHatesByknight)) { continue; } for (int i = 0; i < knights.Length; i++) { if (!FOk(knights[(i - 1 + knights.Length) % knights.Length], knights[i], knights[(i + 1 + knights.Length) % knights.Length], mprgknightNeedsByknight, mprgknightHatesByknight)) { throw new Exception("coki"); } } Solwrt.WriteLine(knights); } } }
public override void Solve() { int ccol, crow, cstep; var pparser = new Pparser(FpatIn); pparser.Fetch(out crow, out ccol, out cstep); var field = new Kcell[crow, ccol]; for (int irow = 0; irow < crow; irow++) { var stLine = pparser.StLineNext(); for (int icol = 0; icol < ccol; icol++) { field[irow, icol] = KcellFromCh(stLine[icol]); } } var cgrassMin = CCell(field, Kcell.Grass); var istepGrassMin = 0; var fieldGrassMin = field; var cgrassMax = CCell(field, Kcell.Grass); var istepGrassMax = 0; var fieldGrassMax = field; var crabbitMin = CCell(field, Kcell.Rabbit); var istepRabbitMin = 0; var fieldRabbitMin = field; var crabbitMax = CCell(field, Kcell.Rabbit); var istepRabbitMax = 0; var fieldRabbitMax = field; var cfoxMin = CCell(field, Kcell.Fox); var istepFoxMin = 0; var fieldFoxMin = field; var cfoxMax = CCell(field, Kcell.Fox); var istepFoxMax = 0; var fieldFoxMax = field; using (var solwrt = new Solwrt(FpatOut, FpatRefout)) { for (int istep = 0; istep < cstep;) { field = Step(istep, field); istep++; var cgrass = CCell(field, Kcell.Grass); var crabbit = CCell(field, Kcell.Rabbit); var cfox = CCell(field, Kcell.Fox); if (crabbit > crabbitMax) { crabbitMax = crabbit; istepRabbitMax = istep; fieldRabbitMax = field; } if (crabbit < crabbitMin) { crabbitMin = crabbit; istepRabbitMin = istep; fieldRabbitMin = field; } if (cgrass > cgrassMax) { cgrassMax = cgrass; istepGrassMax = istep; fieldGrassMax = field; } if (cgrass < cgrassMin) { cgrassMin = cgrass; istepGrassMin = istep; fieldGrassMin = field; } if (cfox > cfoxMax) { cfoxMax = cfox; istepFoxMax = istep; fieldFoxMax = field; } if (cfox < cfoxMin) { cfoxMin = cfox; istepFoxMin = istep; fieldFoxMin = field; } solwrt.WriteLine("{0} {1} {2}", cgrass, crabbit, cfox); } solwrt.WriteLine("Minimum number of grass: {0} after step {1}.", cgrassMin, istepGrassMin); solwrt.WriteLine("Maximum number of grass: {0} after step {1}.", cgrassMax, istepGrassMax); solwrt.WriteLine("Minimum number of rabbits: {0} after step {1}.", crabbitMin, istepRabbitMin); solwrt.WriteLine("Maximum number of rabbits: {0} after step {1}.", crabbitMax, istepRabbitMax); solwrt.WriteLine("Minimum number of foxes: {0} after step {1}.", cfoxMin, istepFoxMin); solwrt.WriteLine("Maximum number of foxes: {0} after step {1}.", cfoxMax, istepFoxMax); } SaveField(fieldGrassMin, FpatOut.Replace(".out", "-1.png")); SaveField(fieldGrassMax, FpatOut.Replace(".out", "-2.png")); SaveField(fieldRabbitMin, FpatOut.Replace(".out", "-3.png")); SaveField(fieldRabbitMax, FpatOut.Replace(".out", "-4.png")); SaveField(fieldFoxMin, FpatOut.Replace(".out", "-5.png")); SaveField(fieldFoxMax, FpatOut.Replace(".out", "-6.png")); }
public T Fetch <T>() { return(Pparser.Fetch <T>()); }
public List <T> FetchN <T>(int cnode) { return(Pparser.FetchN <T>(cnode)); }
public bool FEof() { return(Pparser.FEof()); }