题目描述
no
样例
i:
3 3
??
???
??
o:
10
221
11
算法1
(暴力枚举) $O(n^2)$
本题分析:使用char型二维数组,然后循环嵌套判断:
if(j+1<=m-1&&a[i][j+1]==’‘&&a[i][j]==’?’){
t;
}
if(j-1>=0&&a[i][j-1]==’*’&&a[i][j]==’?’){
t;
}
if(i+1<=n-1&&a[i+1][j]==’‘&&a[i][j]==’?’){
t;
}
if(i-1>=0&&a[i-1][j]==’*’&&a[i][j]==’?’){
t;
}
if(j-1>=0&&i-1>=0&&a[i-1][j-1]==’‘&&a[i][j]==’?’){
t;
}
if(j+1<=m-1&&i+1<=n-1&&a[i+1][j+1]==’*’&&a[i][j]==’?’){
t;
}
if(i+1<=n-1&&j-1>=0&&a[i+1][j-1]==’‘&&a[i][j]==’?’){
t;
}
if(i-1>=0&&j+1<=m-1&&a[i-1][j+1]==’*’&&a[i][j]==’?’){
t;
}
有些人把这道题做麻烦了,其实没有这么麻烦,该?上的数其实不用转换成字符型,直接输出就好了。
::
if(a[i][j]==’?’){
cout<<t;
}else{
cout<<a[i][j];
}
时间复杂度分析:n*m
C++ 代码
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
int main(){
int n,m;
cin>>n>>m;
char a[n+1][m+1];
for(int i=0;i[HTML_REMOVED]>a[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int t=0;
if(j+1<=m-1&&a[i][j+1]=='*'&&a[i][j]=='?'){
t++;
}
if(j-1>=0&&a[i][j-1]=='*'&&a[i][j]=='?'){
t++;
}
if(i+1<=n-1&&a[i+1][j]=='*'&&a[i][j]=='?'){
t++;
}
if(i-1>=0&&a[i-1][j]=='*'&&a[i][j]=='?'){
t++;
}
if(j-1>=0&&i-1>=0&&a[i-1][j-1]=='*'&&a[i][j]=='?'){
t++;
}
if(j+1<=m-1&&i+1<=n-1&&a[i+1][j+1]=='*'&&a[i][j]=='?'){
t++;
}
if(i+1<=n-1&&j-1>=0&&a[i+1][j-1]=='*'&&a[i][j]=='?'){
t++;
}
if(i-1>=0&&j+1<=m-1&&a[i-1][j+1]=='*'&&a[i][j]=='?'){
t++;
}
if(a[i][j]=='?'){
cout<<t;
}else{
cout<<a[i][j];
}
}
cout<<endl;
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla