| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
正搜469MS 倒搜16MS。。就是简单的根据行 列 以及 3*3的方块 哈希。。然后一开始傻逼的开了4维数组。。
代码写挫了QAQ。。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char ma[11][11];
bool row[11][11],col[11][11],mar[11][11],ok;
int change(int x,int y)
{
if(x>=1&&x<=3) return y%3==0?y/3:y/3+1;
else if(x>=4&&x<=6) return 3+(y%3==0?y/3:y/3+1);
else return 6+(y%3==0?y/3:y/3+1);
}
void dfs(int num)
{
if(ok) return ;
if(num==82)
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
putchar(ma[i][j]);
puts("");
}
ok=1;
return ;
}
int x=num%9==0?num/9:num/9+1;
int y=num%9==0?9:num%9;
if(ma[x][y]-'0')
dfs(num+1);
else
{
for(int i=1;i<=9;i++)
{
if(!row[x][i]&&!col[y][i]&&!mar[change(x,y)][i])
{
ma[x][y]=i+'0';row[x][i]=1;col[y][i]=1;mar[change(x,y)][i]=1;
dfs(num+1);
ma[x][y]='0';row[x][i]=0;col[y][i]=0;mar[change(x,y)][i]=0;
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
memset(mar,0,sizeof(mar));
for(int i=1;i<=9;i++)
scanf("%s",ma[i]+1);
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
if(ma[i][j]-'0')
{
row[i][ma[i][j]-'0']=1;
col[j][ma[i][j]-'0']=1;
mar[change(i,j)][ma[i][j]-'0']=1;
}
ok=0;dfs(1);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator