PAT顶级(DFS)——1012 Greedy Snake (35 分)

1012 Greedy Snake (35 分)


解题思路:

emmm
一开始以为是正常的贪吃蛇规则(即可以随意上下左右走),然后发现样例都看不懂了,直接人傻了。
看了好久题目才发现是选了一个方向走到不能走才能转弯,本身题目没什么好说的,裸搜索题,在DFS的时候针对本题规则修改搜索方式,多传一个参数作为方向:如果是刚进来或者下一个走的位置不能走了,则进行正常的4个方向DFS,否则只能沿着方向继续DFS
针对题目的两个问题,直接枚举暴力处理即可:

  1. 枚举每个位置作为起点跑DFS
  2. 枚举每个果子的位置,修改它为障碍,然后再跑步骤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;
}
}```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值