poj 2918 Tudoku 数独dfs
来源:互联网 发布:plc编程多久能入门 编辑:程序博客网 时间:2024/06/15 18:35
题意:
解数独游戏。
分析:
这道数独的数独直接dfs就能过。
代码:
//poj 2918//sep9#include<iostream>using namespace std;char s[12][12];int board[12][12];int CheckSquare[12][12];int CheckRow[12][12];int CheckColumn[12][12];int ok;int GetSquareId(int i,int j){int r=i/3;int c=j/3;return r*3+c;}int check(){int i,j;for(i=0;i<9;++i)for(j=0;j<9;++j){if(CheckRow[i][j]==0||CheckColumn[i][j]==0||CheckSquare[i][j]==0)return 0;}ok=1;return 1;}void dfs(int i,int j){if(ok==1)return;if(board[i][j]!=0){if(j<8)dfs(i,j+1);else{if(i<8)dfs(i+1,0);elsecheck();}}else{int x,ids=GetSquareId(i,j);for(x=1;x<=9;++x){if(CheckRow[i][x]==0&&CheckColumn[j][x]==0&&CheckSquare[ids][x]==0){board[i][j]=x;CheckRow[i][x]=1;CheckColumn[j][x]=1;CheckSquare[ids][x]=1;if(j<8)dfs(i,j+1);else{if(i<8)dfs(i+1,0);elsecheck();}if(ok==1)return ;CheckRow[i][x]=0;CheckColumn[j][x]=0;CheckSquare[ids][x]=0;}}board[i][j]=0;}}int main(){int cs,t=0;scanf("%d",&cs);while(cs--){int i,j;memset(CheckColumn,0,sizeof(CheckColumn));memset(CheckRow,0,sizeof(CheckRow));memset(CheckSquare,0,sizeof(CheckSquare));for(i=0;i<9;++i)scanf("%s",s[i]);for(i=0;i<9;++i)for(j=0;j<9;++j){board[i][j]=s[i][j]-'0';int x=board[i][j];CheckRow[i][x]=1;CheckColumn[j][x]=1;int ids=GetSquareId(i,j);CheckSquare[ids][x]=1;}ok=0;dfs(0,0);printf("Scenario #%d:\n",++t);for(i=0;i<9;++i){for(j=0;j<9;++j)printf("%d",board[i][j]);printf("\n");}printf("\n");}return 0;}
0 0
- poj 2918 Tudoku 数独dfs
- POJ 2918 Tudoku 【DFS】
- POJ 2676 && POJ 2918 数独 DFS
- HOJ 2306 Tudoku --数独
- POJ 2676/2918 数独(dfs)
- poj 2676 数独 ---dfs
- poj 2918 Tudoku
- poj 2918Tudoku(暴力。。)
- poj 2918 Tudoku
- POJ 2918 Tudoku [搜索]
- POJ 2918 Tudoku
- POJ 2918 Tudoku 笔记
- POJ 2676/2918——数独DFS
- poj 2676 dfs/DLX(数独)
- POJ 2676 Sudoku (数独 DFS)
- POJ--2676&HDU--1421(数独,dfs)
- POJ 3074&&2676 数独DFS
- [DFS]poj 2676 Sudoku 数独问题
- 游戏开发术语与常识
- java.util.concurrent介绍
- sgu205:Quantization Problem(dp)
- poj 3624 Charm Bracelet
- 北京工地洗轮机质量怎么样
- poj 2918 Tudoku 数独dfs
- hdu5000(dp)
- 数的长度
- LeetCode Longest Substring Without Repeating Characters
- Java学习笔记32
- 服务端开发问题汇集
- 【Python】Python数据结构
- 程序员应该了解的10种算法
- session.merge ()方法