249602026-02-17 10:04:13DominikVizeskannákcsharpRuntime error 0/4014ms3132 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vizeskannak
{
	class Program
	{
		static void Main(string[] args)
		{
			int[] kannak = new int[3];
			string[] data = Console.ReadLine().Split(' ');
			for (int i = 0; i < 3; i++)
			{
				kannak[i] = int.Parse(data[i]);
			}
			int target = int.Parse(Console.ReadLine());
			Queue<int[]> BFS = new Queue<int[]>();
			BFS.Enqueue(new int[4] { kannak[0] ,0,0,0});
			int minSteps = -1;
			if (kannak[0] == target) minSteps = 0;
			HashSet<string> seen = new HashSet<string>();
			while (BFS.Count>0&&minSteps==-1)
			{
				int[] current = BFS.Dequeue();
				for (int i = 0; i < 3; i++)
				{
					if (current[i]!=kannak[i])
					{
						for (int j = 0; j < 3; j++)
						{
							if (i != j)
							{
								int change = Math.Min(kannak[i] - current[i],current[j]);
								current[i] += change;
								current[j] -= change;
								if (current[i] == target || current[j] == target)
								{
									minSteps = current[3]+1;
									break;
								}
								if (!seen.Contains($"{current[0]}#{current[1]}#{current[2]}"))
								{
									BFS.Enqueue(new int[4] { current[0],current[1],current[2],current[3]+1});
									seen.Add($"{current[0]}#{current[1]}#{current[2]}");
								}
								current[i] -= change;
								current[j] += change;
							}
						}
					}
					if (minSteps != -1) break;
				}
			}
			Console.WriteLine(minSteps);
			Console.ReadKey();
		}
	}
}
SubtaskSumTestVerdictTimeMemory
base0/40
1Runtime error0/014ms2756 KiB
2Runtime error0/014ms2796 KiB
3Runtime error0/114ms2784 KiB
4Runtime error0/114ms2852 KiB
5Runtime error0/114ms3104 KiB
6Runtime error0/114ms2488 KiB
7Runtime error0/114ms2776 KiB
8Runtime error0/114ms2576 KiB
9Runtime error0/114ms2880 KiB
10Runtime error0/114ms2768 KiB
11Runtime error0/114ms2772 KiB
12Runtime error0/114ms2768 KiB
13Runtime error0/214ms3132 KiB
14Runtime error0/214ms2760 KiB
15Runtime error0/214ms2796 KiB
16Runtime error0/214ms2844 KiB
17Runtime error0/214ms2964 KiB
18Runtime error0/214ms2704 KiB
19Runtime error0/214ms2816 KiB
20Runtime error0/214ms2864 KiB
21Runtime error0/214ms2524 KiB
22Runtime error0/214ms2884 KiB
23Runtime error0/214ms2832 KiB
24Runtime error0/214ms2972 KiB
25Runtime error0/214ms2740 KiB
26Runtime error0/214ms2492 KiB
27Runtime error0/214ms2576 KiB