今天莫名不想说话。

结果发现效率挺高?

poj3322 本来可以1a的。。发现我宽搜写的是head<=tail而且初始是head=1,tail=2如果是多组数据简直就gg了。基础不牢固

这题虽然看起来麻烦,但是实际上仔细思考一下是不难推出对于各种不同放在地图上的方式分别表示的。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int dx[]={-,,,};
const int dy[]={,-,,}; int n,m;
char ss[][];
struct node
{
int x,y,l,c;
}list[];
bool check(int x,int y){return <x&&x<=n&&<y&&y<=m&&ss[x][y]!='#';} bool v[][][];
int main()
{
int stx,sty,stl,edx,edy,edl;bool sb;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break; sb=false;
for(int i=;i<=n;i++)scanf("%s",ss[i]+);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(ss[i][j]=='X'&&sb==false)
{
sb=true;
stx=i, sty=j;
if(ss[i+][j]=='X')stl=;
else if(ss[i][j+]=='X')stl=;
else stl=;
}
else if(ss[i][j]=='O') edx=i, edy=j;
}
memset(v,false,sizeof(v));
for(int i=;i<=;i++)list[i].x=list[i].y=list[i].l=list[i].c=;
list[].x=stx;list[].y=sty;list[].l=stl;list[].c=;
int head=,tail=;
while(head<=tail)
{
if(list[head].x==edx&&list[head].y==edy&&list[head].l==)
{
printf("%d\n",list[head].c);
break;
} for(int k=;k<=;k++)
{
node tno=list[head];tno.c++;
if(tno.l==)
{
tno.x+=dx[k]<?dx[k]*:dx[k];
tno.y+=dy[k]<?dy[k]*:dy[k];
tno.l=dx[k]==?:;
}
else if(tno.l==)
{
tno.x+=dx[k]>?dx[k]*:dx[k];
tno.y+=dy[k];
tno.l=dx[k]==?:;
}
else if(tno.l==)
{
tno.x+=dx[k];
tno.y+=dy[k]>?dy[k]*:dy[k];
tno.l=dy[k]==?:;
} if(check(tno.x,tno.y))
{
if(tno.l==&&ss[tno.x][tno.y]=='E')continue;
if(tno.l==&&!check(tno.x+,tno.y))continue;
if(tno.l==&&!check(tno.x,tno.y+))continue;
if(v[tno.x][tno.y][tno.l]==false)
{
v[tno.x][tno.y][tno.l]=true;
list[tail]=tno;
tail++;
}
}
}
head++;
}
if(!v[edx][edy][])printf("Impossible\n");
}
return ;
}

poj3322

bzoj2252 又是gb权限题。。可以说是相当简单,让1去找0,直到全图被访问过

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int dx[]={-,,,};
const int dy[]={,-,,}; struct node
{
int x,y;
}list[];
int d[][]; char ss[];
int main()
{
int n,m,cnt;
scanf("%d%d",&n,&m);cnt=n*m; int head=,tail=;
memset(d,-,sizeof(d));
for(int i=;i<=n;i++)
{
scanf("%s",ss+);
for(int j=;j<=m;j++)
{
if(ss[j]=='')
{
d[i][j]=;
list[tail].x=i,list[tail].y=j;
tail++;
}
}
} while(head<=tail)
{
int x=list[head].x,y=list[head].y;
for(int k=;k<=;k++)
{
int tx=x+dx[k],ty=y+dy[k];
if(<tx&&tx<=n&&<ty&&ty<=m&&d[tx][ty]==-)
{
d[tx][ty]=d[x][y]+;
list[tail].x=tx,list[tail].y=ty;
tail++;
}
}
head++;
} for(int i=;i<=n;i++)
{
for(int j=;j<m;j++)
printf("%d ",d[i][j]);
printf("%d\n",d[i][m]);
}
return ;
}

bzoj2252

poj1475 码量大而且麻烦,这种题是需要沉下心做的。先bfs让人到箱子周边,再让箱子bfs,实际上就是bfs套bfs,人和箱子绑成一块,记得算答案是有四种不同情况的。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int dx[]={-,,,};
const int dy[]={,-,,};
const char ch[]={'N','W','S','E'}; int n,m;
char sc[][];
struct node
{
int x,y,k;
}list[];
int d[][][],f[][][];
char ss[][][][]; int xx[],yy[];
int E[][];
char sd[][][];
bool check(int x,int y){return <x&&x<=n&&<y&&y<=m&&sc[x][y]!='#';}
int getdis(int stx,int sty,int edx,int edy,int k)
{
memset(E,-,sizeof(E));
xx[]=stx,yy[]=sty;E[stx][sty]=;
int head=,tail=;
while(head<tail)
{
int x=xx[head],y=yy[head];
if(x==edx&&y==edy)return E[x][y];
for(int i=;i<=;i++)
{
int tx=x+dx[i],ty=y+dy[i];
if(check(tx,ty)&&(tx!=edx+dx[k]||ty!=edy+dy[k])&&E[tx][ty]==-)
{
for(int o=;o<=E[x][y];o++)sd[tx][ty][o]=sd[x][y][o];
sd[tx][ty][E[x][y]+]=ch[i]+;
E[tx][ty]=E[x][y]+; xx[tail]=tx,yy[tail]=ty;
tail++;
}
}
head++;
}
return -;
}
int main()
{
// freopen("a.out","w",stdout);
int T_T=;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break;
printf("Maze #%d\n",++T_T); int stpx,stpy,stbx,stby,edx,edy;
for(int i=;i<=n;i++)
{
scanf("%s",sc[i]+);
for(int j=;j<=m;j++)
if(sc[i][j]=='S') stpx=i, stpy=j;
else if(sc[i][j]=='B') stbx=i, stby=j;
else if(sc[i][j]=='T') edx=i, edy=j;
} int head=,tail=;
memset(d,-,sizeof(d));
for(int i=;i<=;i++)
{
int ux=stbx+dx[i],uy=stby+dy[i];
if(check(ux,uy))
{
int dis=getdis(stpx,stpy,ux,uy,(i+)%);
if(dis==-)continue; for(int o=;o<=dis;o++)ss[stbx][stby][i][o]=sd[ux][uy][o];
d[stbx][stby][i]=;
f[stbx][stby][i]=dis; list[tail].x=stbx,list[tail].y=stby,list[tail].k=i;
tail++;
}
}
bool bk=false;int id=-,cnt=;
while(head<tail)
{
int x=list[head].x,y=list[head].y,k=list[head].k;
if(x==edx&&y==edy)
{
if(id==-||(d[x][y][k]<d[x][y][id]||(d[x][y][k]==d[x][y][id]&&f[x][y][k]<f[x][y][id])))
{
id=k;
cnt++;if(cnt==)break;
bk=true;
}
} int px=x+dx[k],py=y+dy[k];
for(int i=;i<=;i++)
{
int ux=x+dx[i],uy=y+dy[i];
int tx=x-dx[i],ty=y-dy[i];
if(check(tx,ty)&&check(ux,uy)&&(d[tx][ty][i]==-||d[x][y][k]+==d[tx][ty][i]))
{
int dis=getdis(px,py,ux,uy,(i+)%);
if(dis==-||(d[x][y][k]+==d[tx][ty][i]&&f[x][y][k]+dis>=f[tx][ty][i]))continue; int tot=d[x][y][k]+f[x][y][k];
for(int o=;o<=tot;o++)ss[tx][ty][i][o]=ss[x][y][k][o];
for(int o=;o<=dis;o++)ss[tx][ty][i][tot+o]=sd[ux][uy][o];
ss[tx][ty][i][tot+dis+]=ch[(i+)%]; d[tx][ty][i]=d[x][y][k]+;
f[tx][ty][i]=f[x][y][k]+dis; list[tail].x=tx,list[tail].y=ty,list[tail].k=i;
tail++;
}
}
head++;
}
if(bk==false)printf("Impossible.\n\n");
else
{
for(int o=;o<=d[edx][edy][id]+f[edx][edy][id];o++)printf("%c",ss[edx][edy][id][o]);
printf("\n\n");
}
}
return ;
}

poj1475

0x25 广度优先搜索的更多相关文章

  1. 图的广度优先搜索(BFS)

    把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...

  2. 广度优先搜索(BFS)

    定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...

  3. 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较

    广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...

  4. ACM题目————图的广度优先搜索

    题目描述 图的广度优先搜索类似于树的按层次遍历,即从某个结点开始,先访问该结点,然后访问该结点的所有邻接点,再依次访问各邻接 点的邻接点.如此进行下去,直到所有的结点都访问为止.在该题中,假定所有的结 ...

  5. SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  6. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

随机推荐

  1. 前端分页功能实现(PC)

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>加 ...

  2. Scrapy日志等级以及请求传参

    日志等级 请求传参 提高scrapy的爬取效率 日志等级 - 日志信息:   使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: - 日志信息的种类: - ERROR ...

  3. Mvc NuGet 数据迁移

    网上有很多的ef  code first 的使用的方式,很乱,下面是我自己整理出来的,有什么不正确的地方还请指正,本人菜鸟一枚! 1.新建一个类库 =>引用 右击 管理NuGet程序包 添加En ...

  4. 使用Micrisoft.net设计方案 第一章 企业解决方案中构建设计模式

    第一章企业解决方案中构建设计模式 我们知道的系统总是由简单到复杂,而不是直接去设计一个复杂系统.如果直接去设计一个复杂系统,结果最终会导致失败.在设计系统的时候,先设计一个能够正常工作的系统,然后在此 ...

  5. CSS vs. JS Animation: 哪个更快

    CSS vs. JS Animation: 哪个更快? CSS vs. JS Animation: 哪个更快? 基于JavaScript的动画竟然已经默默地比CSS的transition动画快了?而且 ...

  6. [OpenWrt]安装mjpg-streamer

    安装mjpg-streamer 远程监控基本上是wifi小车的一个必备功能了.摄像头我用的是奥尼百脑通 D881,这个要100左右. 确认安装了以下软件: kmod-usb2 kmod-video-u ...

  7. Splash Screen(短时间弹出框,信息显示一次)

    原文引自codeproject site, http://www.codeproject.com/Articles/6511/Transparent-Splash-Screen 1.A splash ...

  8. 数据的图表统计highcharts

    数据统计常用的图表一般是饼状图.柱状图.线状图,HighCharts可以很好的实现. HighCharts highcharts是基于jquery的一个功能强大的插件,使用时先导入jquery.js ...

  9. Project Euler 21 Distinct primes factors( 整数因子和 )

    题意: 记d(n)为n的所有真因数(小于n且整除n的正整数)之和. 如果d(a) = b且d(b) = a,且a ≠ b,那么a和b构成一个亲和数对,a和b被称为亲和数. 例如,220的真因数包括1. ...

  10. [SDOI2016]数字配对(费用流+贪心+trick)

    重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...