【CQOI2013】新数独 爆搜

题目描述

  题目描述

题目大意

解数独。。。

样例输入

见代码垃圾markdown编辑器

样例输出

5 3 9 4 6 8 2 1 7
2 4 8 1 9 7 3 5 6
1 6 7 2 3 5 9 8 4
6 8 1 7 4 2 5 9 3
3 7 5 9 1 6 8 4 2
9 2 4 5 8 3 7 6 1
7 9 6 8 2 1 4 3 5
4 1 2 3 5 9 6 7 8
8 5 3 6 7 4 1 2 9

解题思路

搜索搜索

代码

/*
 > <   < <   > <
v ^ v v ^ v ^ ^ v
 < <   < >   < <
v ^ v ^ v v ^ ^ v
 < <   < <   > >
 < >   > >   < >
v v ^ ^ v ^ ^ v v
 < >   > <   > >
^ v v v ^ v v ^ v
 > <   < >   > >
 < >   > >   > <
v v v v ^ ^ ^ ^ ^
 > <   < <   < <
^ ^ ^ ^ ^ v v v ^
 > >   < >   < <
*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
char Map[20][20];
bool vish[10][10];
bool viss[10][10];
bool visk[10][10];
int judg[10][10];//1:比左大,2:比上大
int vl[10][10];
int shoudonglisan[16]={0,1,2,2,3,3,4,5,5,6,6,7,8,8,9,9};
int shoudonglisanyzuobiao[7]={0,2,3,5,6,8,9};
void Init(){
    for(int i=1;i<=15;i++){
        for(int j=1;j<=((i%5==2||(i%5==4))?9:6);j++){
            cin>>Map[i][j];
            if(Map[i][j]=='^')
                judg[shoudonglisan[i]][j]+=2;
            if(Map[i][j]=='<')
                judg[shoudonglisan[i]][shoudonglisanyzuobiao[j]]+=1;
        }
    }
}
inline int ff(int x,int y){int delta=3*((x-1)/3);return delta+(y-1)/3+1;}
inline int hh(int x,int y){x=x%3;if(!x)x+=3;y=y%3;if(!y)y+=3;return (x-1)*3+y;}
void dfs(int x,int y){
    if(x==10){
        for(int i=1;i<=9;i++,cout<<"\n")
            for(int j=1;j<=9;j++)
                cout<<vl[i][j]<<" ";
        exit(0);
    }
    if(y==10){
        dfs(x+1,1);
        return;
    }
    for(int i=1;i<=9;i++){
        if(vish[x][i])continue;
        if(viss[y][i])continue;
        if(visk[ff(x,y)][i])continue;
        if(hh(x,y)==1);
        else if(hh(x,y)==2||hh(x,y)==3){
            if((judg[x][y]&1)==1&&i<vl[x][y-1])continue;
            if((judg[x][y]&1)==0&&i>vl[x][y-1])continue;
        }
        else if(hh(x,y)==4||hh(x,y)==7){
            if((judg[x][y]&2)==2&&i<vl[x-1][y])continue;
            if((judg[x][y]&2)==0&&i>vl[x-1][y])continue;
        }
        else {
            if((judg[x][y]&1)==1&&i<vl[x][y-1])continue;
            if((judg[x][y]&1)==0&&i>vl[x][y-1])continue;
            if((judg[x][y]&2)==2&&i<vl[x-1][y])continue;
            if((judg[x][y]&2)==0&&i>vl[x-1][y])continue;
        }
        if(i==1&&hh(x,y)<=6&&(judg[x+1][y]&2)==0)continue;
        if(i==1&&hh(x,y)!=3&&hh(x,y)!=6&&hh(x,y)!=9&&(judg[x][y+1]&1)==0)continue;
        if(i==9&&hh(x,y)<=6&&(judg[x+1][y]&2)==2)continue;
        if(i==9&&hh(x,y)!=3&&hh(x,y)!=6&&hh(x,y)!=9&&(judg[x][y+1]&1)==1)continue;
        vish[x][i]=true;
        viss[y][i]=true;
        visk[ff(x,y)][i]=true;
        vl[x][y]=i;
        dfs(x,y+1);
        vish[x][i]=false;
        viss[y][i]=false;
        visk[ff(x,y)][i]=false;
    }
}
int main(){
    memset(vish,0,sizeof(vish));
    memset(viss,0,sizeof(viss));
    memset(visk,0,sizeof(visk));
    memset(judg,0,sizeof(judg));
    Init();
    dfs(1,1);
    return 0;
}

转载于:https://www.cnblogs.com/Cedric341561/p/6811038.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值