POJ 2918 Tudoku [搜索]

和POJ2676一样哈,,,

原谅我水题目数 = =!。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
int map[10][10];
char tmp[10][10];
bool row[10][10];
bool col[10][10];
bool grid[10][10];
bool DFS(int x,int y)
{
	int here=3*((x-1)/3)+(y-1)/3+1;
	if(x==10)
		return true;
	if(map[x][y])
	{
		bool flag;
		return y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
	}
	else
	{
		for(int num=1;num<=9;num++)
		{
			if(!row[x][num]&&!col[y][num]&&!grid[here][num])
			{
				map[x][y]=num;
				row[x][num]=true;
				col[y][num]=true;
				grid[here][num]=true;
				bool flag;
				y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
				if(flag)
					return true;
				else
				{
					map[x][y]=0;
					row[x][num]=false;
					col[y][num]=false;
					grid[here][num]=false;
				}
			}
		}
	}
	return false;
}
int main()
{
   //freopen("/home/rainto96/in.txt","r",stdin);
	int test;
	cin>>test;
	for(int times=1;times<=test;times++)
	{
		memset(map,0,sizeof(map));
		memset(tmp,0,sizeof(tmp));
		memset(grid,0,sizeof(grid));
		memset(col,0,sizeof(col));
		memset(row,0,sizeof(row));
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=9;j++)
			{
				cin>>tmp[i][j];
				int here=3*((i-1)/3)+(j-1)/3+1;
				map[i][j]=tmp[i][j]-'0';
				if(map[i][j])
				{
					row[i][map[i][j]]=true;
					col[j][map[i][j]]=true;
					grid[here][map[i][j]]=true;
				}
			}
		}
		DFS(1,1);
		cout<<"Scenario #"<<times<<":\n";
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=9;j++)
			{
				cout<<map[i][j];
			}
			cout<<'\n';
		}
		cout<<'\n';
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值