【 奇偶剪枝】【细节】【千万不能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;
}
}
}
}