NYOJ skiing(BFS)

一道写了很长时间的题,后来发现错误的不是搜索过程而是最后输出的,悲剧了抓狂

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); } }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值