Kill the monster (dfs) problem solution

It's not too difficult if you think about it, but-the
problem is I didn't expect


topic

Title Description
In order to celebrate his birthday, Xiao Zhang launched a game. The game is played on a 20×20 square with some monsters on it, indicated by "#", and the others are spaces, indicated by ".". The monster has two physical strengths. He died when his stamina reached zero.
You can perform the following operations:
(1) Decrease the physical strength of a monster on a horizontal line by one
(2) Decrease the physical strength of a monster on a vertical line by one
pair. Each horizontal or vertical line can only be operated once, limited to n times, and asked at most. How many monsters to kill.

Input format: The
first line is an integer n (1≤n≤40), indicating the number of operations.
Next is a 20×20 square, "#" means monster, and "." means space.
Output format:
one line, output the maximum number of monsters that can be killed.

Sample input and output

输入样例一:                          
3         
.................... 
.................... 
..###............... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
.................... 
....................

输出样例一:
2
输入样例二:
10 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 
#################### 

输出样例二:
25

Ideas:

The monster has two physical strengths. Death when the stamina is 0 && can only be operated once for each horizontal or vertical row

So, you want to kill a monster, you must pass a row and one
that we will be able to enumerate the line
every time they complete enumeration of the sort, delete and see which column can kill up to a monster

Enumerate rows --> Sort --> Delete columns --> Accumulate --> Compare

Upload code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

int a[25],v,flag[25],k,maxx,ans;
char z,s[25][25];

bool cmp(int a,int b)
return a>b;

void dfs(int c,int d)//c从哪一行开始继续删(避免重复)d是删了多少次
{
    
    
   if(d==k)
   return;
   int v=min(k-d,20),f[25],maxx=0;//v是可以删除多少列
   for(int i=1;i<=20;i++)
   f[i]=a[i];
   sort(f+1,f+1+20,cmp);//排序
   for(int i=1;i<=v;i++)
   maxx=maxx+f[i];//累加与删除(删除不用表示出来,但要知道意思)
   if(maxx>=ans)
   ans=maxx;//判断
   for(int x=c;x<=20;x++)
   {
    
    
   	for(int i=1;i<=20;i++)  if(s[x][i]=='#') a[i]++;
   	dfs(x+1,d+1);
   	for(int i=1;i<=20;i++)  if(s[x][i]=='#') a[i]--;
   }//枚举
}

int main()
{
    
    
	cin>>k;
	for(int i=1;i<=20;i++)
	{
    
    
		for(int j=1;j<=20;j++)
		{
    
    
			cin>>s[i][j]; 
		} 
	}
	ans=0;
    dfs(1,0);
    cout<<ans;
	return 0;
}

統结撒花✿✿ヽ(°▽°)ノ✿ Thank you for your company (as if you have said so much)

Guess you like

Origin blog.csdn.net/yyh0910/article/details/115308188