开灯游戏c++(自创)······求赞
2026-10-11 15:36:05
发布于:浙江
开灯游戏
代码如下:
#include <bits/stdc++.h>
using namespace std;
int a[1010][1010];
int main (){
int n;
for(int i = 1; i <= 40; i++){
cout << " ";
}
cout << "开灯游戏:按下按钮后它的上下左右与它自己取反。";
cout << endl;
for(int i = 1; i <= 40; i++){
cout << " ";
}
cout << "请输入开灯游戏的阶数:";
cin >> n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
a[i][j] = 0;
}
}
int flag = 1;
while(flag){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= 40; j++){
cout << " ";
}
for(int j = 1; j <= n; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
for(int i = 1; i <= 40; i++){
cout << " ";
}
int x, y;
cout << "请输入按下的行和列:";
cin >> x >> y;
system("cls");
a[x][y] = !a[x][y];
a[x][y - 1] = !a[x][y - 1];
a[x][y + 1] = !a[x][y + 1];
a[x - 1][y] = !a[x - 1][y];
a[x + 1][y] = !a[x + 1][y];
int f = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(a[i][j] != 1){
f = 0;
}
}
}
if(f){
flag = 0;
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= 40; j++){
cout << " ";
}
for(int j = 1; j <= n; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
for(int j = 1; j <= 40; j++){
cout << " ";
}
cout << "恭喜你通过了" << n << "阶开灯问题";
return 0;
}
这里空空如也
















有帮助,赞一个