BFS【bzoj1667】: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛
1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛
Description
经过跟Farmer John长达数年的谈判,奶牛们终于如愿以偿地得到了想要的旱冰鞋。农场上大部分的区域都很平整,适合在上面滑动,但有一些小块的土地上有很多的岩石,凭奶牛们的旱冰技术,是没有办法通过的。 农场可以看成一个被划分成R(1<=R<=113)行C(1<=C<=77)列的矩阵。快要开饭了,贝茜发现自己在坐标为(1,1)的格子里,并且她想赶到坐标为(R,C)的牛棚去享用她的晚饭。贝茜知道,以她所在的格子为起点,每次向上、下、左、右滑动(但不能沿对角线滑动),一定能找到一条通往牛棚的、不经过任何有大量岩石的格子的路。请你为她指出任意一条通往牛棚的路径。
妈的傻逼题做这么长时间,真TM菜。
一开始写Dij写挂了,现在回想就是数组开小了,之后写大法师还T。
正解BFS记录路径,当一个模板记。
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
const int wx=517;
struct node{
int x,y;
};
int dx[]={0,1,0,-1,0};
int dy[]={0,0,1,0,-1};
int vis[wx][wx];
int a[wx][wx],ansx[wx],ansy[wx];
char c[wx];
int prex[wx][wx];int prey[wx][wx];
int n,m,ans,num,tot,cnt;
int xx[wx],yy[wx],dis[wx][wx];
queue<node >q;
void bfs(){
memset(dis,0x3f,sizeof dis);
dis[1][1]=0; q.push((node){1,1});
while(q.size()){
int ux=q.front().x; int uy=q.front().y;
q.pop();
for(int i=1;i<=4;i++){
int ex=ux+dx[i]; int ey=uy+dy[i];
if(ex<1||ex>n||ey<1||ey>m||a[ex][ey])continue;
if(dis[ex][ey]>dis[ux][uy]+1){
dis[ex][ey]=dis[ux][uy]+1;
q.push((node){ex,ey});
prex[ex][ey]=ux; prey[ex][ey]=uy;
}
}
}
}
int main(){
n=read(); m=read();
for(int i=1;i<=n;i++){
scanf("%s",c+1);
for(int j=1;j<=m;j++){
a[i][j]=(c[j]=='*');
}
}
bfs();
int nowx=n; int nowy=m;
while(nowx){
ansx[++tot]=nowx; ansy[tot]=nowy;
int tmp=nowx;
nowx=prex[tmp][nowy]; nowy=prey[tmp][nowy];
}
for(int i=tot;i>=1;i--){
printf("%d %d\n",ansx[i],ansy[i]);
}
return 0;
}
BFS【bzoj1667】: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛的更多相关文章
- BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛
1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 665 Solved: 419 ...
- BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )
裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...
- BZOJ1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5 ...
- BZOJ1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富
1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 459 Sol ...
- BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )
dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...
- 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富
1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 498 Sol ...
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Description 为了防止 ...
- bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 387 Sol ...
- BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包
BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...
随机推荐
- [iOS]UIImageView增加圆角
[iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d ...
- Monitoring tools that everyone's currently using
Although a lot of new tools have arrived since 2011, it's clear that older open source tools like Na ...
- mac 下 配置appium +ios真机环境
mac系统:10.11.6 xcode:7 appium:1.5.3 iphone: 6 p 1.搭建 appium 安卓的环境: 1.jdk 2.sdk 3.appium 4.配置环境变量 mac下 ...
- Ettercap进行arp毒化
攻击者IP:192.168.220.152 受害者IP:192.168.220.151 网关:192.168.220.2 修改DNS文件 ┌─[root@sch01ar]─[~] └──╼ #vim ...
- Delphi IOS (二)
1.Mac 中 simulator模拟器Home快捷键:command(Win键盘,Ctrl与Alt之间的键)+shift+h来代替,也可以点击菜单>HardWare>Home 2.iPh ...
- openGL 预定义变量04
OpenGL4.0 GLSL预定义变量 GLSL为不同的渲染阶段定义了一些特定的变量.这些预定义(也叫做内置变量)有特定的属性. 所有的预定义变量都以gl_开头.用户定义的变量不能以此开头. 下面分类 ...
- C#操作JSON专题
第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...
- 【WebRTC】术语
WebRTC,名称源自网页实时通信(英语:Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的API.它于2011年6月1日开源并在Goog ...
- 【转】TinyMCE(富文本编辑器)
效果预览:http://www.tinymce.com/tryit/full.php [转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 转自:http://www.cnblogs.co ...
- setAttribute这个方法
setAttribute这个方法,在JSP内置对象session和request都有这个方法,这个方法作用就是保存数据,然后还可以用getAttribute方法来取出.比如现在又个User对象,Use ...