[POJ 1101]The Game Bfs
2015-11-05 08:20
253 查看
题目大意
求bfs的线段拐点数只有细节!!!
代码
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <cstdlib> using namespace std; int w,h,mat[500][500],x1,y1,x2,y2,dir[4][2]={{1,0}/*down*/,{-1,0}/*up*/,{0,-1}/*left*/,{0,1}/*right*/},vis[500][500],cnt1=1,cnt2=1; char t[500][500]; struct Holder{ int x,y,cnt; }st,now,next,s,ed; int Bfs(){ memset(vis,0,sizeof(vis)); queue<Holder> Q; Q.push(s);vis[s.x][s.y]=1; while(!Q.empty()){ now=Q.front();Q.pop(); if(now.x==ed.x&&now.y==ed.y&&now.cnt<ed.cnt){ ed.cnt=now.cnt; } for(int i=0;i<4;i++){ next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; while(mat[next.x][next.y]==1&&next.x>=0&&next.x<=h+1&&next.y>=0&&next.y<=w+1&&vis[next.x][next.y]==0){ vis[next.x][next.y]=1; next.cnt=now.cnt+1; Q.push(next); next.x+=dir[i][0]; next.y+=dir[i][1]; } } } } void Initialize(){ memset(mat,0,sizeof mat); memset(vis,0,sizeof vis); memset(t,0,sizeof(t)); } int main(){ while(scanf("%d%d",&w,&h)!=EOF){ if(w==0&&h==0){ return 0; } Initialize(); for(int i=1;i<=h;++i){ cin.get(); for(int j=1;j<=w; ++j) cin.get(t[i][j]); } for(int i=0;i<=h+1;i++){ for(int j=0;j<=w+1;j++){ if(t[i][j]=='X'){ mat[i][j]=10; }else mat[i][j]=1; } } cout<<"Board #"<<cnt1++<<":"<<endl;cnt2=1; while(1){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==0&&y1==0&&x2==0&&y2==0){ break; } s.x=y1;s.y=x1;ed.x=y2;ed.y=x2; s.cnt=0;ed.cnt=1<<20; int flag=0; if(mat[y2][x2]==10){ mat[y2][x2]=1; flag=1; } Bfs(); if(flag==1){ mat[y2][x2]=10; } if(ed.cnt!=1<<20){ cout<<"Pair "<<cnt2++<<": "<<ed.cnt<<" segments."<<endl; }else{ cout<<"Pair "<<cnt2++<<": impossible."<<endl; } } cout<<endl; } }
相关文章推荐
- Win10 th2正式版Build 10586怎么更新升级?
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
- 为代码添加权限检查
- 浏览器并发处理请求个数一览表
- Spring相框
- VC 使用OnCtlColor函数来改变控件颜色(引用)
- MySQL存储过程传参之in,out,inout参数用法
- C/C++语言中关于const用法的总结
- Win10 Mobile正式版10586上手视频曝光
- Heartbeat 与Corosync对比分析
- Openjudge p666:放苹果
- google play 924 错误
- 人体动作识别(一)
- MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类
- Android权限学习之请求权限实例
- Magento 修改首页 New Products 列数
- OpenJudge3531 判断整除
- LeetCode Candy
- 男人一生的精子变化
- 【转】推荐给大家的7本游戏开发书