bzoj3393 [Usaco2009 Jan]Laserphones 激光通讯
来源:互联网 发布:淘宝体检中心不清洗 编辑:程序博客网 时间:2024/05/21 08:00
分析:这题目挺经典的。。求最小拐点数,这个好像印象中遇见了很多次?
直接spfa就好了,但是注意储存方向,四个方向都有可能,所以都要算上。。
#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)using namespace std;int n,m;struct node{ int x,y,dir,dis;}now,next;bool operator <(const node&a,const node&b){ return a.dis>b.dis;}priority_queue <node>q;const int dx[4]={0,1,0,-1};const int dy[4]={1,0,-1,0};int sx,sy,ex,ey;bool bz[105][105];int dis[110][110][4];int main(){ scanf("%d%d",&m,&n); fo(i,1,n) { fo(j,1,m) { char ch=getchar(); while (ch!='C'&&ch!='.'&&ch!='*')ch=getchar(); if (ch=='*')bz[i][j]=1; if (ch=='C') { if (!sx) { sx=i,sy=j; } else ex=i,ey=j; } } } memset(dis,127,sizeof(dis)); now.x=sx,now.y=sy,now.dis=0; fo(i,0,3) { now.dir=i; q.push(now); dis[sx][sy][i]=0; } while (!q.empty()) { now=q.top(); q.pop(); int k=now.dir; next=now; while (next.x+dx[k]>=1&&next.x+dx[k]<=n&&next.y+dy[k]>=1&&next.y+dy[k]<=m&&!bz[next.x+dx[k]][next.y+dy[k]]&&dis[next.x+dx[k]][next.y+dy[k]][k]>next.dis) { next.x+=dx[k];next.y+=dy[k]; dis[next.x][next.y][k]=dis[now.x][now.y][k]; q.push(next); } next=now; next.dis++; fo(k,0,3) if (dis[now.x][now.y][k]>now.dis+1) { dis[now.x][now.y][k]=now.dis+1; next.dir=k; q.push(next); } } int ans=1000000000; fo(k,0,3) ans=min(ans,dis[ex][ey][k]); printf("%d\n",ans);}
0 0
- bzoj3393 [Usaco2009 Jan]Laserphones 激光通讯
- bzoj 3393: [Usaco2009 Jan]Laserphones 激光通讯
- BZOJ 1574:[Usaco2009 Jan]地震损坏Damage
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- 【BZOJ 1576】 [Usaco2009 Jan]安全路经Travel
- 【bzoj1576】[Usaco2009 Jan]安全路经Travel
- bzoj 3396 [Usaco2009 Jan]Total flow 水流
- 【BZOJ3396】【Usaco2009 Jan】Total flow 水流
- 1574: [Usaco2009 Jan]地震损坏Damage
- BZOJ1575 [Usaco2009 Jan]气象牛Baric
- BZOJ1576: [Usaco2009 Jan]安全路经Travel
- BZOJ 1574: [Usaco2009 Jan]地震损坏Damage
- BZOJ 1575: [Usaco2009 Jan]气象牛Baric
- BZOJ 1575 [Usaco2009 Jan]气象牛Baric
- bzoj 1576 [Usaco2009 Jan]安全路经Travel
- [bzoj1576] [Usaco2009 Jan]安全路经Travel
- bzoj 1576[Usaco2009 Jan]安全路经Travel
- [BZOJ1576] [Usaco2009 Jan]安全路经Travel
- swing 插件实现简单的文件读取与保存
- “火了”无人车产业
- 第四次作业
- 基于web的IT技术论坛
- java——修饰符
- bzoj3393 [Usaco2009 Jan]Laserphones 激光通讯
- ES5中新增的Array方法
- 3月题外:关于GeoServer和OpenLayers3实用开源插件或组件的总结
- 项目组git合作模板
- rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 )
- 前端基础之五 元素关系及选择器
- 斯坦福机器学习1-3
- spring di
- MySQL之DML(Data Manipulation Languages)