star

搜索

RSS

RSS Link

计数器

139738

迷你小游戏

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;
}