2015轻院校赛 B 迷宫 (bfs)
http://acm.zznu.edu.cn/problem.php?id=1967
这套题的有毒 我交了好多遍才对
坑:机关要按照顺序走 并且在走这个机关之前不能走这个机关 但是能穿过这个机关 所以不能把机关刚开始设成墙
要设成其他符号
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath> using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 110 struct node
{
int x,y,step;
}e[N]; char maps[N][N];
int n,m;
int dis[][]={{,},{-,},{,},{,-},{-,-},{,},{-,},{,-}};
int vis[N][N]; int IF(int x,int y,int nx,int ny,int k)
{
if(maps[x][y]=='*' || maps[x][y]=='#' || x< || x>=n || y< || y>=m || vis[x][y]==)
return ;
if(k>=)
{
if(k==)
{
if(maps[x][y+]=='#'&&maps[x+][y]=='#')
return ;
}
if(k==)
{
if(maps[x][y-]=='#' && maps[x-][y]=='#')
return ;
}
if(k==)
{
if(maps[x][y-]=='#' && maps[x+][y]=='#')
return ;
}
if(k==)
{
if(maps[x][y+]=='#' && maps[x-][y]=='#')
return ;
}
}
return ;
} int bfs(node s,node o)
{
memset(vis,);
queue <node>Q;
Q.push(s);
vis[s.x][s.y]=;
while(Q.size())
{
node p,q;
p=Q.front();
Q.pop();
if(p.x==o.x && p.y==o.y)
return p.step;
for(int i=;i<=;i++)
{
q.x=p.x+dis[i][];
q.y=p.y+dis[i][];
if(IF(q.x,q.y,p.x,p.y,i)==)
{
q.step=p.step+;
Q.push(q);
vis[q.x][q.y]=;
}
}
}
return -;
} int main()
{
int T,k;
scanf("%d",&T);
while(T--)
{
int flag=;
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<n;i++)
{
scanf("%s",maps[i]);
}
for(int i=;i<=k;i++)
{
scanf("%d %d",&e[i].x,&e[i].y);
e[i].step=;
if(i!= && i!=)
{
if(e[i].x==e[].x && e[i].y==e[].y)
flag=;
}
}
if(flag==)
{
printf("-1\n");
continue;
}
int ans=,sum=;;
for(int i=;i<=k;i++)
{
for(int j=i+;j<=k;j++)
{
maps[e[j].x-][e[j].y-]='*';
}
node S,E;
S.x=e[i-].x-;
S.y=e[i-].y-;
S.step=;
E.x=e[i].x-;
E.y=e[i].y-;
E.step=;
ans=bfs(S,E);
if(ans==-)
{
sum=-;
break;
}
else
sum+=ans;
for(int j=i+;j<=k;j++)
{
maps[e[j].x-][e[j].y-]='.';
}
}
printf("%d\n",sum);
}
return ;
}
2015轻院校赛 B 迷宫 (bfs)的更多相关文章
- 2015轻院校赛 D 社交网络(排列组合)
http://acm.zznu.edu.cn/problem.php?id=1964 题目描述 输入 输出 样例输入 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 样例输出 ...
- 2015北京网络赛 G Boxes BFS+打表
G Boxes 题意:n个位置摆有n个箱子,每次移动只能把相邻的垒起来,且上面的必须小于下面的.求摆成升序需要移动多少步. 思路:这里的n很小,只有7.但是bfs最快的情况需要2s左右,所以就打表了. ...
- 【DFS+堆的二叉树结构】15轻院校赛-J-堆
[题目链接:J-堆] 1734: 堆 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 239 Solved: 113 SubmitStatusWeb B ...
- 【二叉树、堆】15轻院校赛-J-堆
原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 ...
- 轻院校赛-zzuli 2266: number【用每位的二进制的幂的和来进行hash(映射)处理】
zzuli 2266: number 大致题意: 给定n,问有多少数对<x, y>满足: x, y∈[1, n], x < y x, y中出现的[0, 9] ...
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
随机推荐
- Implicit Animations 默认动画 读书笔记
Implicit Animations 默认动画 读书笔记 Do what I mean, not what I say. Edna Krabappel, The Simpsons Part I ...
- Centos 6 安装python2.7.6
centos 是自带python的.但是版本稍微旧一些.搞python开发,肯定要用新一点的稳定版.所以,要升级一下python. 先去python主站下载python的源码包:Python-2.7. ...
- jQuery ajax参数后台获取不到的问题
<script type="text/javascript"> init(); var alldate = {a : "0",b:"1&q ...
- 打开centos直接进入文本模式命令行
2.打开/etc/inittab 文件 #vim /etc/inittab3.在默认的 run level 设置中,可以看到第一行书写如:id:5:initdefault:(默认的 run level ...
- 50个Bootstrap扩展插件
Bootstap这个框架本身已经包含了开发网页的众多要素,包括了常用的工具以及扩展组件,如果你在开发页面时觉得在某些方面还不够的话,不妨看看最新收集的50个Bootstrap扩展插件,这些插件在我们平 ...
- dropuser - 删除一个 PostgreSQL 用户帐户
SYNOPSIS dropuser [ option...] [ username] DESCRIPTION 描述 dropuser 删除一个现有 PostgreSQL 用户 和 该用户所有的数据库. ...
- js 时间戳 随机数 new Date().getTime()
一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new ...
- js 跨域深入理解与解决方法
参考博客: https://www.cnblogs.com/2050/p/3191744.html
- MyBatis基本运行环境
MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...
- 从零开始--系统深入学习Android
http://www.cnblogs.com/tianjian/category/354587.html