CF1214D

CF1214D

Meaning of the questions:

Give you a $ n \ times m $ matrix, find the least number of barriers to use, the $ (1,1) $ path $ (n, m) $ is blocked.

Meaning of the questions:

Because the starting point for both sides can be blocked, so the answer is up to $ 2 $, so the answer is only $ 0,1,2 $.
DFS twice plucked from both the first $ (1,1) reaching point can reach $ $ (n, m) $ to see if the number of points in each step to reach only one.

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define LL long long
#define N 1000010

int n,m,s,ans,f;
bool vis[N];
char ch[N];

void dfs(int x) {
    if(ans) return;
    if(x == f) {
        ans++;
        return;
    }
    int l = x / m,r = x % m;
    if(l + 1 < n && vis[(l + 1) * m + r] == 0 && ch[(l + 1) * m + r] != '#') {
        vis[(l + 1) * m + r] = 1;
        dfs((l + 1) * m + r);
    }
    if(ans) return;
    if(r + 1 < m && vis[l * m + r + 1] == 0 && ch[l * m + r + 1] != '#') {
        vis[l * m + r + 1] = 1;
        dfs(l * m + r + 1);
    }
}

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ; i++)
        scanf("%s",ch + i * m);
    f = n * m - 1,ans = 0;
    vis[s] = 1,vis[f] = 0;
    dfs(s);
    if(ans == 0) {
        puts("0");
        return 0;
    }
    vis[s] = 1,vis[f] = 0;
    ans = 0;
    dfs(s);
    if(ans == 0) puts("1");
    else puts("2");
    //system(
    return 0;
}

Guess you like

Origin www.cnblogs.com/Repulser/p/11469851.html