http://codeforces.com/contest/793/problem/B

题意:
一个地图,有起点和终点还有障碍点,求从起点出发到达终点,经过的路径上转弯次数是否能不超过2。

思路:

直接dfs,但是要优化一下,用vis[x][y][dir]来记录在(x,y)并且方向为dir时的最少转弯数,这样在dfs的时候可以剪掉一些不符合的情况。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int maxn=+; int n,m;
int sx,sy;
int flag;
char g[maxn][maxn];
int vis[maxn][maxn][]; int dx[]={,,,-};
int dy[]={,-,,}; void dfs(int x,int y,int dir,int turn)
{
if(flag) return;
if(turn>) return;
if(vis[x][y][dir]<=turn) return; //优化
if(g[x][y]=='T')
{
if(turn<=) flag=;
return;
}
vis[x][y][dir]=turn;
for(int k=;k<;k++)
{
int xx=x+dx[k];
int yy=y+dy[k];
if(g[xx][yy]=='*') continue;
if(xx<||x>=n||yy<||yy>=m) continue;
if(k!=dir) dfs(xx,yy,k,turn+);
else dfs(xx,yy,k,turn);
}
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
int ff=;
for(int i=;i<n;i++)
{
scanf("%s",g[i]);
if(!ff)
for(int j=;j<m;j++)
if(g[i][j]=='S') {sx=i;sy=j;ff=;}
}
flag=;
memset(vis,inf,sizeof(vis));
for(int k=;k<;k++)
{
int x=sx+dx[k];
int y=sy+dy[k];
if(x<||x>=n||y<||y>=m) continue;
if(g[x][y]!='*') dfs(x,y,k,);
}
if(flag) puts("YES");
else puts("NO");
}
return ;
}

Tinkoff Challenge - Elimination Round B. Igor and his way to work(dfs+优化)的更多相关文章

  1. Tinkoff Challenge - Elimination Round 开始补题

    A. Oleg and shares time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. Tinkoff Challenge - Elimination Round D. Presents in Bankopolis(区间DP)

    http://codeforces.com/contest/793/problem/D 题意:给出一些点和他们之间的距离,是有向的,这些点从1~n顺序排列,现在选出k个点组成一条路径,使他们之间的距离 ...

  3. Tinkoff Challenge - Elimination Round C. Mice problem(模拟)

    传送门 题意 给出一个矩形的左下角和右上角的坐标,给出n个点的初始坐标和运动速度和方向,询问是否存在一个时间使得所有点都在矩形内,有则输出最短时间,否则输出-1 分析 对于每个点如果运动过程中都不在矩 ...

  4. Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) 继续跪一把

    这次的前三题挺简单的,可是我做的不快也不对. A. Bank Robbery time limit per test 2 seconds memory limit per test 256 megab ...

  5. Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2)

    A: 思路:就是找b,c之前有多个s[i] 代码: #include<stdio.h>#define ll long longusing namespace std;ll a,b,c;in ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)

    题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  9. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

随机推荐

  1. 解决在微信中部分IOS不能自动播放背景音乐

    前言在做各种HTML5场景页面的时候,插入背景音乐是一个很普遍的需求.我们都知道,IOS下的safari是无法自动播放音乐的,以至一直以来造成一种错误的认识,iso是无法自动播放媒体资源的.直到微信火 ...

  2. C#的命令行工具

    ​在最开始学java的时候我们一般用 记事本 + 命令行,在命令行里边进行编译和运行, C#也有类似的东西(csc工具),在学习C#语言的时候可以用 文本编辑器来编写代码,然后用C#的命令行工具来编译 ...

  3. ios 屏幕旋转的问题

    在ios6之前我们旋转屏幕只需要实现shouldAutorotateToInterfaceOrientation就行了 - (BOOL)shouldAutorotateToInterfaceOrien ...

  4. 双调欧几里得旅行商问题(TSPhdu2224)

    http://acm.hdu.edu.cn/showproblem.php?pid=2224 The shortest path Time Limit: 1000/1000 MS (Java/Othe ...

  5. SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回

    @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区. ...

  6. opencv学习笔记——FileStorage类的数据存取操作

    OpenCV的许多应用都需要使用数据的存储于读取,例如经过3D校准后的相机,需要存储校准结果矩阵,以方便下次调用该数据:基于机器学习的应用,同样需要将学习得到的参数保存等.OpenCV通过XML/YA ...

  7. PL/SQL集合(一):记录类型(TYPE 类型名称 IS RECORD)

    记录类型 利用记录类型可以实现复合数据类型的定义: 记录类型允许嵌套: 可以直接利用记录类型更新数据. 传统操作的问题 对于Oracle数据类型,主要使用的是VARCHAR2.NUMBER.DATE等 ...

  8. SpringBoot项目属性配置

    如果使用IDEA创建Springboot项目,默认会在resource目录下创建application.properties文件,在SpringBoot项目中,也可以使用yml类型的配置文件代替pro ...

  9. Python大数据:jieba 中文分词,词频统计

    # -*- coding: UTF-8 -*- import sys import numpy as np import pandas as pd import jieba import jieba. ...

  10. saml,sso

    saml,sso centos version get:// cat /etc/redhat-release