关闭

3109: [cqoi2013]新数独

154人阅读 评论(0) 收藏 举报
分类:

3109: [cqoi2013]新数独

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 388  Solved: 239
[Submit][Status][Discuss]

Description

Input

输入一共15行,包含一个新数独的实例。第奇数行包含左右方向的符号(<和>),第偶数行包含上下方向的符号(^和v)。
 

Output

输出包含9行,每行9个1~9的数字,以单个空格隔开。输入保证解惟一。

Sample Input

< > > < > <
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
< > < > < >

Sample Output

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

HINT

Source

[Submit][Status][Discuss]




被爆搜教做人

思路很显然,用大小关系和数独本身性质剪枝

但是任何函数都要有返回值

但是任何函数都要有返回值

但是任何函数都要有返回值

dfs如果无结果请return 0


#include<iostream>  
#include<cstdio>  
#include<cstring>  
using namespace std;  

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};

int n,m,bo[10][10][10],p[10][10][10][10],L[10][10],R[10][10],a[10][10];  
char ch[20];  
  
void MAKE(int x,int y)  
{  
    for (int i = 0; i < 2; i++) {  
        int t = y*2 - 1;  
        if (ch[t] == '<') p[x][y][x][y+1] = 2,p[x][y+1][x][y] = 1;  
        else p[x][y][x][y+1] = 1,p[x][y+1][x][y] = 2;  
        ++y;
    }  
}  
  
bool dfs(int x,int y)
{
	if (x == 10) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j < 9; j++) printf("%d ",a[i][j]);
			printf("%d",a[i][9]);
			if (i < 9) cout << endl;
		}
		return 1;
	}
	int nx = x,ny = y + 1;
	if (ny == 10) nx = x + 1,ny = 1;  
	int xl = (x-1)/3; xl = xl*3 + 1;
	int xr = (x-1)/3; xr = xr*3 + 3;
	int yl = (y-1)/3; yl = yl*3 + 1;
	int yr = (y-1)/3; yr = yr*3 + 3;
	for (int i = L[x][y]; i <= R[x][y]; i++)
		if (!bo[x][y][i]) {
			if (p[x][y][x-1][y] == 1 && i < a[x-1][y]) continue;
			if (p[x][y][x-1][y] == 2 && i > a[x-1][y]) continue;
			if (p[x][y][x][y-1] == 1 && i < a[x][y-1]) continue;
			if (p[x][y][x][y-1] == 2 && i > a[x][y-1]) continue;
			for (int j = 1; j <= 9; j++) ++bo[x][j][i],++bo[j][y][i];
			for (int j = xl; j <= xr; j++)
				for (int l = yl; l <= yr; l++)
					++bo[j][l][i];
			a[x][y] = i;
			if (dfs(nx,ny)) return 1;
			for (int j = 1; j <= 9; j++) --bo[x][j][i],--bo[j][y][i];
			for (int j = xl; j <= xr; j++)
				for (int l = yl; l <= yr; l++)
					--bo[j][l][i];
		}
	return 0;
}
  
void PRE(int &i,int &x,int &y)
{
            if (i&1) {y = 1;for (int j = 0; j < 3; j++) MAKE(x,y),y += 3;}  
            else {  
                ++x; int t = 0;  
                for (y = 1; y <= 9; y++) {  
                    if (ch[t] == '^') p[x][y][x-1][y] = 1,p[x-1][y][x][y] = 2;  
                    else p[x][y][x-1][y] = 2,p[x-1][y][x][y] = 1;  
                    t += 2;  
                }  
            }  
	
}
  
char GET()
{
	char ret = getchar();
	for (;;) {
		if (ret == '>' || ret == '<' || ret == 'v' || ret == '^') return ret;
		ret = getchar();
	}
}  

int main()  
{  
    #ifdef YZY  
        freopen("yzy.txt","r",stdin);  
    #endif
      
    for (int l = 1; l <= 3; l++) {  
        int x,y; x = y = 1; x += (l-1)*3;  
        for (int i = 1; i <= 5; i++) {  
        	if (i & 1) {
        		ch[1] = GET(); ch[3] = GET();
        		ch[7] = GET(); ch[9] = GET();
        		ch[13] = GET(); ch[15] = GET();
        	}
        	else for (int j = 0; j <= 16; j += 2) ch[j] = GET();
            PRE(i,x,y);
        }  
    }  
    
	for (int i = 1; i <= 9; i++)
		for (int j = 1; j <= 9; j++) {
			L[i][j] = 1; R[i][j] = 9;
			for (int l = 0; l < 4; l++) {
				int xx,yy; 
				xx = i + dx[l];
				yy = i + dy[l];
				if (p[i][j][xx][yy] == 1) ++L[i][j];
				if (p[i][j][xx][yy] == 2) --R[i][j];
			}
		}
		
    dfs(1,1);  
    return 0;  
}  


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:109989次
    • 积分:7988
    • 等级:
    • 排名:第2565名
    • 原创:733篇
    • 转载:1篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论