BFS:

1. 从起点开始BFS,遇到X点则return;

2. vis[px][py][0]代表经过pxpy这点前还没有找到车;

 vis[px][py][1]代表经过pxpy这点前已经找到车;

3. ip记录是否找到车;

 d表示方向

4. 最后判断时间是否超时;

5. 简单的BFS,结束!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<cmath>
#define test printf("***\n")
#define ka getchar();getchar()
#define ka1 getchar()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int N = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
struct lp {
int x, y,d,ip,step;
friend bool operator <(const lp &a,const lp &b){
if(a.step!=b.step)return a.step>b.step;
return a.ip<b.ip;
}
} now, t;
int n, k;
char ar[N][N];
int dir[][] = {, , -, , , , , -};
bool vis[N][N][];
int bfs(int a, int b)
{
priority_queue<lp>Q;
memset(vis,,sizeof(vis));
t.x = a; t.y = b;
t.d = -;t.ip=;
t.step=;
Q.push(t);
vis[a][b][]=;
while(!Q.empty()) {
t = Q.top(); Q.pop();
for(int i = ; i < ; ++i) {
int px = t.x + dir[i][], py = dir[i][] + t.y;
if(px < || py < || px >= n || py >= n)continue;
if(ar[px][py] == 'O')continue;
if(t.step>k)return ;
if(t.ip == ) {
if(vis[px][py][])continue;
if(t.d != - && t.d != i)continue;
if(t.d != -) {
now.d = -;now.ip=;
now.x = px; now.y = py;
vis[px][py][]=;
now.step = t.step + ;
if(ar[px][py]=='X')return now.step;
if(ar[px][py]=='C'){
now.ip=;
vis[px][py][]=;
}
Q.push(now);
} else {
now.d = i;now.ip=;
now.x = t.x; now.y = t.y;
now.step = t.step + ;
Q.push(now);
}
}else{
if(vis[px][py][])continue;
now.d = i;now.ip=;
now.x = px; now.y = py;
now.step = t.step + ;
if(ar[px][py]=='X')return now.step;
vis[px][py][]=;
Q.push(now);
}
}
}
return ;
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int a, b;
scanf("%d%d", &n, &k);
for(int i = ; i < n; ++i) {
scanf("%s", &ar[i]);
for(int j = ; j < n; ++j) {
if(ar[i][j] == 'S')a = i, b = j;
}
}
int ans = bfs(a, b);
if(ans!=&&ans<=k) {
printf("YES\n%d\n", ans);
} else {
printf("NO\n");
}
}
return ;
}
/*
3
2 3
.X
S.
2 3
.X
SC
2 4
.X
S.
*/

题目:

https://www.nowcoder.com/acm/contest/93/H

2018年东北农业大学春季校赛-wyh的吃鸡的更多相关文章

  1. 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅

    链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...

  2. 牛客网 2018年东北农业大学春季校赛 I题 wyh的物品

    链接:https://www.nowcoder.com/acm/contest/93/I 来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒空间限制:C/C++ 262144K,其他语言5242 ...

  3. 2018年东北农业大学春季校赛 K wyh的数列【数论/斐波那契数列大数取模/循环节】

    链接:https://www.nowcoder.com/acm/contest/93/K来源:牛客网 题目描述 wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F( ...

  4. 2018年东北农业大学春季校赛 I wyh的物品【01分数规划/二分】

    链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...

  5. 2018年东北农业大学春季校赛 F wyh的集合【思维】

    链接:https://www.nowcoder.com/acm/contest/93/F来源:牛客网 题目描述 你们wyh学长给你n个点,让你分成2个集合,然后让你将这n个点进行两两连接在一起,连接规 ...

  6. 2018年东北农业大学春季校赛 D wyh的迷宫【搜索】

    链接:https://www.nowcoder.com/acm/contest/93/D来源:牛客网 题目描述 给你一个n*m的迷宫,这个迷宫中有以下几个标识: s代表起点 t代表终点 x代表障碍物 ...

  7. 2018年东北农业大学春季校赛 B wyh的矩阵【找规律】

    链接:https://www.nowcoder.com/acm/contest/93/B来源:牛客网 题目描述 给你一个n*n矩阵,按照顺序填入1到n*n的数,例如n=5,该矩阵如下 1 2 3 4 ...

  8. 2018年东北农业大学春季校赛 E wyh的集合 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/93/F 思路 其实容易知道在两个不同集合里 假设元素个数 分别为 a b 然后对于第一个集合里的每一个元素 都可以 ...

  9. 2018年东北农业大学春季校赛 E wyh的阶乘 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/93/E 思路 其实就是找阶乘的项中5的个数 末尾为什么会出现0 因为存在5的倍数和偶数相乘 有0存在 借鉴 htt ...

随机推荐

  1. C#多线程编程(6)--线程安全2 互锁构造Interlocked

    在线程安全1中,我介绍了线程同步的意义和一种实现线程同步的方法:volatile.volatile关键字属于原子操作的一种,若对一个关键字使用volatile,很多时候会显得很"浪费&quo ...

  2. Gson解析json字符串、json数组转换成对象

    实体类: public class Product { private int id; private String name; private String date; public int get ...

  3. 伪样式:hover ,:active,:focus

    <!doctype html>无标题文档 #name:focus { background: #0F6 } #password:hover { background: #F00 } #pa ...

  4. Problem : 1013 ( Digital Roots )

    tips:分析不够仔细,白费了许多功夫.输入数据的范围,平时几乎很少考虑的,这个以后得注意.代码检查不够仔细啊,以后得注意了 #include<iostream> using namesp ...

  5. Intellij IDEA查看所有断点

    项目中打的断点太多,有时自己也想不到打在哪里了,也不知道哪些方法.哪些代码行上打了断点,在IDEA中如何查看所有断点呢? 方法如下: step 1 IDEA的debug面板中有一个名称为View Br ...

  6. 你学会UI设计了吗?

    你学会UI设计了吗? UI设计师如何前驱? 关于产品 作为一个UI设计师,我们还在干巴巴的等着产品经理甚至交互提供的需求和原型再开始动手吗?这样被动的工作是永远无法提升自己的,当然你也永远只能拿到几千 ...

  7. canvas实现将文字变成颗粒

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

  8. Python进阶_类与实例

    上一节将到面对对象必须先抽象模型,之后直接利用模型.这一节我们来具体理解一下这句话的意思. 面对对象最重要的概念就是类(class)和实例(instance),必须牢记类是抽象的模板,比如studen ...

  9. Javascript实现继承

    以下转自阮一峰的文章:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.h ...

  10. Java基本包装类型

    基本类型的对象包装,也就是将常用的基本数据类型包装成对象 byte Byte short Short int Integer long Long boolean Boolean float Float ...