E题
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define ll long long
#define rep(i,a,n) for(ll i = a; i <= n; i ++)
#define per(i,a,n) for(ll i = n; i >= a; i --)
#define pb push_back
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define PII std::pair<ll,ll>
const ll N = 100 + 10;
const ll mod = 1e9 + 7;
char op[N][N];
ll n,m;
ll dx[] = {0,0,1,-1};
ll dy[] = {1,-1,0,0};
ll res = 1e8;
ll nn,mm;
ll dis[N][N][5];
struct ii{
ll a;
ll b;
ll c;
};
void solve(){
std::cin >> n >> m;
//初始化距离为最大
for(ll i = 1; i <= n; i ++){
for(ll j = 1; j <= m; j ++){
for(ll k = 0; k < 4; k ++) dis[i][j][k] = 1e7;
}
}
std::queue<ii> dp;
for(ll i = 1; i <= n; i ++){
std::string a;
std::cin >> a;
a= ' ' + a;
for(ll j = 1; j <= m; j ++){
op[i][j] = a[j];
if(a[j] == 'S'){
for(ll k = 0; k < 4; k ++){
dp.push({i,j,k});
dis[i][j][k] = 0;
op[i][j] = '.';
}
}
if(a[j] == 'T'){
nn = i;
mm = j;
}
}
}
//开始bfs
while(dp.size()){
auto jj = dp.front();
dp.pop();
ll x1 = jj.a;
ll y1 = jj.b;
ll z1 = jj.c;
if(op[x1][y1] == '.'){
ll xx = x1 + dx[z1];
ll yy = y1 + dy[z1];
if(xx >= 1 && yy >= 1 && xx <= n && yy <= m && op[xx][yy] != '#'){
if(dis[xx][yy][z1] > dis[x1][y1][z1] + 1){
dis[xx][yy][z1] = dis[x1][y1][z1] + 1;
dp.push({xx,yy,z1});
}
}
}
else if(op[x1][y1] == 'T'){
std::cout << dis[x1][y1][z1] << "\n";
return ;
}
else if(op[x1][y1] == '*'){//蘑菇能够转点
for(ll i = 0; i < 4; i ++){
if(z1 == 0 && i == 1) continue;
if(z1 == 1 && i == 0) continue;
if(z1 == 3 && i == 2) continue;
if(z1 == 2 && i == 3) continue;
ll xx = x1 + dx[i];
ll yy = y1 + dy[i];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && op[xx][yy] != '#'){
// dis[xx][yy][i] = std::min(dis[xx][yy][i],dis[x1][y1][i] + 1);
if(dis[xx][yy][i] > dis[x1][y1][z1] + 1){
dis[xx][yy][i] = dis[x1][y1][z1] + 1;
dp.push({xx,yy,i});
}
}
}
}
}
std::cout << -1 << "\n";
return ;
}
int main(){
ll t = 1;
std::cin >> t;
while(t --)
solve();
return 0;
}
F题
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#define ll long long
const ll N = 1e3;
ll op[500];
ll dp[500][2];
int main(){
ll _;
std::cin >> _;
while(_ --){
ll n;
std::memset(dp,0x3f,sizeof dp);
std::memset(op,0,sizeof op);
std::cin >> n;
// std::vector<ll> op(n + 1);
// std::vector<ll> dp(300);
for(ll i = 1; i <= n; i ++){
ll a;
std::cin >> a;
op[a] = 1;
}
for(ll i = 0; i <= 210; i ++){
if(op[i]){
dp[i][1] = 1;
for(ll j = 0; j <= 210; j ++){
dp[i & j][1] = std::min(dp[i & j][1],dp[j][0] + 1);
}
for(ll j = 0; j <= 210; j ++){
dp[j][0] = dp[j][1];
}
}
}
if(dp[0][0] > n) std::cout << -1 << "\n";
else
std::cout << n - dp[0][0] << "\n";
}
return 0;
}