T2
就是一个棋盘。首先,个棋子能放在[,]~[,],也就是一共二十五个格子放个棋子选或不选的问题。每次一种方案再模拟能炸到多少敌人即可。
#include<bits/stdc++.h>
using namespace std;
int xx[5000],yy[5000];
int x[10000],y[10000],r[100000];
int maxx = 0;
int n,k,R;
int flag[20];
void dfs(int cur_x, int cur_y, int cnt)//当前填的格子的x,当前填的格子的y,已经填了几个格子了
{
if(cnt==k)//填完了
{
int hh = 0;//炸到的个数
for(int i = 1; i <= n ; i++)flag[i]=0;//清空
for(int i = 1; i <= k; i++)//模拟每个棋子能炸到的敌人
{
for(int j = 1; j <= n; j++)
{
double t= sqrt((xx[i]-x[j])*(xx[i]-x[j])+(yy[i]-y[j])*(yy[i]-y[j]));//计算距离
if(t -(R+r[j])<= 0 && flag[j]==0)//如果距离能炸到并且没有之前被炸死
{
flag[j]=1;//现在炸死了
hh++;
}
}
}
maxx=max(maxx,hh);//最大
return;
}
if(cur_x > 5) return;//如果已经第27个但是k个棋子还没放完
int ny = cur_y + 1;
int nx = cur_x;
if(ny > 5)
{
ny = -5;
nx = cur_x + 1;
}
xx[cnt+1] = cur_x;//记录
yy[cnt+1] = cur_y;
dfs(nx, ny, cnt+1);//放了
dfs(nx, ny, cnt);//没放
}
int main()
{
cin>>n>>k>>R;
for(int i = 1; i <= n; i++)
{
cin >> x[i]>>y[i]>>r[i];
}
dfs(-5,-5,0);//-5,-5开始
cout<<maxx<<endl;
return 0;
}T4
题面有一个点绕了一下弯,就是极大连通块就代表一个不能再扩散的一个联通快,然后求出这样所有联通块的大小为奇数。
明显,里再套一个判断联通块的,每次枚举一个格子放黑放白,找出一个方案后调用连通块判断是否和合法。
#include<bits/stdc++.h>
using namespace std;
int ans[100][100];
int ccnt = 0;
int cnt = 0;
int n,m,cs;
int vis[100][100];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};//向上下左右扩散
bool check(int x,int y)
{
return (x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0&&ans[x][y]==cs);//如果扩散的点合法
}
void lian(int x,int y)
{
for(int i = 0 ; i < 4; i++)//四个方向扩散
{
int xx = x + dx[i];
int yy = y + dy[i];
if(check(xx,yy))
{
cnt++;//数量+1
vis[xx][yy]=1;
lian(xx,yy);
}
}
return;
}
void dfs(int cur)
{
if(cur==m*n+1)//所有格子枚举完
{
memset(vis, 0, sizeof(vis));//清空
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(vis[i][j]==0)//要记录选没选过
{
cnt=1;
cs=ans[i][j];
vis[i][j]=1;
lian(i,j);
if(cnt%2==0)return;//因为本来没算自己,所以判断是否为偶数
}
}
}
ccnt++;
return;
}
int x = (cur-1)/m + 1;
int y = (cur-1)% m + 1;
ans[x][y]=1;//递推
dfs(cur+1);
ans[x][y]=0;
dfs(cur+1);
}
int main()
{
cin>>n>>m;
dfs(1);
cout<<ccnt;
return 0;
}T5
虽然看着是选或不选的一道基础题,仅需个格子填十五个数,填完后判断是否满足条件即可。
无剪枝:
#include<bits/stdc++.h>
using namespace std;
int a[5][5],vis[17];
int n,m;
bool check()//判断每一种条件
{
if(a[1][1] + a[1][4] + a[4][1] + a[4][4] != 34)//四个角落上的数字
return 0;
if( a[1][1] + a[1][2] + a[2][1] + a[2][2] != 34)//每个角落上的 2×2 方格中的数字
return 0;
if(a[2][2] && a[2][3] && a[3][2] && a[3][3] && a[2][2] + a[2][3] + a[3][2] +
a[3][3] != 34)//最中间的 2×2 方格中的数字
return 0;
for(int i = 1; i <= 4; i ++)
{
if(a[1][i] + a[2][i] + a[3][i] + a[4][i] != 34)//每条竖线上四个格子中的数字
return 0;
if( a[i][1] + a[i][2] + a[i][3] + a[i][4] != 34)//每条横线上四个格子中的数字
return 0;
}
if(a[1][1] + a[2][2] + a[3][3] + a[4][4] != 34)//主对角线
return 0;
if(a[1][4] + a[2][3] + a[3][2] + a[4][1] != 34)//副对角线
return 0;
return 1;
}
void dfs(int cur)
{
if(cur==17)
{
if(check())//合法
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1;j<=4;++j)
{
cout<<a[i][j];
cout<<" ";
}
cout<<endl;
}
}
cout<<endl;
return;
}
int x = (cur-1)/4 + 1;
int y = (cur-1)% 4 + 1;
if(x==n&&y==m)//如果是1点
{
a[x][y]=1;
dfs(cur+1);//跳过
return;
}
for(int i = 2; i <= 16; i++)//1~15
{
if(vis[i]==0)//没选过
{
vis[i]=1;
a[x][y]=i;
dfs(cur + 1);
vis[i]=0;
a[x][y]=0;//回溯
}
}
}
int main()
{
cin>>n>>m;
a[n][m]=1;//标记点一
vis[1]=1;//记录1
dfs(1);
return 0;
}可测出,,全部。肯定需要剪枝,思路也很简单,每次选出数可以先判断如果连选了这个数已经不可以了,就直接换掉,这样可以减掉很多方案,不过,判断条件的时候要判断这个条件的四个数有没有填完。
#include<bits/stdc++.h>
using namespace std;
int a[5][5],vis[17];
int n,m;
bool check()//额外判断每次四个点是否填了
{
if(a[1][1] && a[1][4] && a[4][1] && a[4][4] && a[1][1] + a[1][4] + a[4][1] +
a[4][4] != 34)
return 0;
if(a[1][1] && a[1][2] && a[2][1] && a[2][2] && a[1][1] + a[1][2] + a[2][1] +
a[2][2] != 34)
return 0;
if(a[2][2] && a[2][3] && a[3][2] && a[3][3] && a[2][2] + a[2][3] + a[3][2] +
a[3][3] != 34)
return 0;
for(int i = 1; i <= 4; i ++)
{
if(a[1][i] && a[2][i] && a[3][i] && a[4][i] && a[1][i] + a[2][i] + a[3]
[i] + a[4][i] != 34)
return 0;
if(a[i][1] && a[i][2] && a[i][3] && a[i][4] && a[i][1] + a[i][2] + a[i]
[3] + a[i][4] != 34)
return 0;
}
if(a[1][1] && a[2][2] && a[3][3] && a[4][4] && a[1][1] + a[2][2] + a[3][3] +
a[4][4] != 34)
return 0;
if(a[1][4] && a[2][3] && a[3][2] && a[4][1] && a[1][4] + a[2][3] + a[3][2] +
a[4][1] != 34)
return 0;
return 1;
}
void dfs(int cur)
{
if(cur==17)
{
if(check())
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1;j<=4;++j)
{
cout<<a[i][j];
cout<<" ";
}
cout<<endl;
}
}
cout<<endl;
return;
}
int x = (cur-1)/4 + 1;
int y = (cur-1)% 4 + 1;
if(x==n&&y==m)
{
a[x][y]=1;
dfs(cur+1);
return;
}
for(int i = 2; i <= 16; i++)
{
if(vis[i]==0)
{
vis[i]=1;
a[x][y]=i;
if(check())dfs(cur + 1);//剪枝
vis[i]=0;
a[x][y]=0;
}
}
}
int main()
{
cin>>n>>m;
a[n][m]=1;
vis[1]=1;
dfs(1);
return 0;
}