Tinkoff Challenge - Elimination Round B. Igor and his way to work(dfs+优化)
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+优化)的更多相关文章
- Tinkoff Challenge - Elimination Round 开始补题
A. Oleg and shares time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Tinkoff Challenge - Elimination Round D. Presents in Bankopolis(区间DP)
http://codeforces.com/contest/793/problem/D 题意:给出一些点和他们之间的距离,是有向的,这些点从1~n顺序排列,现在选出k个点组成一条路径,使他们之间的距离 ...
- Tinkoff Challenge - Elimination Round C. Mice problem(模拟)
传送门 题意 给出一个矩形的左下角和右上角的坐标,给出n个点的初始坐标和运动速度和方向,询问是否存在一个时间使得所有点都在矩形内,有则输出最短时间,否则输出-1 分析 对于每个点如果运动过程中都不在矩 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
随机推荐
- Dell、IBM服务器配置远程管理卡
author: headsen chen date: 2018-10-09 14:12:32 1,IBM的服务器: 需要在bios里边进行配置,具体配置如下: , 开机画面过完之后,按F1进入bio ...
- PHP 允许Ajax跨域访问 (Access-Control-Allow-Origin)
Ajax访问php,报错 php顶部加上即可: header("Access-Control-Allow-Origin: *");
- 【BZOJ4452】[Cerc2015]Export Estimate 并查集
[BZOJ4452][Cerc2015]Export Estimate Description 给你一个n个点m条边的无向图,每条边有权值,我们可以选择一个整数lim来生成一个新的图,过程如下: 1 ...
- Swift - 获取状态栏一些信息
// 获取状态栏的各种信息 :网络类型,运营商,电池电量,显示的系统时间等信息 import UIKit enum NetWorkType { case NetworkStatesNone // 没有 ...
- MySQL Server has gone away报错原因汇总分析(转自:http://cenalulu.github.io/mysql/mysql-has-gone-away/)
原因1. MySQL 服务宕了 判断是否属于这个原因的方法很简单,执行以下命令,查看mysql的运行时长 $ mysql -uroot -p -e "show global status l ...
- 【MySQL案例】error.log的Warning:If a crash happens thisconfiguration does not guarantee that the relay lo(转)
标签: 1.1.1. If a crash happens thisconfiguration does not guarantee that the relay log info will be c ...
- jsp实现文件下载的代码(转载)
Java代码 OutputStream out=response.getOutputStream(); byte by[]=new byte[500]; File fileLoad=new Fil ...
- Jmeter,常见参数 vars、prev、ctx 、props 类的api--beanshell
http://www.cnblogs.com/fnng/p/5827577.html---------jmeter 性能测试 jmeter常见参数 vars.prev.ctx .props 类的api ...
- CMDB三大绝招,助我站稳运维之巅
上一篇(内功篇)介绍了建设CMDB的内功心法,接下来和各位交流下建设CMDB的招式.内功是根基.是基础,决定了武学修为境界的高低,招式也许就是明心见性之后的修行.修为指一个人的修养.素质.道德.涵养. ...
- 利用GridView实现单选效果
1.实现如图所示的单选效果 由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridV ...