2.4.2(extra)
来源:互联网 发布:金数据登录网址 编辑:程序博客网 时间:2024/04/29 23:20
/*ID:18861501LANG:C++TASK:maze1*//*------------------Header Files------------------*/#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <cstdlib>#include <ctype.h>#include <cmath>#include <stack>#include <queue>#include <map>#include <vector>#include <limits.h>using namespace std;/*------------------Definitions-------------------*/#define LL long long#define PI acos(-1.0)#define INF 0x3F3F3F3F#define MOD 9901/*---------------------Work-----------------------*/int W,H;char maze[250][100];int cnt[250][100];bool vis[250][100];struct node{int x,y;};queue<node>myqueue;int exit1x,exit1y,exit2x,exit2y;void getanswer(int x,int y){node h,k;cnt[x][y]=1;h.x=x,h.y=y;myqueue.push(h);while(!myqueue.empty()) //用函数递归实现BFS超时,手写队列BFS //还有重要的一点就是从终点BFS,如果从每个点开始BFS太麻烦{h=myqueue.front();myqueue.pop();x=h.x,y=h.y;if (y!=1 && maze[x][y-1]==' ' && cnt[x][y-2]>cnt[x][y]+1) //关键在于最后一个式子,只有满足才走//这条路,否则不走{k.x=x;k.y=y-2;cnt[x][y-2]=cnt[x][y]+1;myqueue.push(k);}if (y!=2*W-1 && maze[x][y+1]==' ' && cnt[x][y+2]>cnt[x][y]+1){k.x=x;k.y=y+2;cnt[x][y+2]=cnt[x][y]+1;myqueue.push(k);}if (x!=1 && maze[x-1][y]==' ' && cnt[x-2][y]>cnt[x][y]+1){k.x=x-2;k.y=y;cnt[x-2][y]=cnt[x][y]+1;myqueue.push(k);}if (x!=2*H-1 && maze[x+1][y]==' ' && cnt[x+2][y]>cnt[x][y]+1){k.x=x+2;k.y=y;cnt[x+2][y]=cnt[x][y]+1;myqueue.push(k);}}}void work(){scanf("%d%d",&W,&H);getchar();for(int i=0; i<=2*H; i++){for(int j=0; j<=2*W; j++)scanf("%c",&maze[i][j]);getchar();}for(int i=1; i<=2*H-1; i+=2)for(int j=1; j<=2*W-1; j+=2)cnt[i][j]=INF;int _count=0;for(int i=1; i<=2*H-1; i+=2){for(int j=1; j<=2*W-1; j+=2){if((maze[i][j-1]==' '&&j-1==0)||(maze[i-1][j]==' '&&i-1==0)||(maze[i][j+1]==' '&&j+1==2*W)||(maze[i+1][j]==' '&&i+1==2*H)){if(_count==0) exit1x=i,exit1y=j;else if(_count==1) exit2x=i,exit2y=j;_count++;}if(_count==2) goto here;}}here:while(!myqueue.empty()) myqueue.pop();getanswer(exit1x,exit1y);getanswer(exit2x,exit2y);int m=0;for(int i=1; i<=2*H-1; i+=2)for(int j=1; j<=2*W-1; j+=2)if(cnt[i][j]>m) m=cnt[i][j];cout<<m<<endl;}/*------------------Main Function------------------*/int main(){//freopen("test.txt","r",stdin);freopen("maze1.out","w",stdout);freopen("maze1.in","r",stdin);work();return 0;}
0 0
- 2.4.2(extra)
- day 2 extra
- 1.3.2(extra)
- 1.4.2(extra)
- 1.5.2(extra)
- Extra
- OpenCV3.2 编译extra & Python模块
- C++ Programming Error/Warning Analysis (2) extra qualification
- json读取数据:ValueError: Extra data: line 77 column 2
- Codeforces Round #450 (Div. 2) C. Remove Extra One 暴力
- Extra Krunch
- day3 extra
- extra-code
- Extra属性
- Extra Krunch
- Extra Workloads
- android漏洞收集2-classes.dex重名、extra filed length溢出
- android漏洞收集2-classes.dex重名、extra filed length溢出
- 自定义html maxlength
- LisetView Item高度设置无效
- ActiveMQ入门实例
- ActiveMQ的断线重连机制
- ActiveMQ配置详解之如何配置自动重新连接
- 2.4.2(extra)
- Android开发实践:使用Service还是Thread
- 图像识别工程师 VS The application has requested the Runtime to terminate it in an unusual way.
- ikanalyzer 词频计算
- 设计模式:原型模式(Prototype)
- BZOJ2588Spoj 10628. Count on a tree
- 字符串搜索算法总结
- 自学编程,十年磨一剑
- Centos7安装共享Samba win访问linux