博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF div2 D BFS
阅读量:7224 次
发布时间:2019-06-29

本文共 3705 字,大约阅读时间需要 12 分钟。

http://codeforces.com/contest/676/problem/D

题目大意:

勇者去迷宫杀恶龙。迷宫是有n*m的方格子组成的。迷宫上有各种记号,这些记号表达着能走的方向。当且仅当两个房间的记号是相互联通的,才能走过去。

我们有如下选择,最直白的一种就是直接走到另外一个格子上去(当然格子得相互连通),第二种操作就是顺时针旋转所有的格子(格子的位置保持不变)。

问,勇者要多久才能走到恶龙所在的地方。

 

思路:

表示这道题真心不会。。。下面的这个代码也是参考了别人的吧。。。换成我的话for的循环里面肯定不会写成f*3+i之类的(虽然这个到现在还没有弄明白)

 

我们用结构体保存当前的位置,旋转的次数和行走的距离。然后我们每次对一个当前的格子进行两种操作

①瞬时间旋转操作(每次都旋转1,反正最后肯定会遍历到4的)

②就是枚举上下左右,看看是否能走过去。

然后就OK了

 

1 //看看会不会爆int!  2 #include 
3 using namespace std; 4 #define ll long long 5 #define pb push_back 6 #define mk make_pair 7 #define fi first 8 #define se second 9 #define all(a) a.begin(), a.end() 10 11 const int maxn = 1005; 12 bool vis[maxn][maxn][10]; 13 int n, m; 14 struct point{ 15 int x, y; 16 int f, d; 17 point(int xx = 0, int yy = 0, int ff = 0, int dd = 0){ 18 x = xx, y = yy, f = ff, d = dd; 19 } 20 }p[maxn][maxn]; 21 22 char atlas[maxn][maxn]; 23 int xt, yt, xm, ym; 24 /* 25 int dx[] = {0, 0, -1, 1};//上下左右 26 int dy[] = {-1, 1, 0, 0}; 27 */ 28 int dx[] = {-1, 0, 1, 0};//左下右上 29 int dy[] = {
0, 1, 0, -1}; 30 31 bool check(char ch, int dir){ 32 if (dir == 0) return ch == '+' || ch == '|' || ch == '^' || ch == 'L' || ch == 'R' || ch == 'D'; 33 else if (dir == 1) return ch == '+' || ch == '-' || ch == '>' || ch == 'L' || ch == 'U' || ch == 'D'; 34 else if (dir == 2) return ch == '+' || ch == '|' || ch == 'v' || ch == 'L' || ch == 'R' || ch == 'U'; 35 else return ch == '+' || ch == '-' || ch == '<' || ch == 'R' || ch == 'U' || ch == 'D'; 36 return 0; 37 } 38 39 int bfs(){ 40 queue
que; 41 vis[xt][yt][0] = 1; 42 que.push(point(xt, yt, 0, 0)); 43 while (!que.empty()){ 44 point q = que.front(); 45 que.pop(); 46 int x = q.x, y = q.y; 47 int f = q.f, d = q.d; 48 if (x == xm && y == ym) return d; 49 if (!vis[x][y][(f + 1) % 4]){ 50 vis[x][y][(f + 1) % 4] = true; 51 que.push(point(x, y, (f + 1) % 4, d + 1)); 52 } 53 for (int i = 0; i < 4; i++){ 54 int nx = x + dx[i], ny = y + dy[i]; 55 if (nx <= 0 || ny <= 0 || nx > n || ny > m) continue; 56 if (atlas[nx][ny] == '*' || vis[nx][ny][f]) continue; 57 if (!check(atlas[x][y], (f * 3 + i) % 4)) continue;//我们要选择的是和上面相反的方向 58 if (!check(atlas[nx][ny], (f * 3 + i + 2) % 4)) continue; 59 que.push(point(nx, ny, f, d + 1)); 60 vis[nx][ny][f % 4] = true; 61 } 62 } 63 return -1; 64 } 65 66 int main(){ 67 #ifndef ONLINE_JUDGE 68 freopen("input.txt", "r", stdin); 69 #endif 70 scanf("%d%d", &n, &m); 71 for (int i = 1; i <= n; i++){ 72 scanf("%s", atlas[i] + 1); 73 } 74 scanf("%d%d", &xt, &yt); 75 scanf("%d%d", &xm, &ym); 76 int ans = bfs(); 77 printf("%d\n", ans); 78 return 0; 79 } 80 /* 81 _ooOoo_ 82 o8888888o 83 88" . "88 84 (| -_- |) 85 O\ = /O 86 ____/`---'\____ 87 .' \\| |// `. 88 / \\||| : |||// \ 89 / _||||| -:- |||||- \ 90 | | \\\ - /// | | 91 | \_| ''\---/'' | | 92 \ .-\__ `-` ___/-. / 93 ___`. .' /--.--\ `. . __ 94 ."" '< `.___\_<|>_/___.' >'"". 95 | | : `- \`.;`\ _ /`;.`/ - ` : | | 96 \ \ `-. \_ __\ /__ _/ .-` / / 97 ======`-.____`-.___\_____/___.-`____.-'====== 98 `=---=' 99 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^100 佛祖保佑 永无BUG101 */
View Code

 

转载于:https://www.cnblogs.com/heimao5027/p/5544351.html

你可能感兴趣的文章
IP-COM设置×××
查看>>
VPC配置案例
查看>>
十年IT运维谈(五):要专业化还是平台化?
查看>>
分享超级给力的一个外发光Shader
查看>>
oblog_4.6_SQL 语句
查看>>
通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
查看>>
对象实例化、字符串的使用方法
查看>>
keepalived基于LVS实现高可用,实现web服务的高可用
查看>>
80端口被Microsoft-HTTPAPI/2.0占用的解决办法
查看>>
无法抗拒Minecraft给予超高的自由度和探索-微访谈
查看>>
数据结构之串
查看>>
我的友情链接
查看>>
lvs+keepalived+nginx+tomcat高可用高性能集群部署
查看>>
实验:搭建主DNS服务器
查看>>
org.gjt.mm.mysql.Driver与com.mysql.jdbc.Driver区别
查看>>
部署exchange2010三合一:之五:功能测试
查看>>
nginx编译安装参数
查看>>
代码托管
查看>>
第一次给ThinkPHP5核心框架提pull request的完整过程
查看>>
U-Mail邮件系统何以誉为信息整合中转枢纽
查看>>