LoginSignup
1

More than 5 years have passed since last update.

「ビットあみだくじ 〜 横へな」の回答

Posted at

ルールはこちら
http://nabetani.sakura.ne.jp/hena/ord11bitamida/

基本方針
1.あみだの横一列を取り出す
2.ビット1の開始位置を記録
3.ビット1の終端位置にたどり着いたら、開始位置と交換
4.2-3をビット列の終わりまで繰り返す
5.1-4をあみだの最後まで繰り返す

amida.c
/*
 * ビットあみだくじ ? 横へな 2013.6.1
 *      http://nabetani.sakura.ne.jp/hena/ord11bitamida/
 *
 * あみだの途中経過を表示させる場合は ___PRINT___ を定義する
 */

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

//#define ___PRINT___
#define swap(a,b) a^=b^=a^=b

// あみだの線を表示する
void printLine(int x)
{
    int i;

    printf("|");
    for( i = 0; i < 8; i++ ) {
        printf("%c|", ((x << i ) & 0x80) ? '-':' ' );
    }
    puts("");
}

// 途中経過を表示する
void printNums(int x[9])
{
    int i;

    for( i = 0; i < 9; i++ ) {
        printf("%d ", x[i]);
    }
    puts("");
}
#ifndef ___PRINT___
#   undef printLine
#   undef printNums
#   define printLine 1 ? (void)0 : (void)//
#   define printNums 1 ? (void)0 : (void)//
#endif

char* solve(char* hex)
{
    int i, j, m;
    char* s;
    int nums[9] = {0,1,2,3,4,5,6,7,8};
    int yoko[4];
    static char ans[9];

    // あみだを定義した16進数文字列を数値に変換
    i = 0;
    s = strtok(hex, "-");
    while(s) {
        sscanf(s, "%x", &yoko[i]);
        s = strtok(NULL, "-");
        i++;
    }

    // 1行ずつ解く
    printNums(nums);            // 初期番号位置
    for( i = 0; i < 4; i++ ) {
        printLine(yoko[i]);     // あみだの線
        m = -1;
        for( j = 0; j < 9; j++ ) {
            if( ( yoko[i] << j ) & 0x80 ) {
                if( m == -1 ) {
                    m = j;                  // 横線開始位置
                }
            } else {
                if( m != -1 ) { 
                    swap(nums[m], nums[j]); // 交換する
                    m = -1;
                }
            }
        }
        printNums(nums);        // 移動結果
    }

    // 回答結果を文字列へ
    for( i = 0; i < 9; i++ ) {
        ans[i] = '0' + nums[i];
    }

    return ans;
}

void test(char* amida, char* answer)
{
    static int count = 0;
    char* reply;

    reply = solve(amida);
    if( strcmp(reply, answer) == 0 ) {
        printf("%02d : OK [%s]\n", count, reply);
    } else {
        printf("%02d : NG\n", count);
        exit(1);
    }
    count++;
}

void main()
{
    /*0*/ test( "d6-7b-e1-9e", "740631825" );
    /*1*/ test( "83-4c-20-10", "123805476" );
    /*2*/ test( "fb-f7-7e-df", "274056813" );
    /*3*/ test( "55-33-0f-ff", "123456780" );
    /*4*/ test( "00-00-00-00", "012345678" );
    /*5*/ test( "00-00-00-55", "021436587" );
    /*6*/ test( "40-10-04-01", "021436587" );
    /*7*/ test( "00-00-aa-00", "103254768" );
    /*8*/ test( "80-20-08-02", "103254768" );
    /*9*/ test( "ff-7e-3c-18", "876543210" );
    /*10*/ test( "aa-55-aa-55", "351708264" );
    /*11*/ test( "55-aa-aa-55", "012345678" );
    /*12*/ test( "db-24-db-e7", "812543670" );
    /*13*/ test( "00-01-00-40", "021345687" );
    /*14*/ test( "00-00-80-00", "102345678" );
    /*15*/ test( "01-40-00-00", "021345687" );
    /*16*/ test( "00-00-00-02", "012345768" );
    /*17*/ test( "00-00-02-00", "012345768" );
    /*18*/ test( "00-14-00-00", "012436578" );
    /*19*/ test( "00-00-01-40", "021345687" );
    /*20*/ test( "00-80-01-00", "102345687" );
    /*21*/ test( "c8-00-00-81", "120354687" );
    /*22*/ test( "05-48-08-14", "021435687" );
    /*23*/ test( "24-05-00-f0", "413205687" );
    /*24*/ test( "40-08-14-01", "021536487" );
    /*25*/ test( "18-c8-80-80", "210534678" );
    /*26*/ test( "1c-88-52-00", "120564738" );
    /*27*/ test( "ec-dc-67-62", "213468705" );
    /*28*/ test( "0a-b6-60-e9", "035162784" );
    /*29*/ test( "52-d6-c6-c2", "120345678" );
    /*30*/ test( "47-e7-b0-36", "231047658" );
    /*31*/ test( "0f-85-91-aa", "108263754" );
    /*32*/ test( "76-b6-ed-f3", "601435782" );
    /*33*/ test( "f5-5e-f7-3d", "025847163" );
    /*34*/ test( "dd-e7-fb-f9", "610247538" );
    /*35*/ test( "8f-f4-af-fd", "583246017" );
    /*36*/ test( "bf-fb-cb-f7", "105382674" );
    /*37*/ test( "e5-fd-ff-ff", "512046378" );
    /*38*/ test( "ef-df-ef-fe", "713205648" );
    /*39*/ test( "bf-7f-fd-d7", "826437105" );
    /*40*/ test( "36-ff-df-de", "814527603" );
    /*41*/ test( "6f-dd-ff-ff", "230685147" );
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1