Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created September 19, 2015 16:32
Show Gist options
  • Save mzsima/44c15509ff5b7fee897e to your computer and use it in GitHub Desktop.
Save mzsima/44c15509ff5b7fee897e to your computer and use it in GitHub Desktop.
number button
//
// ViewController.m
// MinimumCombo
//
// Created by MizushimaYusuke on 9/19/15.
// Copyright © 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
#include <vector>
#include <memory>
using namespace::std;
class MiniCombo {
public:
int solve(int p, int q, vector<int> A) {
sort(A.begin(), A.end());
int dp[100][100];
for (int w = 2; w <= q+1; w++) {
for (int i=0; i <= q + 1 - w; i++) {
int t = 999999;
for (int k=i+1; k< i + w; k++) t = min(t, dp[i][k] + dp[k][i + w]);
if (t == 999999) t = 0;
dp[i][i+w] = t + A[i+w] - A[i] - 2;
}
}
return dp[0][q + 1];
}
};
@interface ViewController ()
@property (nonatomic, weak) UILabel *answer;
@property (nonatomic, strong) NSMutableArray *touched;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
for (int i=0; i<20; i++) {
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
box.tag = i + 1;
box.backgroundColor = [UIColor greenColor];
box.center = CGPointMake(i * 30 + 50, 100);
[self.view addSubview:box];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (!self.touched)
self.touched = [NSMutableArray array];
CGPoint p = [[touches anyObject] locationInView:self.view];
UIView *hit = [self.view hitTest:p withEvent:nil];
if (hit.tag > 0) {
[self.touched addObject:hit];
hit.backgroundColor = [UIColor redColor];
shared_ptr<MiniCombo> cppClass(new MiniCombo());
vector<int> num;
num.emplace_back(0);
for (int i=0; i<self.touched.count; i++) {
num.emplace_back([self.touched[i] tag]);
}
num.emplace_back(21);
int n = cppClass->solve(20, (int)self.touched.count, num);
[self.answer removeFromSuperview];
UILabel *l = [[UILabel alloc] init];
l.font = [UIFont systemFontOfSize:50];
l.textColor = [UIColor whiteColor];
l.text = [NSString stringWithFormat:@"%d", n];
[l sizeToFit];
[UIView animateWithDuration:0.5 animations:^{
l.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
}];
[self.view addSubview:l];
self.answer = l;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment