Skip to content

Instantly share code, notes, and snippets.

@kashi
Created November 14, 2013 06:22
Show Gist options
  • Save kashi/7462325 to your computer and use it in GitHub Desktop.
Save kashi/7462325 to your computer and use it in GitHub Desktop.
/*
http://nabetani.sakura.ne.jp/hena/ord13blocktup/
*/
#include <stdio.h>
#include <string.h>
#define FALSE 0
#define TRUE !FALSE
#define MAXWIDTH 100
int pour(char *s)
{
int i, j, d, len, cnt, drain[MAXWIDTH];
len = strlen(s);
for (i=0; i<len; i++) drain[i] = (*(s+i) == '0');
cnt = 0;
for (i=2; i<=9; i++) {
d = TRUE;
for (j=0; j<len; j++) {
if (*(s+j)-'0' >= i) { d = FALSE; continue; }
if (drain[j] == 1) d = TRUE;
drain[j] = d;
}
d = TRUE;
for (j=len-1; j>=0; j--) {
if (*(s+j)-'0' >= i) { d = FALSE; continue; }
if (drain[j] == 1) d = TRUE;
drain[j] = d;
if (!d) cnt++;
}
}
return cnt;
}
void test(char *s, char *expected)
{
char got[4];
sprintf(got, "%d", pour(s));
if (strcmp(got, expected) == 0) {
printf("%s : OK\n", s);
} else {
printf("%s : NG : got=%s, expected=%s\n", s, got, expected);
}
}
int main()
{
/*0*/ test( "83141310145169154671122", "24" );
/*1*/ test( "923111128", "45" );
/*2*/ test( "923101128", "1" );
/*3*/ test( "903111128", "9" );
/*4*/ test( "3", "0" );
/*5*/ test( "31", "0" );
/*6*/ test( "412", "1" );
/*7*/ test( "3124", "3" );
/*8*/ test( "11111", "0" );
/*9*/ test( "222111", "0" );
/*10*/ test( "335544", "0" );
/*11*/ test( "1223455321", "0" );
/*12*/ test( "000", "0" );
/*13*/ test( "000100020003121", "1" );
/*14*/ test( "1213141516171819181716151413121", "56" );
/*15*/ test( "712131415161718191817161514131216", "117" );
/*16*/ test( "712131405161718191817161514031216", "64" );
/*17*/ test( "03205301204342100", "1" );
/*18*/ test( "0912830485711120342", "18" );
/*19*/ test( "1113241120998943327631001", "20" );
/*20*/ test( "7688167781598943035023813337019904732", "41" );
/*21*/ test( "2032075902729233234129146823006063388", "79" );
/*22*/ test( "8323636570846582397534533", "44" );
/*23*/ test( "2142555257761672319599209190604843", "41" );
/*24*/ test( "06424633785085474133925235", "51" );
/*25*/ test( "503144400846933212134", "21" );
/*26*/ test( "1204706243676306476295999864", "21" );
/*27*/ test( "050527640248767717738306306596466224", "29" );
/*28*/ test( "5926294098216193922825", "65" );
/*29*/ test( "655589141599534035", "29" );
/*30*/ test( "7411279689677738", "34" );
/*31*/ test( "268131111165754619136819109839402", "102" );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment