B. Following Directions(模拟::坐标)

目录

题目链接

一些话

流程

套路

ac代码


题目链接

Problem - B - Codeforces


一些话


流程

模拟题,

思考如何用代码表示过程,如何用if来判断结果

1.此题与坐标相关,用两个变量x,y表示当前坐标,遍历字符串字符并根据字符来对x,y进行加减操作

2.结果判断,每次操作后判断xy是否同时等于1,用flag储存结果

3.根据flag输出结果


套路


ac代码

#include <iostream>
using namespace std;
int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        scanf("%d",&n);
        string s;
        cin >> s;
        bool flag = false;
        int x = 0,y = 0;
        for(int i = 0;i < n;i++){
            if(s[i] == 'L') x --;
            else if(s[i] == 'R') x++;
            else if(s[i] == 'U') y++;
            else if(s[i] == 'D') y--;
            if(x == 1 && y == 1) flag = true;
        }
        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Create a function pixel_flip(lst, orig_lst, budget, results, i=0) that uses recursion to generate all possible new unique images from the input orig_lst, following these rules: • The input lst is the current list being processed. Initially, this will be the same as orig_lst which is the original flattened image. • The input budget represents the number of pixels that can still be flipped. When the budget reaches 0, no more pixels can be flipped. • The input results is a list of resulting flattened images with flipped pixels. Initially, this will be an empty list. • The input i represents the index of the pixel being processed, by default set to 0, which is used to drive the recursive function towards its base case (i.e., initially starting from i=0). At termination of the function, the argument results should contain all possibilities of the input orig_lst by only flipping pixels from 0 to 1 under both the budget and the adjacency constraints. fill code at #TODO def pixel_flip(lst: list[int], orig_lst: list[int], budget: int, results: list, i: int = 0) -> None: """ Uses recursion to generate all possibilities of flipped arrays where a pixel was a 0 and there was an adjacent pixel with the value of 1. :param lst: 1D list of integers representing a flattened image . :param orig_lst: 1D list of integers representing the original flattened image. :param budget: Integer representing the number of pixels that can be flipped . :param results: List of 1D lists of integers representing all possibilities of flipped arrays, initially empty. :param i: Integer representing the index of the pixel in question. :return: None. """ #TODO def check_adjacent_for_one(flat_image: list[int], flat_pixel: int) -> bool: """ Checks if a pixel has an adjacent pixel with the value of 1. :param flat_image: 1D list of integers representing a flattened image . :param flat_pixel: Integer representing the index of the pixel in question. :return: Boolean. """ #TODO
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值