《Unaccepted 100》
查看原帖
《Unaccepted 100》
706974
zcs_kim楼主2023/4/9 18:05

Subtask #1太毒瘤了。。。

code:

#include <iostream>
#include <cstring>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int a[15][15], n, m, sum, ans;
bool f[15][15];
void dfs(int x, int y, int s, int cnt)
{
    if(s == sum / 2)
    {
        ans = min(ans, cnt);
        return;
    }
    if(s > sum / 2) return;
    for(int i = 0; i < 4; i++)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 1 || nx > n || ny < 1 || ny > m || f[nx][ny] == 1) continue;
        f[nx][ny] = 1;
        dfs(nx, ny, s + a[nx][ny], cnt + 1);
        f[nx][ny] = 0;
    }
}
int main()
{
    cin >> m >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            sum += a[i][j];
        }
    if(sum % 2)
    {
        cout << 0 << endl;
        return 0;
    }
    ans = 110;
    dfs(1, 1, a[1][1], 1);
    if(ans == 110) cout << 0 << endl;
    else cout << ans << endl;
    return 0;
}

《Unaccepted 100》

2023/4/9 18:05
加载中...