今天上午调试了一道dfs的模板题,这道题目很简单,但是我犯了一些错误,导致他没那么快调试出来。
代码应该是这样的,我有了和这差不多的思路,但是调试出来的结果就一直重复那样例的两个答案,我百思不得其解。
开始没有加那个 i < step就一直输出那两个例子,至于为什么,我还要去理解一下递归的过程。
下午又写了一道bfs的模板题,一开始想的只是练练手,没有想到自己原来这么菜,写了这么久。。。真的感觉自己还有好多没有学的呀,加油加油吧,慢慢来,一道一道题弄懂!!!
#include<iostream>
#include<queue>
using namespace std;
int m , n , flag = 0;
int a[1010][1010],book[1010][1010],b[1010][1010];
int xx,yy;
int main()
{
int next[4][2] = { {0,1},
{1,0},
{0,-1},
{-1,0}
};
queue<int> qh,ql;
cin >> n >> m;
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ )
cin >> a[i][j];
qh.push(1);
ql.push(1);
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m; j ++ )
b[i][j] = 999999999;
b[1][1] = 0;
while(!qh.empty())
{
for(int i = 0;i < 4;i ++ )
{
xx = qh.front() + next[i][0];
yy = ql.front() + next[i][1];
if(xx > n || yy < 1 || xx < 1 || yy > m)
continue;
if(a[xx][yy] == 0 && book[xx][yy] == 0)
{
book[xx][yy] = 1;
b[xx][yy] = min(b[qh.front()][ql.front()] + 1,b[xx][yy]);
qh.push(xx);
ql.push(yy);
}
if(xx == n && yy == m)
{
flag = 1;
break;
}
}
if(flag == 1)
break;
qh.pop();
ql.pop();
}
cout<<b[n][m];
return 0;
}