0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cut.c

Last updated at Posted at 2014-01-11

問題はこちら
http://nabetani.sakura.ne.jp/hena/ord17foldcut/

他の解答例はこちら
http://qiita.com/Nabetani/items/ebd9d7deb30c57447806

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

int chrcnt(const char* s, const char c)
{
	int cnt = 0;
	for(int i = 0; s[i]; i++) {
		if(s[i]==c) cnt++;
	}
	return cnt;
}

int chgcnt(const char* s, const char c1, const char c2)
{
	int cnt = 0;
	char c = c1;
	for(int i = 0; s[i]; i++) {
		if(s[i]==c1 && c==c1) {
			c = c2;
			cnt++;
		} else if (s[i]==c2 && c==c2) {
			c = c1;
			cnt++;
		}
	}
	return cnt;
}

int solve(const char* IN)
{
		// 縦線の数を計算
	int vert = (1<<(chrcnt(IN, 'R')+chrcnt(IN, 'L')))-1,	
		// 横線の数を計算
		hori = (1<<(chrcnt(IN, 'T')+chrcnt(IN, 'B')))-1,	
	
		// 左右の入れ替わりの回数算出
		chgcnt_vert = (strstr(IN, "r") != NULL) ? chgcnt(IN,'R','L'): chgcnt(IN,'L','R'),	
		// 上下の入れ替わりの回数算出
		chgcnt_hori = (strstr(IN, "t") != NULL) ? chgcnt(IN,'T','B'): chgcnt(IN,'B','T'),	
	
		// 穴が開く縦線が奇数か偶数か確認し、本数を算出
		v = ((chgcnt_vert&0x01)==0) ? vert>>1: (vert+1)>>1,	
		// 穴が開く横線が奇数か偶数か確認し、本数を算出
		h = ((chgcnt_hori&0x01)==0) ? hori>>1: (hori+1)>>1;	
	
	return v*h;	// 穴の数を算出
}

void test(const char* IN, const char* ANS)
{
	static int i = 0;
	int a = solve(IN);
	printf("%d\t%d\t%s\n",i++, a, (a == atoi(ANS)) ? "ok": "*** NG ***");
}

int main()
{
/*0*/ test( "RRTRB-bl", "6" );    
/*1*/ test( "R-tr", "0" );    
/*2*/ test( "L-br", "0" );    
/*3*/ test( "T-tl", "0" );    
/*4*/ test( "B-tl", "0" );    
/*5*/ test( "BL-br", "0" );    
/*6*/ test( "LB-tl", "0" );    
/*7*/ test( "RL-tl", "0" );    
/*8*/ test( "BL-tl", "0" );    
/*9*/ test( "TL-bl", "0" );    
/*10*/ test( "RT-tr", "1" );    
/*11*/ test( "TRB-tl", "0" );    
/*12*/ test( "TRL-bl", "0" );    
/*13*/ test( "TRB-br", "2" );    
/*14*/ test( "LLB-bl", "2" );    
/*15*/ test( "RTL-tr", "1" );    
/*16*/ test( "LBB-tr", "0" );    
/*17*/ test( "TLL-tl", "2" );    
/*18*/ test( "RLRR-tr", "0" );    
/*19*/ test( "BBTL-tl", "4" );    
/*20*/ test( "TBBT-tr", "0" );    
/*21*/ test( "LLBR-tl", "0" );    
/*22*/ test( "LBRT-tl", "2" );    
/*23*/ test( "RLBL-bl", "4" );    
/*24*/ test( "BRRL-br", "3" );    
/*25*/ test( "TBBTL-tl", "8" );    
/*26*/ test( "TLBBT-br", "0" );    
/*27*/ test( "LRBLL-br", "7" );    
/*28*/ test( "TRRTT-br", "6" );    
/*29*/ test( "BBBLB-br", "0" );    
/*30*/ test( "RTTTR-tl", "4" );    
/*31*/ test( "BBLLL-br", "6" );    
/*32*/ test( "RRLLTR-tr", "16" );    
/*33*/ test( "TTRBLB-br", "8" );    
/*34*/ test( "LRBRBR-bl", "14" );    
/*35*/ test( "RBBLRL-tl", "8" );    
/*36*/ test( "RTRLTB-tl", "12" );    
/*37*/ test( "LBLRTR-tl", "14" );    
/*38*/ test( "RRLTRL-tl", "16" );    
/*39*/ test( "TBLTRR-br", "12" );    
/*40*/ test( "TTTRLTT-bl", "30" );    
/*41*/ test( "TBBRTBL-tr", "15" );    
/*42*/ test( "TRTRTLL-tr", "28" );    
/*43*/ test( "TLLRTRB-tr", "24" );    
/*44*/ test( "RLLBRLB-tr", "15" );    
/*45*/ test( "LTLRRBT-tr", "32" );    
/*46*/ test( "RBBRBLT-br", "21" );    
/*47*/ test( "LLRLRLR-tr", "0" ); 
	return 0;
}
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?