链接:
https://codeforces.com/problemset/problem/1560/D
题意:
两个操作:
1、删除任意一位
2、在最右端加一个数字
给定n,求让n操作成2的k次方(k>=0)的最小操作数
输入
12
1052
8888
6
75
128
1
301
12048
1504
6656
1000000000
687194767
输出量
2
3
1
3
0
0
2
1
3
4
9
2
解:
把2的k次方列出来,然后字符串对比,看看要做几次操作,找最小
一开始列到50次方不够,开了60AC了,试了试56也能过,绝了QWQ
实际代码:
#include<iostream>
#include<cstdio>
using namespace std;
int map[505][505];
int book[505][505];
bool ans=0;
int lu=0;
int nst;
int dfs(int n,int m ,int gz,int x,int y)
{
nst++;
book[x][y]=1;
//cout<<x<<" "<<y<<endl;
//cout<<nst<<endl;
if(nst==gz)
{
ans=1;
return 0;
}
if(x-1>=1&&book[x-1][y]==0&&map[x-1][y]==-1&&ans==0) dfs(n,m,gz,x-1,y);
//cout<<"done1"<<endl;
if(x+1<=n&&book[x+1][y]==0&&map[x+1][y]==-1&&ans==0) dfs(n,m,gz,x+1,y);
//cout<<"done2"<<endl;
if(y-1>=1&&book[x][y-1]==0&&map[x][y-1]==-1&&ans==0) dfs(n,m,gz,x,y-1);
//cout<<"done3"<<endl;
if(y+1<=m&&book[x][y+1]==0&&map[x][y+1]==-1&&ans==0) dfs(n,m,gz,x,y+1);
//cout<<"done4"<<endl;
}
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char a;
cin>>a;
if(a=='#') map[i][j]=0;
if(a=='.')
{
map[i][j]=-1;
lu++;
}
}
}
for(int i=1;i<=n;i++)
{
if(ans==1) break;
for(int j=1;j<=m;j++)
{
if(ans==1) break;
if(map[i][j]==-1)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
book[i][j]=0;
}
}
nst=0;ans=0;
dfs(n,m,lu-k,i,j);
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(map[i][j]==0)
{
cout<<"#";
}
else if(book[i][j]==1) cout<<'.';
else cout<<"X";
}
cout<<endl;
}
}
限制:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output