Skip to content

Instantly share code, notes, and snippets.

@fkino
Created December 12, 2015 12:05
Show Gist options
  • Save fkino/a57a973c44a6224122cc to your computer and use it in GitHub Desktop.
Save fkino/a57a973c44a6224122cc to your computer and use it in GitHub Desktop.
星めぐり 〜 第2回 ESM オフラインどう書く 例題 回答
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const unsigned char Direction[10][2] = {
/* R , W */
{'H', 'I'}, /*A*/
{'D', 'G'}, /*B*/
{'J', 'A'}, /*C*/
{'F', 'I'}, /*D*/
{'B', 'C'}, /*E*/
{'H', 'A'}, /*F*/
{'D', 'E'}, /*G*/
{'J', 'C'}, /*H*/
{'F', 'G'}, /*I*/
{'B', 'E'}, /*J*/
};
int next(const unsigned char* color, unsigned char* route){
return (*color != 0x00) &&
(*route = Direction[*route++ - 'A'][*color & 0x01]) &&
next(++color, route);
}
void trace(const unsigned char* input, unsigned char* route){
*route = *input;
next(++input, route);
}
void test(const unsigned char* input, const unsigned char* expected) {
unsigned char* actual = (unsigned char*)calloc(strlen(input) + 1,
sizeof(unsigned char));
trace(input, actual);
printf("%s %s %s\n",
!strcmp(expected, actual) ? "\033[32mOK\033[39m" : "\033[31mNG\033[39m",
expected, actual);
free(actual);
}
int main(int argc, char* argv[]){
test("AW", "AI");
test("GR", "GD");
test("GW", "GE");
test("IR", "IF");
test("HR", "HJ");
test("BWW", "BGE");
test("ARW", "AHC");
test("GRR", "GDF");
test("BWR", "BGD");
test("JWWW", "JECA");
test("DRRR", "DFHJ");
test("CWWR", "CAIF");
test("HWWW", "HCAI");
test("GWRWR", "GEBGD");
test("FRRRW", "FHJBG");
test("JRRWW", "JBDIG");
test("JWWRRW", "JECJBG");
test("GRRRWW", "GDFHCA");
test("BRWRWR", "BDIFAH");
test("IRWRRWR", "IFAHJEB");
test("IWWWRRW", "IGECJBG");
test("GWWRWWR", "GECJECJ");
test("HRRWRWRW", "HJBGDIFA");
test("FRWWWRRW", "FHCAIFHC");
test("HRWWWRWRW", "HJECAHCJE");
test("CWWWRRWWW", "CAIGDFAIG");
test("BRRRWRRRRW", "BDFHCJBDFA");
test("FRWRRWRRWW", "FHCJBGDFAI");
test("GWRRRRWRWRW", "GEBDFHCJEBG");
test("DRWWWWWWRRW", "DFAIGECAHJE");
test("ARRRRWRRRRWW", "AHJBDIFHJBGE");
test("AWWWWWWRRWRR", "AIGECAIFHCJB");
test("JWWWRRWRWRWWR", "JECAHJEBGDIGD");
test("CRWRWRRWWWRWW", "CJEBGDFAIGDIG");
test("DWRWRWRWRWWRWW", "DIFAHCJEBGEBGE");
test("GRWWWRRRRWRWRR", "GDIGEBDFHCJEBD");
test("ARWWWRWWRWWWWWW", "AHCAIFAIFAIGECA");
test("DWRWRRWRWWRWWRW", "DIFAHJEBGEBGEBG");
test("JRWRRRRRWRRRRRWR", "JBGDFHJBGDFHJBGD");
test("IRWWRRWWWWWRRWWR", "IFAIFHCAIGEBDIGD");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment