! Choose language
選擇你的語言
close  
 語言 

Announcement

  • 李小鑫
    毕业了四年,我还是我,一点没有变。

My blog

  • 数独 解题算法猜想

    Friday, May 25, 2012 12:07AM / Standard Entry

    最近做数独到了入迷地步,难度增加后,耗时无数。突然想做一个电脑解题,让计算机代替人脑工作。

    网上搜索了一大堆资料,结果解题算法无非是暴力解题:一个一个数字填进空格子去试。填个几万几十万次,总会成功。不过做计算的时候,计算机明显卡很久……

    最后想法是模仿人脑解题。

    1.最开始肯定是做自动草稿,这个宫格的草稿数字(候选数字),肯定是与同行 或同列 或同宫的其他已经填写数字不同。

    遍历所有未填空格后,把每个空格子可以填写的数字写出,此时完成这些空格自动草稿了。

    算法:设每个空可以填写array:123456789,查找同行同列同宫中的已填写的格子,找到一个数字,就删除array中的一个数字。最后剩下的就是这个空格的草稿数字。

    2.明显,即克找出草稿数字数量=1的宫格,这个叫做 唯一数。填写这个唯一数到这个空格。然后往下往下

    算法:遍历所有空格,找到 空格中草稿数字只有一个的空格。把这个草稿数字填入此空格。

    3.找完唯一数后,再找 隐含唯一数。(即这些自动草稿的空格中,同行(列、宫)中,只有某一个空格有草稿数A,这个格子的草稿数字就是隐含唯一数)。填写这个隐含唯一数到这个空格。

    找完后,回到1过程,重新计算一遍草稿数,再看看有没有唯一数和隐含唯一数。第2、3步骤如果有一次找到,就回到1过程。如果没有往下。

    算法:分行列宫三种情况依次查找,比如同行的,逐个检查1~9数字在这行中,草稿数字出现的次数,如果A出现次数是一次,含有这个草稿数字的空格a就是我们要找的格子,在这个空格a填写A数字。

    4.如果同行(列、宫)中,有两个空格都是{A,B}这样的草稿,那么删除这行(列、宫)中,其他空格草稿数中的A和B。2个{AB}这种情况叫数对。这个方法叫 数对删除法。这次查找把所有数对的情况都找到。如果遍历完成后,没有找到一个数对,往下。否则回到2过程。

    算法:分行列宫三种情况依次查找。比如检查某行。先计算,此行的空格数字是否>2,如果不成立,这行就不用检测了,继续检测下一行。如果成立,找到草稿数字为2的空格,比较这些空格,如果有相等的{CD}。说明这两个是数对,执行删除本行其他空格的草稿数字A和B。删除后继续在本行找数对,直达找不到后再检测下一行。

    5.三链数。概念:当某行(列、宫)的某三个空格中,相异的草稿数字不超过 3 个时,就可以把这 3 个数字自 本行(列、宫)的其它空格草稿数字中删减掉了。找到三链数{ABC}后,删除同行(列、宫)其他空格草稿数字中的A、B、C。这次查找把所有三链数的情况都找到。如果遍历完成后,没有找到一个三链数,往下。否则回到2过程。

    算法:尽请期待(如果谁有现成的算法,请留下言,谢谢!)

    6.隐含三链数。概念:当某 3 个数字出现在某行的某三个空格候选数中时,就可以把这三个空格的草稿数删减成该 3 个数字。也就是:设这三个数字是{ABC},把这3个空格草稿数字中的其他草稿数字{DEFG……}删除掉。找完行后,继续找列和宫。如果遍历完成后,没有找到一个隐含三链数,往下。否则回到2过程。注意:要有能删除的{DEFG……}才算隐含三链数。

    Share    

  • [转载]IOS view变为Image

    Tuesday, May 22, 2012 12:39AM / Standard Entry

    原文地址:IOS view变为Image作者:ios流星

    - (UIImage *) imageWithUIView:(UIView*) view

    {

    UIGraphicsBeginImageContext(view.bounds.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:ctx];

    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return tImage;

    }

    Share    

  • Core Image filters

    Sunday, May 20, 2012 11:53PM / Standard Entry

    CoreImage Filter很多,但文档教程很少。只能自己NSlog并研究

    NSArray *properties = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];

    NSLog(@"FilterName:\n%@", properties);//显示所有过滤器名字

    for (NSString *filterName in properties) {

    CIFilter *fltr = [CIFilter filterWithName:filterName];//用一个过滤器名字生成一个过滤器CIFilter对象

    NSLog(@"%@:\n%@", filterName, [fltr attributes]);//这个过滤器支持的属性

    }


    比如常用的 CIColorControls:

    {

    CIAttributeFilterCategories = (//类型

    CICategoryColorAdjustment,

    CICategoryVideo,

    CICategoryStillImage,

    CICategoryInterlaced,

    CICategoryNonSquarePixels,

    CICategoryBuiltIn

    );

    CIAttributeFilterDisplayName = "Color Controls";

    CIAttributeFilterName = CIColorControls;

    inputBrightness = {//亮度

    CIAttributeClass = NSNumber;

    CIAttributeDefault = 0;

    CIAttributeIdentity = 0;

    CIAttributeSliderMax = 1;

    CIAttributeSliderMin = "-1";

    CIAttributeType = CIAttributeTypeScalar;

    };

    inputContrast = {//对比度

    CIAttributeClass = NSNumber;

    CIAttributeDefault = 1;

    CIAttributeIdentity = 1;

    CIAttributeSliderMax = 4;

    CIAttributeSliderMin = 0;

    CIAttributeType = CIAttributeTypeScalar;

    };

    inputImage = {

    CIAttributeClass = CIImage;

    CIAttributeType = CIAttributeTypeImage;

    };

    inputSaturation = {//饱和度

    CIAttributeClass = NSNumber;

    CIAttributeDefault = 1;

    CIAttributeIdentity = 1;

    CIAttributeSliderMax = 2;

    CIAttributeSliderMin = 0;

    CIAttributeType = CIAttributeTypeScalar;

    };

    }


    首先设置好过滤器参数

    // 创建基于GPUCIContext对象

    context = [CIContext contextWithOptions: nil];


    filter = [CIFilter filterWithName:@"CIColorControls"];

    cisourceImg = [CIImage imageWithContentsOfURL:fileNameAndPath];

    [filter setValue:cisourceImg forKey:kCIInputImageKey];

    [filter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputSaturation"];//饱和度

    饱和度已经被定义成 kCIInputSaturationKey,可以直接用

    1.0必须介于min 和 max之间


    // 得到过滤后的图片

    CIImage *outputImage = [filter outputImage];


    // 转换图片

    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

    UIImage *newImg = [UIImage imageWithCGImage:cgimg];


    // 释放C对象

    CGImageRelease(cgimg);


    完************************


    当然filter可以一次同时设置多个值比如:

    [filter setValue:[NSNumber numberWithFloat:1.0] forKey:kCIInputSaturationKey];
    [filter setValue:[NSNumber numberWithFloat:1.0] forKey:kCIInputContrastKey];
    [filter setValue:[NSNumber numberWithFloat:1.0] forKey:kCIInputBrightnessKey];

    当你得到outputImage后,把这个outputImage作为另外一个filter的 cisourceImg,
    这样可以进行多次过滤,最后一轮过滤后,才转换成UIImage 显示。
    过滤效果非常高,不用担心速度。有没有想法做一个照相软件?处理图片呢?

    这个支持的ios5.0以上版本



    Share    

  • iphone通讯录读取写入

    Saturday, May 19, 2012 2:32PM / Standard Entry

    读取手机通讯录

    ABAddressBookRef addressBook = ABAddressBookCreate();


    读取联系人 小明

    CFStringRef cfName = CFSTR("小明");

    NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);


    people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCountpeople

    为了方便强制转换成了NSArray

    其中一个小明

    if(if ((people != nil) && [people count]>0))

    ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0);


    获取小明0的名字

    CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);


    获取小明0的电话信息

    ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);


    获取小明0的第0个电话类型:(比如 工作,住宅,iphone,移动电话等)

    CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0);


    获取小明0的第3个电话号码:(使用前先判断长度 ABMultiValueGetCount(cfphone)>4

    CFStringRef haoma =

    ABMultiValueCopyValueAtIndex(cfphone,3);


    添加一个联系人


    CFErrorRef anError = NULL;

    ABRecordRef aContact = ABPersonCreate();//联系人


    //名字

    NSString* name = @"小利";

    CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8);

    ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//写入名字进联系人

    //号码

    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

    ABMultiValueAddValueAndLabel(phone, @“13800138000”, kABPersonPhoneMobileLabel, NULL);//添加移动号码0

    ABMultiValueAddValueAndLabel(phone, @“18688888888”, kABPersonPhoneIPhoneLabel, NULL);//添加iphone号码1

    ⋯⋯ //添加多个号码


    ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//写入全部号码

    进联系人


    ABAddressBookAddRecord(addressBook, aContact, &anError);//写入通讯录


    //注意释放各数据

    CFRelease(cfsname);

    CFRelease(phone);

    CFRelease(aContact);

    CFRelease(addressBook);



    Share    

  • iOS延时加载图片

    Thursday, May 17, 2012 10:11PM / Standard Entry

    重网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法。
    一个tableView 列表,左边暂时没有图

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

    static NSString *CellIdentifier = @"myCell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

    {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle

    reuseIdentifier:CellIdentifier] autorelease];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }



    // 设置cell一些数据

    AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

    cell.textLabel.text = appRecord.appName;

    cell.detailTextLabel.text = appRecord.artist;

    // 如果不存在图片

    if (!appRecord.appIcon)

    {

    if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//不在拖动中和减速时,开始下载图片

    {

    [self startIconDownload:appRecord forIndexPath:indexPath];

    }

    //设置图片为空白图片(等待下载)

    cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];

    }

    //如果有图片

    else

    {

    cell.imageView.image = appRecord.appIcon;

    }


    return cell;

    }


    关键就是[self startIconDownload:appRecord forIndexPath:indexPath];

    - (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath

    {

    IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

    if (iconDownloader == nil) //已经在下载中的不用重复下载了,没有在下载中就往下走

    {

    iconDownloader = [[IconDownloader alloc] init];

    iconDownloader.appRecord = appRecord;

    iconDownloader.indexPathInTableView = indexPath;

    iconDownloader.delegate = self;

    [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];

    [iconDownloader startDownload];

    [iconDownloader release];

    }

    }


    IconDownloader 是一个下载图片封装类
    关键方法:iconDownloader.delegate = self;
    [iconDownloader startDownload];

    一个是委托,将来告诉self下载完成更新图片
    一个是自己方法开始联网下载图片

    委托调用方法,重设图片

    - (void)appImageDidLoad:(NSIndexPath *)indexPath

    {

    IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

    if (iconDownloader != nil)

    {

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:iconDownloader.indexPathInTableView];

    cell.imageView.image = iconDownloader.appRecord.appIcon;

    }

    }


    IconDownloader 中的方法

    - (void)startDownload

    {

    self.activeDownload = [NSMutableData data];


    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

    [NSURLRequest requestWithURL:

    [NSURL URLWithString:appRecord.imageURLString]] delegate:self];

    self.imageConnection = conn;

    [conn release];

    }


    最后 NSURLConnection的委托需要自己实现了。

    Share    

  • 421/9123456789>

Stats

  • 80后,双眼皮单身帅哥一个。...

    More

  • Age: 28
  • Gender: Male
  • Total visits: 8,962

RSS feed

    Share 分享到:


alivenotdead spotlight

Shout box

Please first sign in or sign up for FREE to post to the Shout Box.

Archived shouts

Join the alivenotdead.com community uniting musicians, filmmakers, and other artists with their fans