Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

神级菜鸟的代码,大神轻喷

Posted by DvZ at 2012-08-21 13:36:09 on Problem 2676
#include<stdio.h>
int map[10][10],flag;
struct tmp
{
	int x,y;
}rec[81];
int check(int x,int y,int num)
{
	int i;
	for(i=0;i<9;i++)//检查横竖
	{
		if(map[x][i]==num)
			return 0;
		if(map[i][y]==num)
			return 0;
	}
			int p,q;
	for(p=x/3*3;p<=x/3*3+2;p++)//英语看不懂,不知道3*3里不能有同样的数字= =纠结半天
		{
			for(q=y/3*3;q<=y/3*3+2;q++)
			{
				if(map[p][q]==num)
					return 0;
			}
		}
	return 1;
}
void dfs(int countr)
{
	if(countr==-1)
	{
		flag=1;
		return ;
	}
	for(int i=1;i<=9;i++)
	{
		if(check(rec[countr].x,rec[countr].y,i))
		{
			map[rec[countr].x][rec[countr].y]=i;//填格
			dfs(countr-1);
			if(flag)
				return ;
			map[rec[countr].x][rec[countr].y]=0;//回溯
		}
	}
	return ;
}
int main()
{
	int t,countr,i,j;
	char s[10];
	scanf("%d\n",&t);
	while(t--)
	{
		flag=0;
		countr=0;
		for(i=0;i<9;i++)
		{
			gets(s);
			for(j=0;j<9;j++)
			{	
				map[i][j]=s[j]-'0';
				if(!map[i][j])//记录要填的格子
				{
					rec[countr].x=i;
					rec[countr++].y=j;
				}
			}
		}
		dfs(--countr);//据说后面搜起比较快= =
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
				printf("%d",map[i][j]);
			printf("\n");
		}
	}
	return 0;
}

没有初始化,本来以为会wa想不到过了,那就算了

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator