1560D - Make a Power of Two

链接:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值