100200H
这是个bfs
首先建图,先从终点bfs求出每点距离,然后从起点开始,确定初始方向:某点和自己相邻距离比自己小1就是
然后就先贪心和上次一样的方向,如果不能走,就找出一个方向,把自己当前方向改掉,重复过程,直到走到终点
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
string s;
const int dx[]={-,,,},dy[]={,,-,};
int n,m;
int can[][][];
int used[][],d[][];
bool inrange(int x,int y)
{
return (x>=&&y>=&&x<=m&&y<=n);
}
void bfs()
{
queue<int>q;
q.push(m);
q.push(n);
used[m][n]=;
while(!q.empty())
{
int x=q.front(); q.pop();
int y=q.front(); q.pop();
for(int i=;i<;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(!used[xx][yy]&&inrange(xx,yy)&&can[x][y][i])
{
d[xx][yy]=d[x][y]+;
q.push(xx); q.push(yy);
used[xx][yy]=;
}
}
}
}
void get_path()
{
int x=,y=,last;//0:南 1:北 2:西 3:东
if(can[x][y][]&&d[x][y]==d[x+dx[]][y+dy[]]+)
{
x++; cout<<"N"<<endl; last=;
}
else if(can[x][y][]&&d[x][y]==d[x+dx[]][y+dy[]]+)
{
y++; cout<<"E"<<endl; last=;
}
while(x!=m||y!=n)
{
if(can[x][y][last]&&d[x+dx[last]][y+dy[last]]==d[x][y]-)
{
x=x+dx[last]; y=y+dy[last]; cout<<"F";
}
else
for(int i=;i<;i++)
{
if(can[x][y][i]&&d[x+dx[i]][y+dy[i]]==d[x][y]-)
{
x=x+dx[i]; y=y+dy[i];
if(last==)
{
if(i==) cout<<"R";
if(i==) cout<<"L";
}
if(last==)
{
if(i==) cout<<"L";
if(i==) cout<<"R";
}
if(last==)
{
if(i==) cout<<"L";
if(i==) cout<<"R";
}
if(last==)
{
if(i==) cout<<"R";
if(i==) cout<<"L";
}
last=i; break;
}
}
}
}
int main()
{
freopen("straight.in","r",stdin);
freopen("straight.out","w",stdout);
scanf("%d%d",&m,&n); cin.ignore();
for(int i=m;i>=;i--)
{
getline(cin,s,'\n');
for(int j=;j<s.length();j+=)
{
if(s[j]=='-')
{
can[i][(j+)/][]=;
can[i][(j+)/+][]=;
}
}
if(i!=)
{
getline(cin,s,'\n');
for(int j=;j<s.length();j+=)
{
if(s[j]=='|')
{
can[i][j/+][]=;
can[i-][j/+][]=;
}
}
}
}
bfs();
get_path();
fclose(stdin);
fclose(stdout);
return ;
}
100200H的更多相关文章
- 工作中MySql的了解到的小技巧
工作中MySql的小技巧 1. 跑脚本时,经常遇到有则更新无插入的 逻辑操作:通常情况下,来一波if()判断然后选择 更新还是插入,前两天逛论坛时发现有人在比较REPLACE INTO 和 INSET ...
随机推荐
- 这几天在搞UNITY3D,感觉回到了AS2
这几天在搞UNITY3D,感觉回到了AS2 代码没有MAIN, 代码在屏幕上,和元件绑定
- lgy -oracle
PL/SQL Developer 和 instantclient客户端安装配置(图文) 一: PL/SQL Developer 安装 下载安装文件安装,我这里的版本号是PLSQL7.1.4.1391, ...
- 转:GCC,LLVM,Clang编译器对比
GCC,LLVM,Clang编译器对比 转自: http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中, ...
- proxy改变this指向
var core_slice = Array.prototype.slice; var proxy = function(context,fn) { var args, proxy; if ( typ ...
- Location of several networks in brain
Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...
- Mysql导出函数、存储过程
下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...
- js checkbox 选中判断
var isSelect = ""; isSelect = $("#tblImgList" + " input[type='checkbox']&qu ...
- 5 个最好的3D游戏开发工具(转)
转自:http://www.open-open.com/news/view/33a4f0 5 个最好的3D游戏开发工具 jopen 2012-11-19 22:56:21 • 发布 摘要:UDK(th ...
- VS2013 抛出 stackoverflow exception 的追踪
本公司使用VWG.Caslte ActiveRecord.CSLA.net .Quantz.net 等组件做为公司的开发基础,自2007年以来,一直工作正常,但最近(2015.12月)以来,打开MDA ...
- redis 学习笔记(4)-HA高可用方案Sentinel配置
上一节中介绍了master-slave模式,在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的”读/写功能都将受影响,这在生产环境中显然 ...