跳到正文

科技·工程

扫雷3.6.1(原创)

Dylan_fan约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年7月18日
更新
2026年7月18日
篇幅
约 11 分钟

版本

  1. v1当前
C++
#define VERSION  "3.6.1"
#include<bits/stdc++.h>
#define endl "\n"
#define int long long
using namespace std;
const int N=25,mod=1E6;
int n,m,k,x,y,op; 
int mode,emd,cnt;
char s[N][N];
bool p[N][N];
int pl[N][N];
int d[8][2]={{+1,+0},{-1,+0},{+0,+1},{+0,-1},{+1,-1},{-1,+1},{-1,-1},{+1,+1}};
bool vis[N][N];
int input(){
	string str;
	cin>>str;
	int l=str.size();
	long long sum=0;
	for(int i=1;i<=l;i++){
		sum=sum*10+str[i-1]-'0';
	} 
	return sum;
}
bool password(){
	time_t curtime;
    time(&curtime);
    tm *nowtime=localtime(&curtime);
	int mon=nowtime->tm_mon+1;
	int day=nowtime->tm_mday;
	long long sum=700000+mon*1000+day*10+7;
	int T=sum;
	while(T--) sum=sum*sum%1000000;
	system("cls");
	cout<<"请输入密码:";
	int answer=input();
	if(answer==2147483647){
		cout<<"欢迎使用!"<<endl;
		system("pause");
		return 1;
	}
	if(sum==answer){
		cout<<"密码正确!"<<endl;
		system("pause");
		return 1;
	}
	cout<<"密码错误!"<<endl;
	system("pause");
	return 0;
}
void output(){
	system("cls");
	cout<<"       ";
	for(int i=1;i<=n;i++) printf("%2d   ",i); 
	cout<<endl<<"       ";
	for(int i=1;i<=5*n+1;i++) cout<<"_"; 
	cout<<endl;
	for(int i=1;i<=n;i++){
		printf("%2d    | ",i); 
		for(int j=1;j<=n;j++) cout<<s[i][j]<<"    "; 
		cout<<"|"<<endl<<"      |"; 
		for(int j=1;j<=5*n+1;j++) cout<<" "; 
		cout<<"|"<<endl; 
	}
	cout<<"      |"; 
	for(int i=1;i<=5*n+1;i++) cout<<"_"; 
	cout<<"|"<<endl; 
	return ;
}
void end_output(){
	system("cls");
	cout<<"       ";
	for(int i=1;i<=n;i++) printf("%2d   ",i); 
	cout<<endl<<"       ";
	for(int i=1;i<=5*n+1;i++) cout<<"_"; 
	cout<<endl;
	for(int i=1;i<=n;i++){
		printf("%2d    | ",i); 
		for(int j=1;j<=n;j++){
			if(p[i][j]==1) cout<<"X";
			else cout<<pl[i+1][j+1]-pl[i+1][j-2]-pl[i-2][j+1]+pl[i-2][j-2];
			cout<<"    "; 
		}
		cout<<"|"<<endl<<"      |"; 
		for(int j=1;j<=5*n+1;j++) cout<<" "; 
		cout<<"|"<<endl; 
	}
	cout<<"      |"; 
	for(int i=1;i<=5*n+1;i++) cout<<"_"; 
	cout<<"|"<<endl; 
	return ;
}
bool open(int x,int y){
	int ss=pl[x+1][y+1]-pl[x+1][y-2]-pl[x-2][y+1]+pl[x-2][y-2];
	if(p[x][y]==0) cnt++; 
	s[x][y]=ss+'0';
	if(ss==0) return 0; 
	else return 1; 
} 
void dfs(int x,int y){
	if(open(x,y)) return ; 
	s[x][y]=' ';
	for(int i=0;i<8;i++){
		int xx=x+d[i][0]; 
		int yy=y+d[i][1];
		if(s[xx][yy]=='.') dfs(xx,yy); 
	} 
	return ;
}
void input_nmk(){
	system("cls");
	cout<<"请输入地图大小(边长≤20):";
	n=input();
	cout<<"请输入地雷数量(数量≤格子数):";
	m=input();
	cout<<"请输入地图编号(编号≤10^6):";
	k=input();
	if(n<=0||n>20||m<0||m>n*n||k<0||k>1e8){
		cout<<"数据异常!请重新输入。"<<endl; 
		system("pause");
		input_nmk();
	} 
	return ;
}
void input_xy(){
	cout<<"当前共有"<<m<<"个雷"<<endl; 
	cout<<"请输入坐标:(标记该点先按0再输入坐标)"<<endl;
	op=input();
	if(op==0){
		x=input();
		y=input();
		if(x>n||x<1||y>n||y<1){
			cout<<"数据异常!请重新输入。"<<endl; 
			system("pause");
			output();
			input_xy();
		}
	}
	else{
		x=op,op=1;
		y=input();
		if(x>n||x<1||y>n||y<1){
			cout<<"数据异常!请重新输入。"<<endl; 
			system("pause");
			output();
			input_xy();
		}
	}
	return ;
} 
void input_emd(){
	system("cls");
	cout<<"请选择难度:"<<endl;
	cout<<"1.新手	2.简单	3.中等	4.困难"<<endl;
	emd=input();
	while(emd<1||emd>4){
		cout<<"数据异常!请重新输入。"<<endl; 
		cout<<emd<<endl;
		system("pause");
		input_emd(); 
	}
	return ;
}
void input_gamemode(){
	system("cls");
	cout<<"请选择模式:"<<endl;
	cout<<"1.常规模式	2.自定义模式"<<endl;
	mode=input();
	while(mode<1||mode>2){
		cout<<"数据异常!请重新输入。"<<endl; 
		cout<<mode<<endl;
		system("pause");
		input_gamemode();
	}
	return ;
}
void judge_emd(){
	if(emd==1) n=5,m=5,k=rand()%mod;
	if(emd==2) n=10,m=15,k=rand()%mod;
	if(emd==3) n=15,m=30,k=rand()%mod;
	if(emd==4) n=20,m=50,k=rand()%mod;
	return ;
}
void initial(){
	system("cls");
	system("color FD");
	cout<<"名称:扫雷"<<endl<<endl;
	cout<<"版本:"<<VERSION<<endl<<endl;
	cout<<"作者:小冬瓜"<<endl<<endl;
	cout<<"作者洛谷:wangzifan1227"<<endl<<endl;
	cout<<"最终解释权归作者所有"<<endl<<endl;
	cout<<"侵权必究"<<endl<<endl;
	cout<<"困难模式挑战成功获得秘钥"<<endl<<endl;
	cout<<"一共有三次踩雷机会"<<endl<<endl;
	cout<<"发现bug请联系作者(洛谷私信)必有重谢"<<endl<<endl;
	system("pause");
	return ;
}
void initial1(){
	input_gamemode();
	if(mode==1){
		srand(time(0));
		input_emd();
		judge_emd();
	}
	if(mode==2) input_nmk();
	return ;
}
void initial2(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			s[i][j]='.';
		}
	}
	for(int i=1;i<=k;i++)
		int x=rand();
	for(int i=1;i<=m;i++){
		int x=rand()%n; 
		if(x==0) x=n; 
		int y=rand()%n; 
		if(y==0) y=n;
		if(p[x][y]==0) p[x][y]=1; 
		else i--; 
	}
	for(int i=1;i<=n+1;i++){
		for(int j=1;j<=n+1;j++){
			pl[i][j]=pl[i-1][j]+pl[i][j-1]-pl[i-1][j-1]+p[i][j];
		}
	}
	return ;
}
void surprise(){
	system("cls");
	system("color F3");
	cout<<"秘钥是:"<<endl; 
	cout<<"ZCJ"<<endl; 
	system("pause");
	return ;
}
void fail(){
	system("cls");
	system("color FD");
	end_output();
	cout<<endl<<endl<<"又踩到雷了!你失败了!再试一次吧!"<<endl; 
	system("pause");
	return ;
}
void win(){
	system("cls");
	system("color FD");
	end_output();
	cout<<endl<<endl<<"你成功了!"<<endl; 
	system("pause");
	if(emd==4&&mode==1) surprise();	
	return ;
}
signed main(){
	while(!password());
	int life=3;
	initial();
	initial1();
	initial2();
	system("cls");
	system("color FA");
	output();
	while(true){
		cout<<endl<<endl;
		input_xy();
		if(p[x][y]==1&&op==1&&vis[x][y]==0){
			system("cls");
			if(life){
				cout<<"真不幸,踩到雷了!你还有"<<life--<<"次机会!再接再厉!"<<endl; 
				s[x][y]='X';
				vis[x][y]=1;
				system("pause");
				output();
			}
			else{
				s[x][y]='X';
				vis[x][y]=1;
				fail();
				return 0; 
			}
		}
		else if(op==0){
			if(s[x][y]=='.') s[x][y]='X';
			else{
				cout<<"此操作无效。"<<endl; 
				system("pause");
			}
		}
		else{
			if(s[x][y]=='.'||s[x][y]=='X'&&vis[x][y]==0) dfs(x,y);
			else{
				cout<<"此操作无效。"<<endl; 
				system("pause");
			}
		}
		output();
		if(cnt+m==n*n){
			win();
			break; 
		}
	}
	return 0;
}
原文