一码学程OC Round #2

本文介绍了四个编程题目,包括车的横冲直撞、精英人才选择、统计和以及字符串拼接问题。涉及贪心算法、数组和字符串处理等编程技巧。
摘要由CSDN通过智能技术生成

一套较难题。

首先来看A题。

A : 横冲直撞的车

戳这里

时间限制: 1000 MS 内存限制: 131072 KB 提交总数: 40 AC总数: 29

问题描述

有一天无聊的迎哥哥不知道从哪弄来一副象棋,不过不知道下象棋的迎哥哥很是头疼,各种百度查规则ORZ... 这不,我们迎哥哥查阅到,中国象棋是中华民族传统文化的一部分,不仅在国内深受广大棋迷的喜爱,而且流传至国外,也逐步赢得很多棋迷的青睐。国外比较流行国际象棋(chess),很多外国棋友都想知晓中国象棋的游戏规则。两者走棋方法毕竟有很多差异,熟知一下基本规则,两种棋艺很多地方都是能够触类旁通的。很想精通中国象棋的他就开始一个一个的研究象棋中棋子的行棋规则。 迎哥哥第一个研究的就是象棋中的车,车在象棋中威力最大,无论横线、竖线均可行走,只要无子阻拦,步数不受限制。俗称“车行直路”。 那么问题就来了,现在迎哥哥的手上有一个n*m的棋盘,棋盘上已经有k个棋子存在。因为“车行直路”,所以迎哥哥想让他的车想横冲直撞。车能够横冲直撞的条件就是车所在的行或者列不存在别的棋子;已知的k个棋子的位置会干扰车的横冲直撞;所以现在想问你,已知的棋盘有多少个行和多少列列可以让我们迎哥哥的车横冲直撞?

输入格式

第一行输入n,m,k三个整数,棋盘的大小为n*m,k表示棋盘中已有棋子的个数; 接下来k行,每行两个整数x,y表示棋子的位置 其中数据范围:(1<=n,m<=1000000),(0<=k<=1000000)(1<=x<=n,1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Objective-C 编写的手势解锁密码的示例代码: 首先,在你的项目创建一个名为 "GestureLockView" 的 UIView 子类。这个类将用于绘制手势解锁的视图。 GestureLockView.h: ```objective-c #import <UIKit/UIKit.h> @protocol GestureLockViewDelegate <NSObject> - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path; @end @interface GestureLockView : UIView @property (nonatomic, weak) id<GestureLockViewDelegate> delegate; @end ``` GestureLockView.m: ```objective-c #import "GestureLockView.h" @interface GestureLockView() @property (nonatomic, strong) NSMutableArray *selectedButtons; @end @implementation GestureLockView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.selectedButtons = [NSMutableArray array]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); for (int i = 0; i < 9; i++) { CGFloat x = (i % 3) * 100 + 25; CGFloat y = (i / 3) * 100 + 25; CGRect frame = CGRectMake(x, y, 50, 50); UIButton *button = [[UIButton alloc] initWithFrame:frame]; button.tag = i + 1; button.layer.cornerRadius = 25.0; button.layer.borderWidth = 1.0; button.layer.borderColor = [UIColor blackColor].CGColor; [button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateSelected]; [button setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } if (self.selectedButtons.count > 0) { UIBezierPath *path = [UIBezierPath bezierPath]; for (int i = 0; i < self.selectedButtons.count; i++) { UIButton *button = self.selectedButtons[i]; CGPoint point = button.center; if (i == 0) { [path moveToPoint:point]; } else { [path addLineToPoint:point]; } } [path addLineToPoint:self.currentPoint]; [[UIColor blueColor] set]; path.lineWidth = 5.0; CGContextAddPath(context, path.CGPath); CGContextStrokePath(context); } } - (void)buttonTapped:(UIButton *)button { button.selected = YES; [self.selectedButtons addObject:button]; [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableString *path = [NSMutableString string]; for (UIButton *button in self.selectedButtons) { [path appendFormat:@"%ld", button.tag]; } [self.delegate gestureLockView:self didFinishWithPath:path]; [self reset]; } - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self reset]; } - (void)handleTouches:(NSSet<UITouch *> *)touches { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; for (UIButton *button in self.subviews) { if (CGRectContainsPoint(button.frame, point)) { if (![self.selectedButtons containsObject:button]) { button.selected = YES; [self.selectedButtons addObject:button]; } break; } } self.currentPoint = point; [self setNeedsDisplay]; } - (void)reset { for (UIButton *button in self.selectedButtons) { button.selected = NO; } [self.selectedButtons removeAllObjects]; [self setNeedsDisplay]; } - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end ``` 在你的视图控制器,你可以添加一个 GestureLockView 实例,并设置它的 delegate。当手势解锁完成时,GestureLockView 将调用代理方法 gestureLockView:didFinishWithPath:,你可以在这个方法处理解锁的逻辑。 ViewController.h: ```objective-c #import <UIKit/UIKit.h> #import "GestureLockView.h" @interface ViewController : UIViewController <GestureLockViewDelegate> @end ``` ViewController.m: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) GestureLockView *lockView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)]; self.lockView.center = self.view.center; self.lockView.delegate = self; [self.view addSubview:self.lockView]; } - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path { NSLog(@"Path: %@", path); if ([path isEqualToString:@"123"]) { NSLog(@"Unlock success!"); } else { NSLog(@"Unlock failed!"); } } @end ``` 这里的解锁密码是 "123"。你可以根据需要修改这个密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值