【题目链接】:http://codeforces.com/contest/793/problem/B

【题意】



给一个n*m大小的方格;

有一些方格可以走,一些不能走;

然后问你从起点到终点,能不能在转弯次数不超过2的情况下达到;

【题解】



记忆化搜索写一个;

f[x][y][z]表示,到了x,y这个坐标,当前方向是z的最小转弯次数;

按照这个数组;

写一个dfs就好;

不会难.



【Number Of WA】



2(一开始没加第三维TAT)



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e3+100; int n,m,a[N][N],x0,y0,x1,y1,f[N][N][5];
char s[N]; void dfs(int x,int y,int pre,int num)
{
if (a[x][y]==0) return;
if (num>2) return;
if (f[x][y][pre]!=-1 && f[x][y][pre]<=num) return; f[x][y][pre] = num;
rep1(i,1,4)
{
int tx = x+dx[i],ty = y + dy[i];
int before = pre+2;
if (before>4) before-=4;
if (pre!=0 && i==before) continue;
if (pre==0)
dfs(tx,ty,i,num);
else
{
if (pre!=i)
dfs(tx,ty,i,num+1);
else
dfs(tx,ty,i,num);
}
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n >> m;
rep1(i,1,n)
{
cin>>(s+1);
rep1(j,1,m)
if (s[j]=='*')
a[i][j] = 0;
else
{
a[i][j] = 1;
if (s[j]=='S')
x0= i,y0=j;
if (s[j]=='T')
x1 =i,y1 = j;
}
}
ms(f,255);
dfs(x0,y0,0,0);
rep1(i,1,4)
if (f[x1][y1][i]!=-1)
return cout <<"YES"<<endl,0;
cout <<"NO"<<endl;
return 0;
}

最新文章

  1. 1000行代码实现MVVM (类似Angular1.x.x , Vue)
  2. javaScript 基础知识
  3. CRC校验码原理、实例、手动计算
  4. 循环执行n次的代码
  5. http协议之request
  6. html5,表单
  7. bzoj3413
  8. xss(跨站脚本攻击),crsf(跨站请求伪造),xssf
  9. Eratosthenes筛选法
  10. Good vs Evil
  11. PHP学习之[第06讲]数组、多维数组和数组函数
  12. (转)WCF入门教程(一)简介
  13. LeeCode-Same Tree
  14. eclipse导入android studio时一些异常的处理
  15. Linux(CentOS6.5)下修改Nginx初始化配置
  16. 网站升级HTTPS后WebSocket不能连接的问题
  17. 5000量子位支持量子编程,D-Wave推出下一代量子计算平台计划
  18. javascript高级程序设计第3版——第5章 引用类型
  19. js获得焦点和失去焦点那些事
  20. 1.1.2A+B for Input-Output Practice (II)

热门文章

  1. oc56--ARC多个对象的内存管理
  2. fopen文件目录问题
  3. [Swift]LeetCode1066. 校园自行车分配 II | Campus Bikes II
  4. BZOJ 2333 左偏树 (写得我人生都崩溃了...)
  5. MySQL学习笔记之右连接
  6. Objective-C—— Block
  7. Android使用charles抓包
  8. 国内DNS服务器地址
  9. logger日志
  10. Jmeter JSON断言和响应断言的区别是什么?