codeforces Igor and his way to work

探讨了一个有趣的问题:如何在一个存在障碍物的城市网格中找到一条不超过两次转弯的路径,从起点到达终点。通过优化搜索策略,避免了在大数据集上进行全图搜索导致的时间效率问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Igor and his way to work
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.

Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn’t contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor’s car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.

Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:

“.” — an empty cell;
“*” — a cell with road works;
“S” — the cell where Igor’s home is located;
“T” — the cell where Igor’s office is located.
It is guaranteed that “S” and “T” appear exactly once each.

Output
In the only line print “YES” if there is a path between Igor’s home and Igor’s office with no more than two turns, and “NO” otherwise.

Examples
input
5 5
..S..
**.
T….
**.
…..
output
YES
input
5 5
S….
**.
…..
.**
..T..
output
NO

1000*1000的数据 只允许你转两个弯,问你能不能从起点到终点。

一开始用的广搜。。。太久没写居然wa了。。写对了开始t。。。1000*1000还是太大了。。。。。只能先处理起点,再处理终点,再一个个处理。。。比光搜快很多。。因为这个是on复杂度。。 广搜不行

#include <bits/stdc++.h>
using namespace std;
char mp[1010][1010];
int si,sj,ei,ej;
int f1[1010][1010];
int f2[1010][1010];
    int n,m;
void qu(int x,int y,int (&vis)[1010][1010])
{
    for(int i=0;i+x<n;i++)
    {
        if(mp[i+x][y]=='*')
            break;
        vis[i+x][y]=1;
    }
    for(int i=0;x-i>=0;i++)
    {

        if(mp[x-i][y]=='*')
            break;
        vis[x-i][y]=1;
    }
    for(int i=0;i+y<m;i++)
    {

        if(mp[x][i+y]=='*')
            break;
        vis[x][i+y]=1;
    }
    for(int i=0;y-i>=0;i++)
    {

        if(mp[x][y-i]=='*') break; 
        vis[x][y-i]=1;
    }
}

int ch(int x,int y)
{
    for(int i=0;i+x<n;i++)
    {
        if(mp[i+x][y]=='*')
            break;

        if(f2[i+x][y]==1) return 1;
    }
    for(int i=0;x-i>=0;i++)
    {

        if(mp[x-i][y]=='*')
            break;

        if(f2[x-i][y]==1) return 1;
    }
    for(int i=0;i+y<m;i++)
    {

        if(mp[x][i+y]=='*')
            break;

        if(f2[x][i+y]==1) return 1;
    }
    for(int i=0;y-i>=0;i++)
    {

        if(mp[x][y-i]=='*') break; 

        if(f2[x][y-i]==1) return 1;
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf(" %c",&mp[i][j]);
            if(mp[i][j]=='S')
                si=i,sj=j;
            if(mp[i][j]=='T')
                ei=i,ej=j;
        }
    }
    qu(si,sj,f1);

    qu(ei,ej,f2);

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(f1[i][j])
            {
                if(ch(i,j))
                {
                    printf("YES\n");
                    return 0;
                }
            }
        }
    }
    printf("NO\n");
    return 0;
}

更新 广搜是可以的,只是比我写得要暴力很多。我的一个个方向走完比较慢。
贴代码

#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#define sf(i) scanf("%d",&i)
#define MAXN 1002
#define ii pair<int,int>
#define iii pair<ii,int>;
#define pii <ii,ii >
using namespace std;
bool vis[MAXN][MAXN][5][3];

struct node{
    ii pos;
    int in_dir;
    int turns;
};
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};


bool solve(){
   char M[MAXN][MAXN];
   queue<node> Q;
   ii start,goal;
   int n,m;
   //sf(n);sf(m);
   cin>>n>>m;
   for(int i=0;i<n;i++)
       for(int j=0;j<m;j++){
           cin>>M[i][j];
           if(M[i][j]=='S')
               start=make_pair(i,j);
           if(M[i][j]=='T')
               goal=make_pair(i,j);
       }
   node s;
   s.pos=start;
   s.in_dir=4;
   s.turns=0;
   Q.push(s);
   vis[start.first][start.second][4][0]=true;
   while(!Q.empty()){
       node u=Q.front();
       Q.pop();
       if(u.pos==goal)
           return true;
       for(int k=0;k<4;k++){
           int ni=u.pos.first+dir[k][0];
           int nj=u.pos.second+dir[k][1];
           if(ni>=0 && ni<n && nj>=0 && nj<m){
               node nu=u;
               nu.in_dir=k;
               nu.pos=make_pair(ni,nj);
               nu.turns=((u.in_dir!=k)&&(u.in_dir<4))?u.turns+1:u.turns;
               if(nu.turns<=2 && !vis[ni][nj][k][nu.turns] && M[ni][nj]!='*'){
                   vis[ni][nj][k][nu.turns]=true;
                   Q.push(nu);
               }
           }
       }
   }
   return false;
}

int main(int argc, char *argv[])
{
    if(solve())
        cout<<"YES\n";
    else
        cout<<"NO\n";
    return 0;
}
### 编程问题或教程从0到y的获取方法 对于希望在Codeforces平台上找到难度范围从简单至特定等级(标记为`y`)的问题或教程,可以采取多种策略来实现这一目标。Codeforces提供了一个强大的过滤器功能,允许用户基于标签、解决次数以及题目难度筛选问题。 #### 使用Codeforces内置工具寻找合适的问题 通过访问Codeforces的问题集页面并应用适当的选择条件,能够有效地定位所需资源。具体而言,在问题集合页面中存在一个高级搜索选项,支持按照多个维度进行细化检索: - **按标签分类**:可以选择诸如“constructive algorithms”这样的主题标签[^1]。 - **依据难度区间挑选**:设定下限为入门级(即 rating 800),上限则根据个人需求调整至指定值`y`。例如,如果想要覆盖所有不超过rating 1900的问题,则应在此处输入相应数值作为最大边界。 ```python import requests from bs4 import BeautifulSoup def fetch_problems_by_difficulty(min_rating, max_rating): url = f"https://codeforces.com/problemset/page/1?difficulty={min_rating}-{max_rating}" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") problem_list = [] for row in soup.select(".problems tr"): columns = row.find_all('td') if len(columns)>0: name_cell = columns[0].find("a", href=True)['href'] title = columns[0].text.strip() difficulty = int(columns[-1].text) problem_info = { 'title': title, 'link': f'https://codeforces.com{name_cell}', 'difficulty': difficulty } problem_list.append(problem_info) return problem_list[:5] # Example usage with min_rating set to 800 and max_rating as variable y problem_set = fetch_problems_by_difficulty(800, y) for p in problem_set: print(f"{p['title']} ({p['difficulty']}) [{p['link']}]") ``` 此Python脚本展示了如何利用BeautifulSoup库解析HTML文档结构,并从中提取满足给定难度区间的前五个编程挑战的信息。 #### 探索高质量的学习资料 除了直接解决问题外,学习者还可以借助一系列精心编写的指南和文章加深理解。这些材料通常由经验丰富的竞赛选手撰写,旨在帮助新手逐步掌握必要的技能和技术要点[^2][^3]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值