codeforces 676D Theseus and labyrinth BFS搜索
分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=1e3+;
struct Node{
int t,x,y;
};
queue<Node>q;
char c[N][N];
int s[][N][N],n,m;
char p[]="0+-|^><vLRUD*";
int a[]={,,,,,,,,,,,,};
int b[]={,,,,,,,,,,,,};
int dp[][N][N];
bool judgel(int x){
if(x==||x==||x==||x==||x==||x==)
return true;
return false;
}
bool judger(int x){
if(x==||x==||x==||x==||x==||x==)
return true;
return false;
}
bool judgeu(int x){
if(x==||x==||x==||x==||x==||x==)
return true;
return false;
}
bool judged(int x){
if(x==||x==||x==||x==||x==||x==)
return true;
return false;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
scanf("%s",c[i]+);
for(int i=;i<=n;++i)
{
for(int j=;j<=m;++j)
{
for(int k=;k<=;++k)
if(c[i][j]==p[k])
{
s[][i][j]=a[k];
break;
}
}
}
for(int k=;k<=;++k)
{
for(int i=;i<=n;++i)
{
for(int j=;j<=m;++j)
{
s[k][i][j]=b[s[k-][i][j]];
}
}
}
Node st,aim;
scanf("%d%d%d%d",&st.x,&st.y,&aim.x,&aim.y);
memset(dp,-,sizeof(dp));
st.t=;
q.push(st);dp[][st.x][st.y]=;
int ans=INF;
while(!q.empty()){
Node u=q.front();
q.pop();
if(u.x==aim.x&&u.y==aim.y){
break;
}
Node e=u;e.t=(e.t+)%;
if(dp[e.t][e.x][e.y]==-){
dp[e.t][e.x][e.y]=dp[u.t][e.x][e.y]+;
q.push(e);
}
if(u.y>&&judgel(s[u.t][u.x][u.y])&&judger(s[u.t][u.x][u.y-])){
if(dp[u.t][u.x][u.y-]==-){
e=u;--e.y;
dp[e.t][e.x][e.y]=dp[u.t][u.x][u.y]+;
q.push(e);
}
}
if(u.y<m&&judger(s[u.t][u.x][u.y])&&judgel(s[u.t][u.x][u.y+])){
if(dp[u.t][u.x][u.y+]==-){
e=u;++e.y;
dp[e.t][e.x][e.y]=dp[u.t][u.x][u.y]+;
q.push(e);
}
}
if(u.x>&&judgeu(s[u.t][u.x][u.y])&&judged(s[u.t][u.x-][u.y])){
if(dp[u.t][u.x-][u.y]==-){
e=u;--e.x;
dp[e.t][e.x][e.y]=dp[u.t][u.x][u.y]+;
q.push(e);
}
}
if(u.x<n&&judged(s[u.t][u.x][u.y])&&judgeu(s[u.t][u.x+][u.y])){
if(dp[u.t][u.x+][u.y]==-){
e=u;++e.x;
dp[e.t][e.x][e.y]=dp[u.t][u.x][u.y]+;
q.push(e);
}
}
}
for(int i=;i<;++i)
if(dp[i][aim.x][aim.y]!=-){
ans=min(dp[i][aim.x][aim.y],ans);
}
if(ans==INF)ans=-;
printf("%d\n",ans);
return ;
}
codeforces 676D Theseus and labyrinth BFS搜索的更多相关文章
- CodeForces 676D Theseus and labyrinth
最短路. $dis[i][j][k]$记录到点$(i,j)$,门的状态为$k$时的最短路.转移的时候有$8$种方案,即直接走向周围四个点,或者进行旋转.比较烦的是判断两个相邻的点在状态$k$下是否连通 ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
- Codeforces 301_div.2_Ice Cave(BFS走冰块)
Ice Cave Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descripti ...
- BFS搜索
参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...
- Horse Pro(带负坐标的bfs搜索)
Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...
随机推荐
- c# Oracle 远程连接方式 plsql 连接oracle 11g 64位
1.本地连接字符串: string connect = "Data Source=orcl;user=XXX;password=XXX;Persist Security Info=Tru ...
- C# Windows - ListView
ListView控件的属性 属性 说明 Activation 控制用户在列表视图中激活选项的方式Standard - 用户为自己的计算机选择的值OneClick – 单击一个选项,激活它TwoClic ...
- bnuoj 4357 传送阵
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4357 [题意]:在1000个数中选择3个之和是m的倍数,可能有多种选择方案,请输出标号排序最小的一组 ...
- MySQL的基本命令
MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...
- eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.
项目移除原来的jre环境lib后,添加本地的jre,报错如下: Java compiler level does not match the version of the installed Java ...
- 字符串转到js对象
var obj = (new Function("return " + str))();
- PAT-乙级-1011. A+B和C (15)
1011. A+B和C (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 给定区间[-231, 231 ...
- httpsclient 自动获取证书 无证书访问 验证过能直接用
首先实现写一个 实现接口SecureProtocolSocketFactory的类. /** *ClassName: bcde *date: 2015年2月26日 下午4:51:01 * *@auth ...
- DLL搜索路径和DLL劫持
DLL搜索路径和DLL劫持 环境:XP SP3 VS2005 作者:magictong 为什么要把DLL搜索路径(DLL ORDER)和DLL劫持(DLL Hajack)拿到一起讲呢?呵呵,其实没啥深 ...
- codeforces #310 div1 D
一开始写了个暴力模拟绳子的摆动轨迹 然后在Test 16 T掉了 后来%了一下别人的代码,发现需要对特殊情况进行特殊处理 首先我们考虑绳子的向右摆动,设当前位置为p,绳子当前长度为L 如果其旋转中心位 ...