HDOJ 1010 JAVA 暴力 + 奇偶剪枝

我写的究极垃圾的DFS hdoj 1010 java

【 奇偶剪枝】【细节】【千万不能bfs】


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

public class Main
{
	public static int move[][] =
	{
			{ 0, 1 },
			{ 0, -1 },
			{ 1, 0 },
			{ -1, 0 } };
	public static char arr[][] = new char[50][50];
	public static boolean ok;
	public static boolean map[][] = new boolean[50][50];

	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext())
		{
			for (int i = 0; i < arr.length; i++)
			{
				for (int j = 0; j < arr[i].length; j++)
				{
					arr[i][j] = 'X';
					map[i][j] = false;
				}
			}
			ok = false;
			int a, b, c;
			a = sc.nextInt();
			b = sc.nextInt();
			c = sc.nextInt();
			if (a == 0 && b == 0 & c == 0)
			{
				return;
			}
			int x, y, x1, y1;
			x = 0;
			y = 0;
			x1 = 0;
			y1 = 0;

			for (int i = 0; i < a; i++)
			{
				char g[] = sc.next().toCharArray();
				for (int j = 0; j < g.length; j++)
				{
					arr[i][j] = g[j];
				}
				for (int j = 0; j < b; j++)
				{
					if (arr[i][j] == 'S')
					{
						x = i;
						y = j;
					}
					if (arr[i][j] == 'D')
					{
						x1 = i;
						y1 = j;
					}
				}
			}

			///
			map[x][y] = true;
			dfs(x, y, 0, c, a, b, x1, y1);
			if (a * b < c)
			{
				System.out.println("NO");
			} else
			{

				if (ok == true)
				{
					System.out.println("YES");
				} else
				{
					System.out.println("NO");
				}
			}
		}

	}

	public static void dfs(int x, int y, int step, int all, int a, int b, int x1, int y1)
	{
		if (arr[x][y] == 'D' && step == all)
		{
			ok = true;
			return;
		}
		if (step > all)
		{
			return;
		}
if (ok==true)
{
	return;
}
		if ((all - (Math.abs(x - x1) + Math.abs(y - y1)) - step) % 2 != 0)
		{
			return;
		}

		for (int i = 0; i < 4; i++)
		{
			int xx = x + move[i][0];
			int yy = y + move[i][1];

			if (xx >= 0 && xx < a && yy >= 0 && yy < b && step <= all && map[xx][yy] != true && arr[xx][yy] != 'X')
			{
				map[xx][yy] = true;
				dfs(xx, yy, step + 1, all, a, b, x1, y1);
				map[xx][yy] = false;
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值