# NEUQ-2021寒假训练-搜索

A - Prime Ring Problem

#include <iostream>
#include <cstring>
using namespace std;

const int maxn = 20 + 5;
int a[maxn], vis[maxn];

int n, cas;

void init ();
bool p (int x);
void dfs (int u);

int main()
{
while (~scanf ("%d", &n))
{
printf ("Case %d:\n", ++ cas);
init ();
dfs (1);
putchar ('\n');
}
return 0;
}

void init ()
{
a[0] = 1;
memset (vis, 0, sizeof vis);
}

bool p (int x)
{
if (x == 1) return 0;
for (int i = 2; i * i <= x; i ++)
{
if (x % i == 0) return 0;
}
return 1;
}

void dfs (int u)
{
if (u == n && p(1 + a[n - 1]))
{
printf ("%d", 1);
for (int i = 1; i < n; i ++)
{
printf (" %d", a[i]);
}
putchar ('\n');
}

for (int i = 2; i <= n; i ++)
{
if (!vis[i] && p(a[u - 1] + i))
{
a[u] = i;
vis[i] = 1;
dfs (u + 1);
vis[i] = 0;
}
}
}


B - 生日蛋糕

#include<iostream>
using namespace std;

const int inf = 0x3f3f3f;
const int maxn = 20 + 5;
int mins[maxn];
int minv[maxn];
int vol, flo;
int ans;

void dfs (int m, int sums, int sumv, int r, int h)
{
if (m == 0)
{
if (sums < ans && sumv == vol)
{
ans = sums;
}
return;
}

if (mins[m] + sums > ans || minv[m] + sumv > vol)
{
return;
}

if (2 * (vol - sumv) / r + sums >= ans)
{
return;
}

for (int i = r - 1; i >= m; i --)
{
if (m == flo)
{
sums = i * i;
}
for (int j = h - 1; j >= m; j --)
{
dfs (m - 1, sums + i * j * 2, sumv + i * i * j, i, j);
}
}
}

int main()
{
cin >> vol >> flo;

for (int i = 1; i < maxn; i ++)
{
mins[i] = mins[i - 1] + i * i * 2;
minv[i] = minv[i - 1] + i * i * i;
}

ans = inf;
dfs (flo, 0, 0, 100, 10000);
if (ans == inf)
{
ans = 0;
}
cout << ans << endl;

return 0;
}


C - 放苹果

#include<iostream>
using namespace std;

int T, m, n;

int dfs (int m, int n)
{
if (m == 0 || n == 1) return 1;
if (m < n) return dfs (m, m);
else return dfs (m, n - 1) + dfs (m - n, n);
}

int main()
{
cin >> T;
while (T --)
{
scanf ("%d %d", &m, &n);
printf ("%d\n", dfs(m, n));
}
return 0;
}



D - Dungeon Master

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 30 + 5;
char map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int l, r, c, sx, sy, sz, ex, ey, ez;
int to[6][3] = {{0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0}};

struct node
{
int x, y, z, step;
};

int check (int x,int y,int z)
{
if(x < 0 || y < 0 || z < 0 || x >= l || y >= r || z >= c) return 1;
if(map[x][y][z] == '#') return 1;
if(vis[x][y][z]) return 1;
return 0;
}

int bfs()
{
queue <node> q;
node a;
a.x = sx;
a.y = sy;
a.z = sz;
a.step = 0;
q.push(a);

vis[sx][sy][sz] = 1;

while(!q.empty())
{
a = q.front();
q.pop();

if(a.x == ex && a.y == ey && a.z == ez) return a.step;

for(int i = 0; i < 6; i ++)
{
node next = a;

next.x = a.x + to[i][0];
next.y = a.y + to[i][1];
next.z = a.z + to[i][2];

if(check(next.x, next.y, next.z)) continue;

vis[next.x][next.y][next.z] = 1;
next.step = a.step + 1;
q.push (next);
}
}
return 0;
}

int main()
{
while (scanf("%d %d %d",&l, &r, &c), l + r + c)
{
memset(vis, 0, sizeof vis);
for(int i = 0; i < l; i ++)
{
for(int j = 0; j < r; j ++)
{
scanf("%s", map[i][j]);
for(int k = 0; k < c; k ++)
{
if(map[i][j][k] == 'S')
{
sx = i, sy = j, sz = k;
}
if(map[i][j][k] == 'E')
{
ex = i, ey = j, ez = k;
}
}
}
}
int ans = bfs();
if(ans) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

fwxljwawawawawa

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

11-29 2763
10-20 768
01-31 141
12-03 907
12-09 409