• 2612Mine Sweeper


    Mine Sweeper
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions:7034 Accepted: 2718

    Description

    The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses. If a positon not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below. 
     
    Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the figures resembling asterisks represent mines. The leftmost image represents the partially played game. From the first image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines. 

    Your job is to read the information for a partially played game and to print the corresponding board. 

    Input

    The first line of input contains a single postitive integer n <= 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined positon while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an x, and untouched positions by a period. The sample input corresponds to the middle figure above.

    Output

    Your output should represent the board, with each position filled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period.

    Sample Input

    8
    ...**..*
    ......*.
    ....*...
    ........
    ........
    .....*..
    ...**.*.
    .....*..
    xxx.....
    xxxx....
    xxxx....
    xxxxx...
    xxxxx...
    xxxxx...
    xxx.....
    xxxxx...
    

    Sample Output

    001.....
    0013....
    0001....
    00011...
    00001...
    00123...
    001.....
    00123...
    #include<stdio.h>
    #include<string.h>
    #define Max 12
    int main(int argc, char const *argv[])
    {
    	char in[Max][Max];
    	char out[Max][Max];
    	char flag[Max][Max];
    	int ans[Max][Max];
    	int i,j;
    	int n;
    	int fg;
    	while(~scanf("%d",&n))
    	{
    		fg=0;
    		getchar();
    		for(i=1;i<=n;i++)
    			{
                    for(j=1;j<=n;j++)
    				scanf("%c",&in[i][j]);
    			    getchar();
    			}
    				for(i=1;i<=n;i++)
    					{
    						for(j=1;j<=n;j++)
    						scanf("%c",&flag[i][j]);
    					    getchar();
    					}
    			memset(ans,0,sizeof(ans));
    			for(i=1;i<=n;i++)
    				for(j=1;j<=n;j++)
    				{
    					if(in[i][j]=='*')
    						{
    							ans[i-1][j-1]++;
    							ans[i][j-1]++;
    							ans[i+1][j-1]++;
    							ans[i-1][j]++;
    							ans[i+1][j]++;
    							ans[i-1][j+1]++;
    							ans[i][j+1]++;
    							ans[i+1][j+1]++;
    						}
    				}
                memset(out,0,sizeof(out));
                for(i=1;i<=n;i++)
                	for(j=1;j<=n;j++)
                	{
                		if(flag[i][j]=='x')
                		{
                			if(in[i][j]=='*')fg=1;
                		    out[i][j]=ans[i][j]+'0';
                		}
                		else
                			out[i][j]='.';
                	}
                	if(fg)
                	{
                		for(i=1;i<=n;i++)
                			for(j=1;j<=n;j++)
                				if(in[i][j]=='*')
                					out[i][j]='*';
                	}
                	for(i=1;i<=n;i++)
                	{
                			for(j=1;j<=n;j++)
                		{
                			printf("%c",out[i][j]);
                		}
                		printf("
    ");
                	}
    	}
    	return 0;
    }
    

  • 相关阅读:
    iOS之蓝牙开发—CoreBluetooth详解
    iOS-GCD使用详解
    iOS—Mask属性的使用
    idea导入eclipse中的maven项目
    SQL Server 查找字符串中指定字符出现的次数
    lLinux的常用命令
    从excel表中生成批量SQL
    ORA-00911: invalid character 错误解决
    sqlserver sp_who2和inputbuffer的使用,连接数
    如果存在这个表,则删除这个表的sql
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363353.html
Copyright © 2020-2023  润新知