star

搜索

RSS

RSS Link

计数器

140257

迷你小游戏

2010年8月21日 03:46 | Comments(3) | Category:C语言 | Tags:

#include <stdio.h>
#define MAXLEN 10	//最大8*8阵 
#defint MAXSTEP 100 //最多100步 
int DesMar[MAXLEN][MAXLEN];//目标矩阵 
int SrcMar[MAXLEN][MAXLEN];//原矩阵 
int N, NeedTimes;//矩阵大小和需要次数
struct pos {int x, y;}res[MAXSTEP];//解 

void TurnMar(int x, int y)//转一下 
{
	int i, j;
	for(i = x-1; i <= x+1; ++i)
		for(j = y-1; j <= y+1; ++j)
			SrcMar[i][j] ^= 1;
}

int Checked()//检查是否为解 
{
	int i, j;
	for(i = 1; i <= N; ++i)
		for(j = 1; j <= N; ++j)
			if(SrcMar[i][j] != DesMar[i][j])
				return 0;
	return 1;
}

int DFS(int n)//搜索 
{
	int i, j;
	if(n >= NeedTimes)
		if(Checked())
			return 1;
		else
			return 0;
	for(i = 1; i <= N; ++i)
		for(j = 1; j <= N; ++j)
		{
			TurnMar(i, j);
			res[n].x = i, res[n].y = j;
			if(!DFS(n+1))
				TurnMar(i, j);
			else
				return 1;
		}
	return 0;
}

int main(void)
{
	int i, j;
	while(scanf("%d", &N),N)//输入0退出程序 
	{
		puts("input the source info:");
		for(i = 1; i <= N; ++i)//输入原矩阵信息 
			for(j = 1; j <= N; ++j)
				scanf("%d", &SrcMar[i][j]);
		puts("input the destination info:");
		for(i = 1; i <= N; ++i)//输入目标矩阵信息 
			for(j = 1; j <= N; ++j)
				scanf("%d", &DesMar[i][j]);
		puts("input the times:");//输入次数 
		scanf("%d", &NeedTimes);

		DFS(0);

		for(i = 0; i < NeedTimes; ++i)
			printf("%d %d\n", res[i].x, res[i].y);
	}
	return 0;
}

达夫的设备

2010年8月12日 23:14 | Comments(7) | Category:C++ | Tags:

void duff_memcpy(char* to, char* from, size_t count)
{
    size_t n = (count+7) / 8;
    switch(count % 8) 
	{
    case 0: do{ *to++ = *from++;
    case 7:     *to++ = *from++;
    case 6:     *to++ = *from++;
    case 5:     *to++ = *from++;
    case 4:     *to++ = *from++;
    case 3:     *to++ = *from++;
    case 2:     *to++ = *from++;
    case 1:     *to++ = *from++;
    		}while(--n>0);
    }
} 

N皇后问题

2010年8月10日 00:22 | Comments(22) | Category:C语言 | Tags:

#include <stdio.h>

static int upperlim, sum; 

void binqueen(int row, int ld, int rd)
{
    int pos, p;
    if(row != upperlim)
    {
        pos = upperlim & ~(row | ld | rd);
        while (pos)
        {
            p = pos & -pos;
            pos -= p;
            binqueen(row+p, (ld+p)<<1, (rd+p)>>1);
        }
    }
    else sum++;
}


int main(void)
{
    int n;
    scanf("%d", &n);
    upperlim = (1<<n) - 1;
    binqueen(0, 0, 0);
    printf("%d", sum);
    return 0;
}

卡特兰数

2010年8月08日 23:29 | Comments(12) | Category:C语言 | Tags:

$$C(0) = 1$$

$$C(n) = \frac{C(n-1)(4n-2)}{n+1} (n>0)$$

#include <stdio.h>
int C[101][101], len[101];

int main(void)
{
	int i, j, r, t, l;
	C[0][0] = 1;
	len[0]  = 1;
	l       = 1;        //长度
	
	for(i = 1; i <= 100; ++i)
	{
		for(j = 0; j < l; ++j)        //乘法
			C[i][j] = C[i-1][j] * (4*i-2);
		for(r=j = 0; j < l; ++j)
		{
			t       = C[i][j] + r;
			C[i][j] = t % 10;
			r       = t / 10;
		}
		while(r)
			C[i][l++] = r % 10, r /= 10;
		for(j = l-1, r = 0; j >= 0; --j)        //除法
		{
			t       = r*10 + C[i][j];
			C[i][j] = t / (i+1);
			r       = t % (i+1);
		}
		while(!C[i][l-1])
			--l;
		len[i] = l;
	}
			
	for(i = 1; i <= 100; ++i)
	{
		printf("%d:", i);
		for(j = len[i]-1; j >= 0; --j)
			printf("%d", C[i][j]);
		putchar(10);
	}
	
	return 0;
}

乱代码之右三角

2010年8月08日 05:18 | Comments(7) | Category:C语言 | Tags:

Just For Fun.

输出下列图形N=4时:

*
***
*****
*******
*****
***
*

#define N 4
#include <stdio.h>
main(_)
{static int __=1;_<=N*N?putchar('*'),__*__==_?++__,putchar(10):_:--__,_;
_<=N*N?main(++_),_-=1+1,_<(1-N)*(1-N)?putchar((1<<(1<<2)+1)+10),
_==(__-1-1)*(__-1-1)?puts(""),--__:1:2:3;}