Sudoku Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6799 Accepted Submission(s): 2118
Problem Description
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。
据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。
数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。
例题:
答案:
据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。
数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。
例题:
答案:
Input
本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。
Output
对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。
对于每组测试数据保证它有且只有一个解。
对于每组测试数据保证它有且只有一个解。
Sample Input
7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1 3 6 7 2 9 2 4 ? 5 6 ? 3 7 5 ? 6 ? ? ? 2 4 1 1 ? 3 7 2 ? 9 ? 5 ? ? 1 9 7 5 4 8 6 6 ? 7 8 3 ? 5 1 9 8 5 9 ? 4 ? ? 2 3
Sample Output
7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1 9 4 4 9 8 5 1 3 6 7 2 9 2 4 1 5 6 8 3 7 5 7 6 3 9 8 2 4 1 1 8 3 7 2 4 9 6 5 2 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 2 3
思路:把要填的数字的位置保存下来,然后去枚举填哪个数字就好
不用害怕会超时,题目给的三个剪枝条件已经减掉足够多的枝了,因为保证了只有一个解,当有一个解后就一直出栈即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 9
int n,m;
char ma[N][N];
bool vis[N][N][N][N][N][N][N][N];///需要三个箱子和人位置确定才能确定状态
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
struct Node
{
int step;
int x[4],y[4];
};
struct Node1
{
int step;
int x,y;
};
int check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m||ma[x][y]=='#') return 0;
return 1;
}
int bfs()
{
int cnt=0;
Node a,next;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(ma[i][j]=='X')
a.x[3]=i,a.y[3]=j;
else if(ma[i][j]=='*')
a.x[cnt]=i,a.y[cnt++]=j;
}
a.step=0;
queue<Node>que;
que.push(a);
memset(vis,false,sizeof(vis));
vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]]=true;
while(!que.empty())
{
Node now=que.front();
que.pop();
if(ma[now.x[0]][now.y[0]]=='@'&&ma[now.x[1]][now.y[1]]=='@'&&ma[now.x[2]][now.y[2]]=='@')
return now.step;
for(int i=0; i<4; i++)
{
next=now;
next.x[3]+=dir[i][0],next.y[3]+=dir[i][1];
if(!check(next.x[3],next.y[3])) continue;
int flag=0;
for(int j=0; j<3; j++)
if(next.x[3]==next.x[j]&&next.y[3]==next.y[j])
{
next.x[j]+=dir[i][0],next.y[j]+=dir[i][1];
if(!check(next.x[j],next.y[j]))
flag=1;
for(int k=0;k<3;k++)
if(j!=k&&next.x[j]==next.x[k]&&next.y[j]==next.y[k])
flag=1;
}
if(flag) continue;
if(vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]]) continue;
vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]]=1;
next.step=now.step+1;
que.push(next);
}
}
return -1;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=0; i<n; i++)
scanf("%s",ma[i]);
printf("%d\n",bfs());
}
return 0;
}