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,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
随机推荐
- 高中生的IT之路-1.2离开校园
记得那是07年夏季的一天,高考成绩出来之后,班主任老师通知大家回学校报考志愿. 那天我刚到学校会议室,我还没来得及和同学见面就被班主任喊过去了,把志愿表递给我了我,我当时连仔细看那张志愿表都没看,随手 ...
- System.Threading.Tasks.Task引起的IIS应用程序池崩溃
问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0 ...
- 【BZOJ1030】[JSOI2007]文本生成器 AC自动机+动态规划
[BZOJ1030][JSOI2007]文本生成器 Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文 ...
- Python 自学积累(二)
1. onfigParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数( ...
- MAC电脑里的休眠功能在哪里?
Windows7和Ubuntu里都有睡眠和休眠功能,睡眠一般是指挂起到内存,电脑停止运行,数据都在内存里,只需要给内存供电,恢复时很快:休眠是指挂起到硬盘,电脑可以完全停止供电,恢复时从硬盘读取数据, ...
- No module named pip.req
https://stackoverflow.com/questions/25192794/no-module-named-pip-req#
- The Accomodation of Students---hdu2444(二分图,最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:有n个学生,m个关系,但是如果a认识b,b认识c,但是a不一定认识c: 求能不能把这n个人 ...
- 评论抓取:Python爬取微信在APPStore上的评论内容及星级
#完整程序如下: import requests import re def getHTMLText(url): try: r = requests.get(url) r.raise_for_stat ...
- OC发送短信
- (IBAction)sendMessage1:(id)sender { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@" ...
- Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中的输入流 第一节课
Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中 ...