M - 非常可乐 HDU - 1495 java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext())
		{
			int a, b, c;
			a = sc.nextInt();
			b = sc.nextInt();
			c = sc.nextInt();

			if (a == 0 && b == 0 && c == 0)
			{
				return;
			}
			if (a % 2 != 0)
			{
				System.out.println("NO");
			} else
			{

				Queue<cola> deal = new LinkedList<cola>();
				deal.add(new cola(a, 0, 0, 0));
				boolean qd[][][] = new boolean[105][105][105];

				int pp = a / 2;
				boolean nmb = false;
				qd[0][0][c] = true;
				while (!deal.isEmpty())
				{

					cola ok = deal.poll();

					int nb = 0;
					int yes = a / 2;
					if (ok.a == ok.b && ok.a == yes)
						nb++;
					if (ok.b == ok.c && ok.b == yes)
						nb++;
					if (ok.c == ok.a && ok.c == yes)
						nb++;
					if (nb >= 1)
					{
						System.out.println(ok.step);

						nmb = true;
						break;
					}
					//
					int x, y, z, ee;
					x = ok.a;
					y = ok.b;
					z = ok.c;
					ee = ok.step;
					if (ok.a > 0)
					{
						if (y != b)
						{
							if (x + y <= b)
							{
								if (qd[0][x + y][z] == false)
								{

									deal.add(new cola(0, x + y, z, ee + 1));
									qd[0][x + y][z] = true;
								}
							} else
							{
								if (qd[x + y - b][b][z] == false)
								{

									deal.add(new cola(x + y - b, b, z, ee + 1));
									qd[x + y - b][b][z] = true;
								}
							}

						}

						if (z != c)
						{
							if (x + z <= c)
							{
								if (qd[0][y][x + z] == false)
								{

									deal.add(new cola(0, y, x + z, ee + 1));
									qd[0][y][x + z] = true;
								}
							} else
							{
								if (qd[x + z - c][y][c] == false)
								{

									deal.add(new cola(x + z - c, y, c, ee + 1));
									qd[x + z - c][y][c] = true;
								}

							}
						}
					}
					if (ok.b > 0)
					{
						if (y + x <= a)
						{
							if (qd[x + y][0][z] == false)
							{

								deal.add(new cola(x + y, 0, z, ee + 1));
								qd[x + y][0][z] = true;
							}
						} else
						{
							if (qd[a][x + y - a][z] == false)
							{

								deal.add(new cola(a, x + y - a, z, ee + 1));
								qd[a][x + y - a][z] = true;
							}
						}
						if (y + z <= c)
						{
							if (qd[x][0][z + y] == false)
							{

								deal.add(new cola(x, 0, z + y, ee + 1));
								qd[x][0][z + y] = true;
							}

						} else
						{
							if (qd[x][y + z - c][c] == false)
							{

								deal.add(new cola(x, z + y - c, c, ee + 1));
								qd[x][y + z - c][c] = true;
							}
						}

					}
					if (ok.c > 0)
					{
						if (z + x <= a)
						{
							if (qd[x + z][y][0] == false)
							{

								deal.add(new cola(x + z, y, 0, ee + 1));
								qd[x + z][y][0] = true;
							}
						} else
						{
							if (qd[a][y][x + z - a] == false)
							{

								deal.add(new cola(a, y, x + z - a, ee + 1));
								qd[a][y][x + z - a] = true;
							}
						}
						if (z + y <= b)
						{
							if (qd[x][y + z][0] == false)
							{

								deal.add(new cola(x, y + z, 0, ee + 1));
								qd[x][y + z][0] = true;
							}

						} else
						{
							if (qd[x][b][y + z - b] == false)
							{

								deal.add(new cola(x, b, y + z - b, ee + 1));
								qd[x][b][y + z - b] = true;
							}
						}

					}
				}
				if (nmb == false)
				{
					System.out.println("NO");
				}

			}
		}
	}
}

class cola
{
	int a, b, c, step;

	public cola(int a, int b, int c, int step)
	{
		this.a = a;
		this.b = b;
		this.c = c;
		this.step = step;
	}
}

你可能感兴趣的:(dfs,bfs)