Codeforces 769C

本文介绍了一种寻找迷宫中特定长度路径的方法,该路径需从起点出发并回到起点,同时要求路径的字典序最小。文章详细解释了解题思路,包括初始错误的解决方案及修正后的正确思路,并提供了完整的代码实现。

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

很久没有发题解,今天这题卡了下百度没看到相关题解,最后还是看了官方题解才找到原本思路的bug过的。

题意:给出一个二维迷宫,*表示墙,. 表示路,X表示起点,问一个长度为k的路径,从X出发并且回到X,且路径字典序最小。如存在输出路径(方向按U、D、L、R表示,英文首字母),不存在则输出IMPOSSIBLE。

 

解题思路(wa在第49个case):

显然按照字典序,方向优先级D>L>R>U。随便弄了几组样例发现通常情况下,当所走路径长度达到一半的时候,原路返回比绕一个圈更优,比如LLUDRR优于LLURRD,于是很草率的决定后一半的路径全为原路返回。

特殊判断当k为奇数或者四个方向都无法前往时输出IMPOSSIBLE。

wa了后先是想了比较久都没找到反例,看了官方题解后一下子就找到了,如下:(原本反例有误,已修正)

7 4 18
..X.
....
....
....
.**.
....
*b..

按上面的算法,当走到"b"的时候,因为路径长度计数到9,也就是18的一半,开始原路返回(即下一步为U),但其实再往右、右、上、上、上……(下一步为R)更优,且路径长度亦为18。

思路纠正:

先广搜出X到所有其它点的最短距离,遍历点时按优先级访问,当下一点到起点的最短距离大于k-cnt时,跳过。

先是奇怪地CE了一发,原因是queue<pair<int,int>>q;这句定义报错,两个>符号要分开。(好吧vs上面没有这个问题,语言编译系统不一样)

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
const int N=1e3+10;
int n,m,k,cnt,x,y,tx,ty,dis[N][N];
int dx[]={1,0,0,-1},dy[]={0,-1,1,0};
char g[N][N],res[N*N],dir[]="DLRU";
bool vis[N][N];
queue<pair<int,int>>q;
bool check(int tx,int ty){
    return tx>=0&&tx<n&&ty>=0&&ty<m&&g[tx][ty]!='*';
}
void init(){
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(make_pair(x,y));
    vis[x][y]=true;
    while(!q.empty()){
        pair<int,int>tmp=q.front();q.pop();
        int tx=tmp.first,ty=tmp.second;
        for(int i=0;i<4;i++){
            int nx=tx+dx[i],ny=ty+dy[i];
            if(check(nx,ny)&&!vis[nx][ny]){
                q.push(make_pair(nx,ny));
                dis[nx][ny]=dis[tx][ty]+1;
                vis[nx][ny]=true;
            }
        }
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k)){
        cnt=0;
        for(int i=0;i<n;i++){
            scanf("%s",g[i]);
            for(int j=0;j<m;j++) if(g[i][j]=='X')
                x=i,y=j;
        }
        if(k&1){
            puts("IMPOSSIBLE");
            continue;
        }
        init();
        bool flag=true;
        while(flag){
            flag=false;
            for(int i=0;i<4;i++){
                tx=x+dx[i],ty=y+dy[i];
                if(check(tx,ty)&&dis[tx][ty]<=k-cnt){
                    flag=true;
                    res[cnt++]=dir[i];
                    x=tx,y=ty;
                    break;
                }
            }
            if(cnt==k) break;
        }
        if(!flag) puts("IMPOSSIBLE");
        else{
            res[k]='\0';
            puts(res);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/names-yc/p/6547667.html

### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值