1012 Greedy Snake (35 分)
解题思路:
emmm
一开始以为是正常的贪吃蛇规则(即可以随意上下左右走),然后发现样例都看不懂了,直接人傻了。
看了好久题目才发现是选了一个方向走到不能走才能转弯,本身题目没什么好说的,裸搜索题,在DFS的时候针对本题规则修改搜索方式,多传一个参数作为方向:如果是刚进来或者下一个走的位置不能走了,则进行正常的4个方向DFS,否则只能沿着方向继续DFS
针对题目的两个问题,直接枚举暴力处理即可:
- 枚举每个位置作为起点跑DFS
- 枚举每个果子的位置,修改它为障碍,然后再跑步骤1(非常非常暴力,但是就是T不了,这就是PTA吗)
代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define G 6.67430*1e-11
#define rd read()
#define pi 3.1415926535
using namespace std;
const ll mod = 1e9 + 7;
const int MAXN = 30000005;
const int MAX2 = 300005;
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int g[20][20], vis[20][20];
int n, m;
//上下左右
int nextx[4] = { 0,1,0,-1 };
int nexty[4] = { 1,0,-1,0 };
int a[2], b[2];
int dfs(int x, int y, int count,int dir)
{
if (count == (n - 2) * (n - 2) - m)
{
return count;
}
int ma = 0;
int nx = x + nextx[dir], ny = y + nexty[dir];
if (g[nx][ny]||vis[nx][ny] || dir == 5)
{
for (int i = 0; i < 4; i++)
{
int nx = x + nextx[i], ny = y + nexty[i];
if (!vis[nx][ny] && !g[nx][ny])
{
vis[x][y] = 1;
ma = max(ma, dfs(nx, ny, count + 1, i));
vis[x][y] = 0;
}
}
}
else
{
vis[x][y] = 1;
ma = dfs(nx, ny, count + 1, dir);
vis[x][y] = 0;
}
return max(ma, count);
}
signed main()
{
n = rd, m = rd;
for (int i = 0; i < m; i++)
{
a[i] = rd, b[i] = rd;
g[a[i]][b[i]] = 1;
}
for (int i = 1; i <= n; i++)
{
g[1][i] = 1;
g[i][1] = 1;
g[n][i] = 1;
g[i][n] = 1;
}
int ma = 0, c = 0;
for (int i = 2; i <= n - 1; i++)
{
for (int j = 2; j <= n - 1; j++)
{
if (g[i][j])continue;
memset(vis, 0, sizeof vis);
int res = dfs(i, j, 1,5);
if (res > ma)
{
ma = res;
c = 1;
}
else if (res == ma)
{
c++;
}
}
}
cout << (n - 2) * (n - 2) - m - ma << ' ' << c << endl;
if (ma == (n - 2) * (n - 2) - m)
{
cout << -1;
}
else
{
int fama = 0, fac = 0;
m++;
for (int i = 2; i <= n - 1; i++)
{
for (int j = 2; j <= n - 1; j++)
{
if (!g[i][j])
{
g[i][j] = 1;
int ma = 0;
for (int i = 2; i <= n - 1; i++)
{
for (int j = 2; j <= n - 1; j++)
{
if (g[i][j])continue;
memset(vis, 0, sizeof vis);
int res = dfs(i, j, 1,5);
if (res > ma)
{
ma = res;
}
}
}
g[i][j] = 0;
if (ma > fama)
{
fama = ma;
fac = 1;
}
else if (fama == ma)
{
fac++;
}
}
}
}
cout << (n - 2) * (n - 2) - m - fama << ' ' << fac << endl;
}
return 0;
}
}```