一个有几个小坑的bfs

题目很长,但并不复杂,大概总结起来有这么点。

有t组输入

每组输入n, m, p。表示一个n*m的地图,每p秒按键会右移一次(这个等会儿再讲)。

然后是地图的输入。其中'@'为起点,'$'为终点,'.'为通路,'*'为不通。

问从起点到终点最少需要多久?

一眼看去,裸的bfs嘛,10*10的地图,简单!

不过还是连错4次……

注意!

每一秒有4种操作,注意,不是三种,1. 光标左移,2. 光标右移,3. 按照光标方向行走,4. 不动(尤其是这一个)。

所谓光标左移,右移,因为题目中给出了光标(在图上,共4个方向),每次改变的移动方向只能是当前光标选择的方向的左右两个。而行走只能按照光标所指的方向。

另外每过p秒,方向会变化(初始四个方向的顺序为左右上下,p秒后变成右上下左,再过p秒后变为上下左右)。

废话说完,上代码——

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int N = ; struct node
{
int x, y, dis, step;
}; int go[][] = {{, -}, {, }, {-, }, {, }}; int t;
int n, m, P;
char mp[N][N];
bool v[N][N];
bool vv[N][N][]; int change(int a)
{
switch(a)
{
case :
return ;
case :
return ;
case :
return ;
case :
return ;
}
} void bfs()
{
node p;
bool k = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(mp[i][j] == '@')
{
p.x = i;
p.y = j;
k = ; break;
}
}
if(k) break;
}
p.dis = ;
p.step = ;
vv[p.x][p.y][] = ; queue<node> que;
que.push(p);
while(!que.empty())
{
node p = que.front();
que.pop(); int xx = p.x+go[p.dis][];
int yy = p.y+go[p.dis][];
if(xx >= && xx < n && yy >= && yy < m)
{
if(v[xx][yy] == )
{
if(mp[xx][yy] == '.')
{
v[xx][yy] = ;
node q;
q.x = xx;
q.y = yy;
q.step = p.step+;
q.dis = p.dis;
if(q.step%P == )
{
q.dis = change(q.dis);
vv[xx][yy][q.dis] = ;
}
que.push(q);
}
else if(mp[xx][yy] == '$')
{
printf("%d\n", p.step+);
return;
}
}
} if((p.step+)%P == ) {p.dis = change(p.dis);} node q;
q.x = p.x;
q.y = p.y;
q.step = p.step+; q.dis = p.dis+;
q.dis %= ;
if(vv[q.x][q.y][q.dis] == )
{
que.push(q);
vv[q.x][q.y][q.dis] = ;
} q.dis = p.dis+;
q.dis %= ;
if(vv[q.x][q.y][q.dis] == )
{
que.push(q);
vv[q.x][q.y][q.dis] = ;
} q.dis = p.dis;
if(vv[q.x][q.y][q.dis] == )
{
que.push(q);
vv[q.x][q.y][q.dis] = ;
}
}
printf("YouBadbad\n");
} int main()
{
//freopen("test.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
memset(mp, , sizeof(mp));
memset(v, , sizeof(v));
memset(vv, , sizeof(vv));
scanf("%d%d%d", &n, &m, &P);
for(int i = ; i < n; i++)
{
scanf("%s", mp[i]);
}
bfs();
}
}

ZOJ3865:Superbot(BFS) The 15th Zhejiang University Programming Contest的更多相关文章

  1. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  2. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...

  3. The 16th Zhejiang University Programming Contest-

    Handshakes Time Limit: 2 Seconds      Memory Limit: 65536 KB Last week, n students participated in t ...

  4. 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...

  5. The 19th Zhejiang University Programming Contest - H

    Princess Cjb is caught by Heltion again! Her knights Little Sub and Little Potato are going to Helti ...

  6. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) B"Even Number Theory"(找规律???)

    传送门 题意: 给出了三个新定义: E-prime : ∀ num ∈ E,不存在两个偶数a,b,使得 num=a*b;(简言之,num的一对因子不能全为偶数) E-prime factorizati ...

  7. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A     Thanks, TuSimple! Time ...

  8. 2019 The 19th Zhejiang University Programming Contest

    感想: 今天三个人的状态比昨天计院校赛的状态要好很多,然而三个人都慢热体质导致签到题wa了很多发.最后虽然跟大家题数一样(6题),然而输在罚时. 只能说,水题还是刷得少,看到签到都没灵感实在不应该. ...

  9. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

随机推荐

  1. PHP 开发中的外围资源性能分析(一)

    暂且不讨论「PHP 是不是最好的编程语言」,本文我们将分别分析一下在 PHP 程序的后端外围资源和前端外围资源,它们对整个 PHP Web 应用体验的影响,这往往比语言本身大得多. 首先,后端外围资源 ...

  2. mac os 下如何清除/切换svn eclipse插件的用户

    以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al ... drwxr-xr-x   6 linxyz  staff   ...

  3. What is the difference between database table and database view?

    The database table has a physical existence in the database. A view is a virtual table, that is one ...

  4. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  5. POJ 1781

    #include <iostream> #include <string> #include <cmath> using namespace std; unsign ...

  6. hdu1102

    http://acm.hdu.edu.cn/showproblem.php?pid=1102 最小生成树(模板题) 3 0 990 692 990 0 179 692 179 0 1 1 2 一共3个 ...

  7. 合并傻子//区间dp

    P1062 合并傻子 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 从前有一堆傻子,钟某人要合并他们~但是,合并傻子是要掉RP的...... 描述 在一个园 ...

  8. 51Nod 有限背包计数问题 题解报告

    首先这道题理论上是可以做到O(nlogn)的,因为OEIS上有一个明显可以用多项式乘法加速的式子 但是由于模数不是很兹磁,所以导致nlogn很难写 在这里说一下O(n*sqrt(n))的做法 首先我们 ...

  9. HEOI2016游记

    DAY -1: 省选前集训因为某些事情感觉心情和状态异常的糟糕 坐在窗户上吹了半个小时的冷风之后觉得回家休息一段时间 (反正我家在保定,大不了我省选的时候直接去河北大学就好辣) 在家里颓了三天吧,想清 ...

  10. lintcode: 三数之和II

    题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = .  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...