(BFS)codeforces #Round354-div2-D
来源:互联网 发布:中国移动网络测速 编辑:程序博客网 时间:2024/04/29 15:03
题目链接:题目链接
题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间
#include<bits/stdc++.h>using namespace std;const int N=1003;int n,m;int sx,sy,tx,ty;int dirx[]={1,-1,0,0};int diry[]={0,0,1,-1};char matrix[N][N];bool visit[N][N][4];char rotat[6][15]={{"+-|^><vLRUD*"},{"+|->v^<UDRL*"},{"+-|v<>^RLDU*"},{"+|-<^v>DULR*"}};class node{ public: int x; int y; int angle; int step; public: node(int x,int y,int a,int s):x(x),y(y),angle(a),step(s){}};int number(char c){ if(c=='+') return 0; if(c=='-') return 1; if(c=='|') return 2; if(c=='^') return 3; if(c=='>') return 4; if(c=='<') return 5; if(c=='v') return 6; if(c=='L') return 7; if(c=='R') return 8; if(c=='U') return 9; if(c=='D') return 10; if(c=='*') return 11;}bool yestop(char c){ if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D') return 1; return 0;}bool yesbottom(char c){ if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U') return 1; return 0;}bool yesleft(char c){ if(c=='+'||c=='-'||c=='<'||c=='R'||c=='U'||c=='D') return 1; return 0;}bool yesright(char c){ if(c=='+'||c=='-'||c=='>'||c=='L'||c=='U'||c=='D') return 1; return 0;}bool in(int x,int y){ if(x<=0||x>n||y<=0||y>m) return 0; return 1;}queue<node> Q;int bfs(){ Q.push(node(sx,sy,0,0)); visit[sx][sy][0]=1; while(!Q.empty()) { node p=Q.front(); Q.pop(); if(p.x==tx&&p.y==ty) return p.step; for(int i=0;i<4;i++) { int nx=p.x+dirx[i]; int ny=p.y+diry[i]; switch(i) { case 0: if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesbottom(rotat[p.angle][number(matrix[p.x][p.y])]) &&yestop(rotat[p.angle][number(matrix[nx][ny])])) { Q.push(node(nx,ny,p.angle,p.step+1)); visit[nx][ny][p.angle]=1; } break; case 1: if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yestop(rotat[p.angle][number(matrix[p.x][p.y])]) &&yesbottom(rotat[p.angle][number(matrix[nx][ny])])) { Q.push(node(nx,ny,p.angle,p.step+1)); visit[nx][ny][p.angle]=1; } break; case 2: if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesright(rotat[p.angle][number(matrix[p.x][p.y])]) &&yesleft(rotat[p.angle][number(matrix[nx][ny])])) { Q.push(node(nx,ny,p.angle,p.step+1)); visit[nx][ny][p.angle]=1; } break; case 3: if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesleft(rotat[p.angle][number(matrix[p.x][p.y])]) &&yesright(rotat[p.angle][number(matrix[nx][ny])])) { Q.push(node(nx,ny,p.angle,p.step+1)); visit[nx][ny][p.angle]=1; } break; } } p.angle=(p.angle+1)%4; if(!visit[p.x][p.y][p.angle]) { p.step++; Q.push(node(p)); visit[p.x][p.y][p.angle]=1; } } return -1;}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",matrix[i]+1); scanf("%d%d",&sx,&sy); scanf("%d%d",&tx,&ty); printf("%d\n",bfs());}
0 0
- (BFS)codeforces #Round354-div2-D
- codeforces #Round354-div2-B
- codeforces #Round354-div2-C
- Codeforces--192.div2.D Biridian Forest bfs
- codeforces 400(div2) D
- codeforces#ff(div2 D)
- Codeforces 292 div2 D
- codeforces #310 div2 D
- Codeforces 353 div2 D
- Codeforces #331 Div2 D
- Codeforces #332 Div2 D
- Codeforces 395 div2 D
- codeforces 136 div2 D题
- CodeForces #156 Div2 D题
- CodeForces Round 230 Div2 D
- codeforces div2 round#230 D
- Codeforces Round #317div2 D
- codeforces round#377 div2 D
- bzoj3744 Gty的妹子序列
- emacs
- 完美解决Linux(Android)操作系统下aes解密失败的问题
- bzoj2002【HNOI2010】Bounce 弹飞绵羊
- iOS gif 图片播放实现方法 (image 扩展方案)
- (BFS)codeforces #Round354-div2-D
- <机器学习练习>EM算法
- MIPI协议的DCS指令格式
- 5.31
- poj 2828 线段树 单点更新
- android中保存Bitmap图片到指定文件夹中的方法
- LAMP的环境的搭建
- 百度地图之获取Android签名证书的sha1值
- 基于U-BOOT-2010.09移植OK6410开发版记录(一)