zoj3890 BFS
就是搜。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 22
int fang[][]={{,},{,},{,-},{-,}};
int map[maxn][maxn],vis[maxn][maxn][][],n;
struct node
{
int count;//计数
int way;//方向
int x;
int y;
int flag;//是否拿到金子
};
int ok(int x,int y)
{
if(x<||x>=n||y<||y>=n||map[x][y]==||map[x][y]==)
return ;
return ;
}
int bfs()
{
int i,j;
queue<node>q;
node temp,t;
memset(vis,,sizeof(vis));
temp.x=temp.y=temp.flag=temp.count=;
temp.way=;
q.push(temp);
vis[][][][]=;
while(!q.empty())
{
temp=q.front();
q.pop();
if(temp.count>)
return -;
if(map[temp.x][temp.y]==&&!temp.flag)
{
temp.count++;
temp.flag=;
vis[temp.x][temp.y][temp.way][temp.flag]=;
q.push(temp);
continue;
}
if(temp.x==&&temp.y==&&temp.flag)
{
return temp.count+;
}
for(i=-;i<;i++)
{
if(i==)
{
t.count=temp.count+;
t.flag=temp.flag;
t.way=temp.way;
t.x=temp.x+fang[t.way][];
t.y=temp.y+fang[t.way][];
}
else
{
t.count=temp.count+;
t.flag=temp.flag;
t.way=temp.way+i+;
t.way%=;
t.x=temp.x;
t.y=temp.y;
}
if(!vis[t.x][t.y][t.way][t.flag]&&ok(t.x,t.y))
{
vis[t.x][t.y][t.way][t.flag]=;
q.push(t);
}
}
}
return -;
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(map,,sizeof(map));
while()
{
int x,y,z;
scanf("%d%d%d",&z,&x,&y);
if(z==-&&y==-&&x==-)
break;
map[x][y]=z;
}
if(map[][]==)
{
printf("-1\n");
continue;
}
int ans=bfs();
if(ans==-)
printf("-1\n");
else if(-*ans<)
printf("-1\n");
else
printf("%d\n",-*ans);
}
}
/*
1
2
3 1 1
-1 -1 -1
*/
zoj3890 BFS的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- poj1274 The Perfect Stall (二分最大匹配)
Description Farmer John completed his new barn just last week, complete with all the latest milking ...
- 2014 Super Training #6 A Alice and Bob --SG函数
原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...
- Android Studio中提示:Project SDK is not defined
Android Studio中提示:Project SDK is not defined 2015 年 4 月 1 日 下午 9:04crifan已有2209人围观我来说几句 [背景] 之前用Andr ...
- emberjs重写补充类之reopen方法和reopenClass方法
无需一次性将类定义完全,你可以使用reopen方法来重新打开(reopen)一个类并为其定义新的属性. Person.reopen({ isPerson: true }); Person.create ...
- mysql及php命名规范
一.mysql命名规范 1.设计原则 1) 标准化和规范化数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完 ...
- 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结
小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...
- PHP通用函数 - 日期生成时间轴
/** * 时间轴函数, Unix 时间戳 * @param int $time 时间 */ function TranTime($time) { //$time = strtotime($time) ...
- 解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法
1.开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: reconfigure reconfigure 2.关闭Ad Hoc Distributed Qu ...
- Linux经常用到的命令
1. Linux下用vim打开配置文件乱码,在终端输入:“LANG=”即可. 2. 查看端口是否被占用: 3. netstat -anp | grep port netstat -ltn 4. lso ...
- JS 之DOM range对象
DOM范围 DOM中的range对象是DOM2中新定义的接口.通过这个对象可以选择文档中的某个区域,而不必考虑节点的界限. 创建范围 document.createRange()创建一个范围,这个范围 ...