C++#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
bool h = false;
int n[4][4];
bool pan1[4][4];
bool pan2[4][4];
bool pan3[4][4];
int nn[4][4];
bool pan4[4][4];
void dfs(int x, int y){
if(x == 4){
h = true;
return;
}
if(h){
return;
}
int xy = x / 2 * 2 + y / 2;
if(n[x][y] == 0){
for(int i = 0; i < 4; i++){
if(!pan1[x][i] && !pan2[y][i] && !pan3[xy][i]){
pan1[x][i] = pan2[y][i] = pan3[xy][i] = true;
n[x][y] = i + 1;
if(y == 3){
dfs(x + 1, 0);
}else{
dfs(x, y + 1);
}
if(h){
return;
}
pan1[x][i] = pan2[y][i] = pan3[xy][i] = false;
n[x][y] = 0;
}
}
}else{
if(y == 3){
dfs(x + 1, 0);
}else{
dfs(x, y + 1);
}
}
}
void input(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cin >> n[i][j];
nn[i][j] = n[i][j];
}
}
return;
}
void memn(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
n[i][j] = nn[i][j];
}
}
return;
}
void pann1(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
int b = n[i][j];
if(b == 0){
continue;
}else{
pan1[i][b - 1] = true;
pan2[j][b - 1] = true;
pan3[i / 2 * 2 + j / 2][b - 1] = true;
pan4[i][j] = true;
}
}
}
return;
}
void output(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cout << n[i][j] <<" ";
}
cout << endl;
}
return;
}
bool ying(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(n[i][j] == 0){
return false;
}
}
}
return true;
}
bool shu(){
while(true){
cout << "请输入:";
int a, b, c;
cin >> a;
if(a == 0){
cout << "参考答案:" << endl;
dfs(0, 0);
output();
cout << "下次努力哦!" << endl;
Sleep(5000);
system("cls");
break;
}
cin >> b >> c;
if(a > 4 || b > 4 || c > 4){
cout << "您的输入不符合规则,输入无效。" << endl;
Sleep(500);
system("cls") ;
output();
continue;
}
a -= 1;
b -= 1;
c -= 1;
int ab = a / 2 * 2 + b / 2;
if(n[a][b] != 0){
cout << "这个格子已经有数了哟~~~" << endl;
cout << "您是要修改这个格子吗?(1:是 2:不是)" << endl;
int g;
cin >> g;
if(g == 1){
if(!pan4[a][b]){
int p = n[a][b] - 1;
pan1[a][p] = pan2[b][p] = pan3[ab][p] = false;
n[a][b] = 0;
cout << "修改成功!" << endl;
}else{
cout << "这个格子您无法修改" << endl;
Sleep(500);
system("cls");
output();
continue;
}
}else{
Sleep(500);
system("cls");
output();
continue;
}
}
if(!pan1[a][c] && !pan2[b][c] && !pan3[ab][c]){
n[a][b] = c + 1;
if(ying()){
cout << "恭喜您 挑战成功!!!" << endl;
Sleep(5000);
system("cls");
break;
}
pan1[a][c] = true;
pan2[b][c] = true;
pan3[ab][c] = true;
//cout << "继续!" << endl;
Sleep(500);
system("cls");
output();
continue;
}else{
n[a][b] = c + 1;
Sleep(500);
system("cls");
output();
cout << "您已挑战失败" << endl;
n[a][b] = 0;
cout << "是否满血复活?(1:是 2:不是)" << endl;
int d;
cin >> d;
if(d == 1) {
cout << "满血复活!" << endl;
Sleep(500);
system("cls");
output();
continue;
}else{
cout << "Game over" << endl;
Sleep(5000);
system("cls");
break;
}
}
}
cout << "是否再来一局?(1:是 2:不是)" << endl;
int e;
cin >> e;
if(e == 1){
cout << "重新开始还是新的一局?(1:重新开始 2:新的一局)" << endl;
int f;
cin >> f;
if(f = 1){
memn();
Sleep(500);
system("cls");
pann1();
return true;
}else{
Sleep(500);
system("cls");
input();
pann1();
return true;
}
}else{
cout << "欢迎下次再来!!!" << endl;
cout << " 游戏结束" << endl;
system("cls");
return false;
}
}
int main( ){
cout << "数独游戏 4宫格 现在开始!" << endl;
cout << "规则:每次输入三个数,表示在第几行第几列的位置填入数字几。" << endl;
cout << " 玩的愉快!(10秒后开始游戏)" << endl;
Sleep(10000);
system("cls");
input();
pann1();
system("cls");
while(true){
Sleep(300);
cout << "加载中▁" << endl;
Sleep(300) ;
system("cls");
cout << "加载中▁▂" << endl;
Sleep(300);
system("cls");
cout << "加载中▁▂▃" << endl;
Sleep(300);
system("cls");
cout << "加载中▁▂▃▅" << endl;
Sleep(300);
system("cls");
cout << "加载中▁▂▃▅▆" << endl;
Sleep(300);
system("cls");
cout << "加载中▁▂▃▅▆▇" << endl;
Sleep(300);
system("cls");
cout << "加载中▁▂▃▅▆▇▉" << endl;
Sleep(300);
system("cls");
output();
cout << "游戏开始!" << endl;
if(!shu()){
break;
}
}
return 0;
}
/*
0 2 0 4
3 0 2 0
0 0 4 3
0 0 1 0
*/