#include <bits/stdc++.h>
using namespace std;
int main() {
	int a[11][11]={0};
	srand(time(0));
	int i=0;
	while(i<10) {
		int x = rand()%9+1;
		int y = rand()%9+1;
		if(a[x][y] == 0) {
			a[x][y] = -1;
			i++;
		}
	}
	for(int i=1;i<=9;i++) {
		for(int j=1;j<=9;j++) {
			if(a[i][j]!=(-1)) {
				for(int x=i-1;x<=i+1;x++) {
					for(int y=j-1;y<=j+1;y++) {
						if(a[x][y]==(-1)) {
							a[i][j]++;
						}
					}
				}
			}
		}
	}
	for(int i=1;i<=9;i++) {
		for(int j=1;j<=9;j++) {
			cout<<setw(2)<<'*'<<" ";
		}
		cout<<endl;
	}
	int x,y;
	bool flag[11][11]={0};
	do {
		cout<<"---------------------------------------------"<<endl;
		cout<<"请输入两个数字,代表要点开的位置(第几行第几列)"<<endl;
		cin>>x>>y;
		flag[x][y]=1;
		for(int i=1; i<=9;i++) {
			for(int j=1;j<=9;j++) {
				if(flag[i][j]==1) {
					cout<<setw(2)<<a[i][j]<<" ";
				} else {
					cout<<setw(2)<<'*'<<" ";
				}
			}
			cout<<endl;
		}
	} while(a[x][y]!=(-1));
	return 0;
}

2 条评论

  • @ 2026-7-28 15:34:21
    #include <bits/stdc++.h>
    #include <cstdlib>
    #include <unistd.h>
    using namespace std;
    
    bool a[201][201]={};
    bool d[201][201]={};
    int b[201][201]={};
    int c[201][201]={};
    int n;
    void h(int x,int y){
    	if(x >= 1 && x <= n && y >= 1 && y <= n){
    		if(c[x][y] == 0){
    			d[x][y] = true;
    			if(d[x+1][y] != true){
    				h(x+1,y);	
    			}
    			if(d[x][y+1] != true){
    				h(x,y+1);
    			}
    			if(d[x][y-1] != true){
    				h(x,y-1);
    			}
    			if(d[x-1][y] != true){
    				h(x-1,y);
    			}
    			return ;
    		}
    		if(c[x][y] != -1 && c[x][y]!= -2){
    			d[x][y] = true;
    			return ;
    		}	
    	}else{
    		return ;	
    	}
    }
    
    int main(){
    	bool f =true;
    	//cout <<"■";
        srand(getpid());
        
        cout << "给一个10~100的数作为地图边长:\n" ;
    	cin >> n;
    	system("cls");
    	while(1){
    		if(n > 100){
    			cout << "太大了\n"; 
    		} else if(n < 10){
    			cout << "太小了\n"; 
    		}else{
    			break;
    		}
    		cin >> n; 
    	} 
        
        int cnt[1001]={};
        cout << 0 << "  ";
        for(int i = 1;i <= n;i++){
        	if(i <= 9){
        		cout << " "<< i;
    		}else if(i >= 10 && i <= 99){
    			cout << " " << i/10%10;
    		}else{
    			cout << " " << 1;
    		}
    	}
    	cout << endl;
    	cout << 0 << "  ";
        for(int i = 1;i <= n;i++){
        	if(i <= 9){
        		cout << "  ";
    		}else if(i >= 10 && i <= 99){
    			cout << " " << i%10;
    		}else{
    			cout << " " << 0;
    		}
    	}
    	cout << endl;
    	cout << 0 << "  ";
        for(int i = 1;i <= n;i++){
        	if(i <= 9){
        		cout << "  ";
    		}else if(i >= 10 && i <= 99){
    			cout << "  ";
    		}else{
    			cout << " " << 0;
    		}
    	}
    	cout << endl; 
    	for(int i = 1;i <= n;i++){
    		if(i >= 10 && i <= 99){
    			cout << i << " |";
    		}else if(i == 100){
    			cout << "100|";
    		}else{
    			cout<< i<<"  |"; 
    		}
    		for(int j = 1;j <= n;j++){
    			cout << "■";
    			a[i][j] = false;
    			d[i][j] = false;
    		}
    		cout <<endl;
    	}
    
    	for(int i = 0;i <= n+1;i++){
    		for(int j = 0;j <= n+1;j++){
    			if(i == 0 ||j ==0||j == n+1 || i == n+1){
    				c[i][j] = -2;
    			}
    		}
    	}
    	for(int i = 1;i <= n*n/10;i++){
            int x = rand();
            int y = rand();
    		x%= (n+1);
    		y %= (n+1);
    		if(x >= 0 && x <= n-1 && y >= 0 && y <= n-1){
    			a[x+1][y+1] = true;	
    		}else{
    			i--;
    		}
    	}
    	for(int i = 1;i <= n;i++){
    		for(int j = 1;j <= n;j++){
    			if(a[i][j] != true){
    				int num =0; 
    				if(a[i+1][j] == true){
    					num++;
    				}
    				if(a[i][j+1] == true){
    					num++;
    				}
    				if(a[i-1][j] == true){
    					num++;
    				}
    				if(a[i][j-1] == true){
    					num++;
    				}
    				if(a[i+1][j+1] == true){
    					num++;
    				}
    				if(a[i+1][j-1] == true){
    					num++;
    				}
    				if(a[i-1][j+1] == true){
    					num++;
    				}
    				if(a[i-1][j-1] == true){
    					num++;
    				}	
    				c[i][j] = num;			
    			}else{
    				c[i][j] = -1;
    			}
    		}		
    	}
    	while(f == true){
    		int num=0,ans=0;
    		for(int i = 1;i <= n;i++){
    			for(int j = 1;j <= n;j++){
    				if(a[i][j] != true && d[i][j] == true){
    					num++;
    				}else if(a[i][j] == true && d[i][j] != true){
    					ans++;
    				}
    			}
    		}
    		if(ans == n*n/10 && n*n- ans == num){
    			system("cls");
    			cout <<"你赢了【强】【强】【强】";
    			for(int i = 1;i <= 10;i++){
    				for(int j = 1;j <= 10;j++){
    					if(a[i][j] == true){
    						cout << "●";
    					}else{
    						cout << c[i][j]<<" ";
    					}
    				}
    				cout << endl;
    			} 
    			return 0; 
    		}
    		int x,y;
    		char ca;
    		cout << "分别输入行,列,操作(1挖开,2标记):";
    		cin >> x;
    		cin >> y;
    		cin >> ca;
    		system("cls");
    		if(ca== '2'){
    
    			system("cls");
    		    cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << " "<< i;
    				}else if(i >= 10 && i <= 99){
    					cout << " " << i/10%10;
    				}else{
    					cout << " " << 1;
    				}
    			}
    			cout << endl;
    			cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << "  ";
    				}else if(i >= 10 && i <= 99){
    					cout << " " << i%10;
    				}else{
    					cout << " " << 0;
    				}
    			}
    			cout << endl;
    			cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << "  ";
    				}else if(i >= 10 && i <= 99){
    					cout << "  ";
    				}else{
    					cout << " " << 0;
    				}
    			}
    			cout << endl; 
    			for(int i = 1;i <= n;i++){
    				if(i >= 10 && i <= 99){
    					cout << i << " |";
    				}else if(i == 100){
    					cout << "100|";
    				}else{
    					cout<< i<<"  |"; 
    				}
    				for(int j = 1;j <= n;j++){
    					if(i == x && y == j || b[i][j] == 1){
    						d[i][j] = true;
    						cout <<"★";
    						b[x][y] = 1;
    					}else if(d[i][j] == false){
    						cout << "■";	
    					}else{
    						cout << c[i][j]<<" ";
    					}
    					
    				}
    				cout <<endl;
    			}					
    		}
    		if(ca == '1'){
    			if(a[x][y] == true){
    				system("cls");
    				cout <<"你失败了【菜】【菜】【菜】\n";
    				for(int i = 1;i <= n;i++){
    					for(int j = 1;j <= n;j++){
    						if(a[i][j] == true){
    							cout << "●";
    						}else{
    							cout << c[i][j]<<" ";
    						}
    					}
    					cout << endl;
    				} 
    				return 0; 
    			}
    			h(x,y);
    			system("cls");
    		    cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << " "<< i;
    				}else if(i >= 10 && i <= 99){
    					cout << " " << i/10%10;
    				}else{
    					cout << " " << 1;
    				}
    			}
    			cout << endl;
    			cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << "  ";
    				}else if(i >= 10 && i <= 99){
    					cout << " " << i%10;
    				}else{
    					cout << " " << 0;
    				}
    			}
    			cout << endl;
    			cout << 0 << "  ";
    		    for(int i = 1;i <= n;i++){
    		    	if(i <= 9){
    		    		cout << "  ";
    				}else if(i >= 10 && i <= 99){
    					cout << "  ";
    				}else{
    					cout << " " << 0;
    				}
    			}
    			cout << endl; 
    			for(int i = 1;i <= n;i++){
    				if(i >= 10 && i <= 99){
    					cout << i << " |";
    				}else if(i == 100){
    					cout << "100|";
    				}else{
    					cout<< i<<"  |"; 
    				}
    				for(int j = 1;j <= n;j++){
    					if(d[i][j] == false){
    						cout << "■";
    					}else if(b[i][j] == 1){
    						cout <<"★";
    					}else{
    						cout << c[i][j]<<" ";
    					}
    				}
    				cout <<endl;
    			}	
    		}
    	}
    	return 0;
    }
    
    
    
    
    
    
    • 不多见啊,玄武记社区又来活人了

      • 1