Mine Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Every one once played the game called Mine Sweeping, here I change the rule. You are given an n*m map, every element is a '*' representing a mine, or a '.' representing any other thing. If I ask you what's the total number of mines around (i, j), you should check (i, j)'s up, down, left, right and also itself (if overstep the boundary, ignore it), if that position is a '*', you should add one to the total number of (i, j), and here I call the number Mine Number. For example, if the map is "..**.. ", we can get the Mine Number as "012210" easily, but here is the question, if I give you the Mine Number, can you tell me the original map?
输入
The input consists of multiple test cases.
The first line contains a number T, representing the number of test cases.
Then T lines follow. For each case, the first line contains two numbers n and m (1<=n, m<=20).representing the lines and rows. Then following n lines, each line contain m numbers each number represents the Mine Number.
输出
For each case, please print the case number (beginning with 1) and the original map which you reverted. The data guarantee there is only one result.
示例输入
2
7 11
10010100101
21223221222
32354532323
32355532323
31235321333
21022201333
10001000111
5 6
001110
013431
014541
013431
001110
示例输出
Case 1:
...........
*..*.*..*.*
*.*****.*.*
*.*****.*.*
*..***..*.*
*...*...***
...........
Case 2:
......
..***.
..***.
..***.
......
提示
来源
饭稀
...........
*..*.*..*.*
*.*****.*.*
*.*****.*.*
*..***..*.*
*...*...***
...........
. . . . . . . . . . .
* . . * . * . . * . *
* . * * * * * . * . *
* . * * * * * . * . *
* . . * * * . . * . *
* . . . * . . . * * *
. . . . . . . . . . .
懒得吐槽了,讲一下这道题吧,就是类似扫雷的规则,相信大家都比较熟悉扫雷,只是题中矩阵的数字表示上下左右和中(就是数字所在位置)五个位置中雷的个数。给出要求输入数字矩阵输出唯一确定的符合数字矩阵的图形矩阵'*'表示雷,'.'表示安全。
看似题目要求从数字矩阵推出图形矩阵,我们亦可以认为是要收缩符合已知数字矩阵的图形矩阵,矩阵的每个点只有两种状态(. or *),用搜索实现起来便不是很难。
注意:输入数字数组时要用字符形式存入字符数组中。
货不多说,下面看代码
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char mapp[][];
char num[][];
int n,m,dx[]={,-,,},
dy[]={,,,-};
bool ispos;
bool isout(int x,int y)//判断是否出界
{
if( x<||x>=m)
return ;
if(y<||y>=n)
return ;
return ;
}
void print(void)//输出字符数组
{
int i,j;
for(i=; i<m; ++i)
{
for(j=; j<n; ++j)
printf("%c ",mapp[i][j]);
printf("\n");
}
}
/*void printn(void)
{
int i,j;
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
printf("%c ",num[i][j]);
printf("\n");
}
}*/
int prints()//判断最后一行是否全为零
{
int j;
for(j=; j<n; ++j)
if(num[m-][j]!='')
return ;
return ;
}
void dfs(int x,int y)
{
// cout<<x<<' '<<y<<endl;
// cout<<"--------------"<<endl;
// printn();
// print();
if(ispos)
return;
if(x==m&&y==)
{
if(prints())
{
ispos=true;
print();
}
return;
}
int xx,yy,k,falg=;
for(k=;k<=;++k)//判断四周是否有0,
{
xx=x+dx[k];
yy=y+dy[k];
if( isout(xx,yy) && num[xx][yy]=='' )
{
falg=;//标记周围有0,不能不为'*'
break;
}
} if(falg)//没有标记,则可以放地雷
{
if(isout(x-,y)&&num[x-][y]!='')//若上方不唯1,则前面填错,需回溯
return;
mapp[x][y]='*';
for(k=;k<=;++k)//若放地雷,周围标记地雷数-1
{
xx=x+dx[k];
yy=y+dy[k];
if( isout(xx,yy))
num[xx][yy]--;
}
if(y==n-)
dfs(x+,);
else
dfs(x,y+);
for(k=;k<=;++k)//周围标记地雷数+1恢复
{
xx=x+dx[k];
yy=y+dy[k];
if(isout(xx,yy))
num[xx][yy]++;
}
}
mapp[x][y]='.';
if(isout(x-,y)&&num[x-][y]!='')//若上方不唯0,则前面填错,需回溯
return;
if(y==n-&&!ispos)
dfs(x+,);
else if(!ispos)
dfs(x,y+);
}
int main()
{
int i,test,t_num;
cin>>test;
for(t_num=;t_num<=test;++t_num)
{
cin>>m>>n;
for(i=;i<m;++i)
scanf("%s",num[i]);
printf("Case %d:\n",t_num);
ispos=;
dfs(,);
}
return ;
}