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

0MS呀,一次AC,发代码留恋

Posted by hepeng597 at 2009-05-09 10:43:03 on Problem 2676
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

#define   max(a,b)    ((a)>(b)?(a):(b))
#define   min(a,b)    ((a)<(b)?(a):(b))
#define   sqr(a)         ((a)*(a))
#define   rep(i,a,b)  for(i=(a);i<(b);i++)
#define   REP(i,n)     rep(i,0,n)
#define   inf         1000000000
#define N 10
char g[N][N];
bool row[N][N];
bool col[N][N];
bool square[N][N];
stack<int> s;
int getsquare(int x,int y)
{
	return (x-1)/3*3+(y-1)/3+1;
}
bool check(int x,int y,int value)
{
	return !row[x][value]&&!col[y][value]&&!square[getsquare(x,y)][value];
}
bool solve()
{
	int i;
	bool f=false;
	if(!s.empty())
	{
		int index=s.top();
		s.pop();
		int x=index/9+1;
		int y=index%9+1;
		rep(i,1,N)
		{
			if(check(x,y,i))
			{
				f=true;
				row[x][i]=true;
				col[y][i]=true;
				square[getsquare(x,y)][i]=true;
				g[x][y]=i+'0';
				if(solve())
				{
					return true;
				}else
				{
					f=false;
					row[x][i]=false;
					col[y][i]=false;
					square[getsquare(x,y)][i]=false;
					g[x][y]='0';
				}
			}
		}
		if(!f) 
		{
			s.push(index);
			return false;
		}
	}
	return true;
}
int main()
{
	int Case,i,j;
	scanf("%d",&Case);
	getchar();
	while(Case--)
	{
		memset(row,0,sizeof row);
		memset(col,0,sizeof col);
		memset(square,0,sizeof square);
		rep(i,1,N)
		{
			rep(j,1,N)
			{
				scanf("%c",&g[i][j]);
				if(g[i][j]!='0')
				{
					row[i][g[i][j]-'0']=true;
					col[j][g[i][j]-'0']=true;
					square[getsquare(i,j)][g[i][j]-'0']=true;
				}else
				{
					s.push((i-1)*9+j-1);
				}
			}
			getchar();
		}
		if(solve()) 
			rep(i,1,N) 
			{
				rep(j,1,N) printf("%c",g[i][j]);
				printf("\n");
			}
	}
	return 0;
}

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