HOJ3237----BFS/DFS

 /*
注意两点
. 不可以使用替换可用节点为不可用节点的方法进行DFS
因为角落也可能有油,替换了就出不来。(某学长指导)
. 可用通过开一个数组(例如我的b[][]数组)
用了存储到当前位置剩余最大油量
这样到话,下次若有更优解,则更新b
反之无需继续遍历
对于BFS,可用结构体记录坐标和到当前位置到剩余油量
用方法2剪枝即可
以下为DFS方法实现
*/
#include <cstring>
#include <iostream>
using namespace std;
int n, m, l;
char a[][];
int b[][];
int w[][] = {{, }, {, }, { -, }, {, -}};
bool ans;
void dfs(int x, int y, int s)
{
//cout << "x = " << x+1 << " y = " << y+1 << " s = " << s <<" a = " << a[x][y] << endl;
if (x == n - && y == m - ) {ans = true; return;}
if (b[x][y] >= s) return;
if (a[x][y] == '+') s = l;
if (s <= || ans) return;
b[x][y] = s; for (int i = ; i < ; ++i)
{
int nx = x + w[i][], ny = y + w[i][];
if (nx >= && ny >= && nx < n && ny < m && a[nx][ny] != '#')
dfs(nx, ny, s-);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
memset(b, -, sizeof b);
cin >> n >> m >> l;
for (int i = ; i < n; ++i)
cin >> a[i];
ans = false;
dfs(, , l);
if (ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
return ;
}
上一篇:C++——创建类的时候用new与不用new 的区别


下一篇:HDU 3831 DICS