一个史山扫雷
2025-10-31 20:25:13
发布于:广东
感谢@SiO2 提供的源代码()
输入方式举例: 5 5 1
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int a[25][25];
int woniu[25][25];
int b[4][2] = {1,0,0,1,-1,0,0,-1};
int n;
void color(int x){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
}
bool v = true;
void ct(){
system("cls");
for(int i = 0;i <= (n + 1);i++){
for(int j = 0;j <= (n + 1);j++){
if(i == 0 && j == 0) cout << " ";
else if(i == 0 && j <= (n + 1) / 2) cout << " " << j - 1;
else if(i == 0 && j <= (n + 1)) cout << j - 1;
else if(j == 0 && i <= (n + 1) / 2) cout << i - 1 << " ";
else if(j == 0 && i <= (n + 1)) cout << i - 1 << " ";
else if(i == 1 || j == 1) cout << " ";
else if(woniu[i - 1][j - 1] == 1) {
if(a[i - 1][j - 1] <= 2) color(7);
else if(a[i - 1][j - 1] == 3) color(14);
else if(a[i - 1][j - 1] == 4) color(6);
else if(a[i - 1][j - 1] < 8) color(12);
else if(a[i - 1][j - 1] == 8) color(8);
cout << a[i - 1][j - 1] << " ";
color(7);
}
else if(woniu[i - 1][j - 1] == 2){
color(11);
cout << "雷";
color(7);
}else if(woniu[i - 1][j - 1] == 3){
color(4);
cout << 9 << " ";
color(7);
}else if(woniu[i - 1][j - 1] == 4){
color(64);
cout << 9 << " ";
color(7);
}else {
color(255);
cout << "? ";
color(7);
}
cout << " ";
}
color(0);
cout << endl << endl;
color(7);
}
color(7);
}
bool win(){
int sum = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(woniu[i][j] == 1 || (woniu[i][j] == 2 && a[i][j] == 9)) sum++;
}
}
if(sum == n * n) return true;
return false;
}
struct node{
int x;
int y;
int ni;
};
int main(){
cout << "输入格式:行 列 操作方法(0表示点开,1表示标记或者取消标记)\n";
cout << "请选择地图大小(9-19): ";
cin >> n;
srand(time(0));
int all = (n + 2) * (n + 2),now = 0;
memset(woniu,0,sizeof(woniu));
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(rand()%31 <= abs(10 - i) || rand()%31 <= abs(10 - j)){
a[i][j] = 9;
all--;
}
}
}
for(int i = (n + 1) / 2 - 1;i <= (n + 1) / 2 + 1;i++){
for(int j = (n + 1) / 2 - 1;j <= (n + 1) / 2 + 1;j++){
a[i][j] = 0;
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(a[i][j] != 9){
if(a[i - 1][j] == 9) a[i][j]++;
if(a[i + 1][j] == 9) a[i][j]++;
if(a[i][j - 1] == 9) a[i][j]++;
if(a[i][j + 1] == 9) a[i][j]++;
if(a[i - 1][j - 1] == 9) a[i][j]++;
if(a[i - 1][j + 1] == 9) a[i][j]++;
if(a[i + 1][j - 1] == 9) a[i][j]++;
if(a[i + 1][j + 1] == 9) a[i][j]++;
}
}
}
while(v){
ct();
int x,y,z;
cin >> x >> y >> z;
if(z == 0){
if(a[x][y] == 9){//踩雷了
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
woniu[i][j] = 1;
if(a[i][j] == 9) woniu[i][j] = 3;
}
}
woniu[x][y] = 4;
ct();
break;
}
queue<node> q;
q.push({x,y,0});
woniu[x][y] = 1;
while(!q.empty()){
node r = q.front();
q.pop();
for(int i = 0;i < 4;i++){
int nx = r.x + b[i][0];
int ny = r.y + b[i][1];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && (a[r.x][r.y] == 0 || r.ni <= 1) && a[nx][ny] != 9 && woniu[nx][ny] == 0){
now++;
int l = r.ni;
if(a[nx][ny] != 0 && a[nx][ny] != 9) l++;
q.push({nx,ny,l});
woniu[nx][ny] = 1;
}
}
}
}
else if(z == 1){
if(woniu[x][y] == 0){
woniu[x][y] = 2;
if(a[x][y] == 9) now++;
}else{
woniu[x][y] = 0;
if(a[x][y] == 9) now--;
}
}
if(win()){
system("cls");
color(14);
cout << "You Win!" << endl << endl;
color(7);
break;
}
}
return 0;
}
只有windows能用
全部评论 1
现在对不太齐,还在研究(我看不懂他的史山)
昨天 来自 广东
0










有帮助,赞一个