一道写了很长时间的题,后来发现错误的不是搜索过程而是最后输出的,悲剧了
http://59.69.128.200/JudgeOnline/problem.php?pid=10
#include <iostream> #include<cstdio> #include<string.h> #include<queue> using namespace std; int R,C; int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}}; struct stu { int x,y,h; int num; }mat[105][105]; int t; //保存最大的num void bfs(stu st) { queue<stu> Q; st.num=0; Q.push(st); stu fr; while(!Q.empty()) { fr=Q.front(); Q.pop(); for(int i=0;i<4;i++) { if(mat[fr.x][fr.y].num>t) //如果当前的值大于最大的则更新 t=mat[fr.x][fr.y].num; int x=fr.x+dir[i][0],y=fr.y+dir[i][1]; if(x<1||y<1||x>R||y>C||fr.h>=mat[x][y].h) continue; if(fr.num+1>=mat[x][y].num) { mat[x][y].num=fr.num+1; Q.push(mat[x][y]); } } } } int main() { //freopen("1.txt","r",stdin); int n; scanf("%d",&n); while(n--) { t=0; scanf("%d%d",&R,&C); for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) { scanf("%d",&mat[i][j].h); mat[i][j].x=i; mat[i][j].y=j; mat[i][j].num=0; } for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) bfs(mat[i][j]); printf("%d\n",t+1); } }