# Winter vacation study record D22

Posted by robinas on Sat, 05 Feb 2022 18:59:58 +0100

Deep search

## Title Description

Qi Qi and the driver are playing the stand-alone game "Red Alert IV". Now their game map is divided into an n*m square map. Qi Qi's base is in the top four rows, and the driver's base is in the bottom four rows. They have only one attack mode: long-range artillery. The relevant attributes are as follows:

1. Cannons can hit anywhere on the map.

2. Each time, both sides must use one of their own artillery to attack, take the lead, and attack alternately.
3. One side's artillery can only attack the other side's artillery, and cannot attack its own side or forcibly attack areas without vision.
4. The artillery of the other side will produce a 3 * 3 spread area centered on the attack point. If there are other artillery in the spread area, it will also produce a 3 * 3 spread area.
5. The bases of the two sides are far apart, so there is no situation that the artillery of our side will be affected when attacking the enemy's artillery.
Qi Qi secretly opened the "spy satellite", so he could see the deployment of the driver's cannon, while the driver had no vision. However, if Qi Qi makes an attack, the driver will immediately obtain the view of the artillery launching the attack and use the artillery (if any) to destroy it at the beginning of the turn (the chain that may be generated after destruction is not included in the view).
Now the artillery deployment of Qi Qi and the driver is given. Under the optimal strategy, Qi Qi can retain up to a few of his own artillery after destroying all the drivers' artillery.

## Enter Description:

```In line 1, enter an integer m to represent the width of the map.
In line 2-5, input a string of length m in each line to represent the deployment of the driver's cannon. (the cannon is "*" and the open space is "." No.)
In lines 6-9, enter a string of length m in each line to represent the deployment of neat artillery. (the cannon is "*" and the open space is "." No.)
Data guarantee: 0 < m ≤ 100```

## Output Description:

`Output a line, an integer. The representative destroys all drivers' cannons and retains up to a few cannons. If all drivers' cannons cannot be destroyed, output - 1.`

Example 1

```3
...
.*.
..*
*..
*..
.**
...
*.*```

`4`

Example 2

```3
*..
..*
...
...
...
...
.*.
...```

## output

`-1`

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 3;
int fa[8 * maxn];
int dis[8 * maxn];
int dxdy[8][2] = { {1, 1}, {1, 0},
{1, -1}, {0, 1},
{0, -1}, {-1, 1},
{-1, 0}, {-1, -1}
};
string s[8];
int playerB;
int playerA;
int num[maxn];
int sum;
int find(int x)
{
if(fa[x]==x) return x;
else fa[x] = find(fa[x]);
}
void unionn(int x, int y) {
int a = find(x);
int b = find(y);
if (a != b)
{
fa[a] = b;
dis[b] += dis[a];
}
}
int main() {
memset(num, 0, sizeof(num));
int m;
scanf("%dis",&m);
for (int i = 0; i < 8; i++)
{
cin >> s[i];
for (int j = 0; j < m; j++)
{
fa[i * m + j] = i * m + j;
dis[i * m + j] = 1;
}
}
for (int i = 0; i < 8; i++)
for (int j = 0; j < m; j++)
if ('*' == s[i][j])
for (int k = 0; k < 8; k++)
{
int dx = i + dxdy[k][0];
int dy = j + dxdy[k][1];
if ( ( dx && dx <= 3 && i <= 3) || (dx >= 4 && dx <= 7 && i >= 4) )
{
if (dy && dy <= m - 1)
{
if ( s[dx][dy]=='*' )unionn(dx * m + dy, i * m + j);
}//If it's Qiqi's territory

}//If it's not out of bounds

}

for (int i = 0; i < 8; i++)
for (int j = 0; j < m; j++)
{
int w=i*m+j;
if ('*' == s[i][j] && fa[m] == m)
{
if (i <= 3) playerB++;
else num[playerA++] = dis[i * m + j];
}
}

if (playerA < playerB)
{
cout << -1 << endl;
return 0;
}

sort(num, num + playerA);

for (int i = playerB - 1; i <= playerA - 1; i++)
sum += num[i];

printf("%d\n",sum);
return 0;
}

```

Eight digit
describe
There are 8 numbers from 1 to 8 in the Jiugong grid, and another is a space. The number adjacent to the space can be moved to the position of the space. Ask how many steps it takes to reach the target state in a given state (use 0 to represent the space):
1 2 3
4 5 6
7 8 0

input
Enter a given state.

output
Output the minimum number of steps to reach the target state. Output - 1 when not reachable.

sample input
1 2 3
4 0 6
7 5 8

sample output
2

```#include<iostream>
#include<map>
#include<queue>

using namespace std;

queue<int>Q;
map<int,int>vis;//Tag array
map<int,int>step;//Record the number of steps in this state

int dir[4][2]={-1,0,0,1,1,0,0,-1};//Up, right, down, left
int mat[3][3];
int state;//Initial state of input
int r,c;

void input()//Enter the data and convert the matrix to a nine digit integer
{
int tmp=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
cin>>mat[i][j];
tmp=tmp*10+mat[i][j];
}
state=tmp;
}

bool can_move(int u,int d)//Judge whether you can go
{
for(int i=2;i>=0;i--)//It is easy to judge by changing the integer back to the matrix
{
for(int j=2;j>=0;j--)
{
mat[i][j]=u%10;
u/=10;
if(mat[i][j]==0)
{
r=i;
c=j;
}
}
}
//Judge whether the four directions can go
if((d==0&&r==0)||(d==1&&c==2)||(d==2&&r==2)||(d==3&&c==0))
return 0;
return 1;
}

{
int tmp=0;
int nr=r+dir[d][0];
int nc=c+dir[d][1];
mat[r][c]=mat[nr][nc];
mat[nr][nc]=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
tmp=tmp*10+mat[i][j];
}
return tmp;
}

int bfs(int s)
{
Q.push(s);
vis[s]=1;
step[s]=0;
while(Q.size())
{
int u,v;
u=Q.front();
Q.pop();
if(u==123456780)
return step[u];
for(int i=0;i<4;i++)
{
if(can_move(u,i))
{
v=move_to(u,i);
if(!vis[v])
{
vis[v]=1;
step[v]=step[u]+1;
Q.push(v);
}
}
}
}
return -1;
}

int main()
{
input();
int ans=bfs(state);
cout<<ans<<endl;
return 0;
}

```