码迷,mamicode.com
首页 > 其他好文 > 详细

luoguP5003 跳舞的线-乱转弯

时间:2019-10-13 23:29:04      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:spl   end   char   can   read   har   dig   etc   turn   

下面讨论最大值,最小值类似.

\(f[i][j][0/1]\)表示从右或上走到\((i,j)\)时最大拐弯数.

\[\therefore f[i][j][k]=\begin{cases}\max(f[i][j][k],\max(f[i-dx[k]][j-dy[k]][k],f[i-dx[k]][j-dy[k]][!k]+1)))&map[i][j]='\!o'\\-inf&map[i][j]!='\!\#'\end{cases}\]

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define il inline
#define rg register
#define gi read<int>
using namespace std;
const int O = 1e3 + 10;
const int dx[2] = {1, 0}, dy[2] = {0, 1};
template<class TT>
il TT read() {
    TT o = 0, fl = 1; char ch = getchar();
    while (!isdigit(ch)) fl ^= ch == '-', ch = getchar();
    while (isdigit(ch)) o = o * 10 + ch - '0', ch = getchar();
    return fl ? o : -o;
}
bool v[O][O];
int n, m, fx[O][O][2], fy[O][O][2];
char s[O];
int main() {
    n = gi(), m = gi();
    for (int i = 1; i <= n; ++i) {
        scanf("%s", s + 1);
        for (int j = 1; j <= n; ++j)
            v[i][j] = s[j] == '#';
    }
    if (v[1][1]) return 0 & puts("-1");
    memset(fx, -127, sizeof fx);
    memset(fy, 127, sizeof fy);
    fx[1][1][0] = fy[1][1][0] = fx[1][1][1] = fy[1][1][1] = 0;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            if(v[i][j] ^ 1)
                for (int k = 0; k <= 1; ++k) {
                    fx[i][j][k] = max(fx[i][j][k],max(fx[i - dx[k]][j - dy[k]][k], fx[i - dx[k]][j - dy[k]][k ^ 1] + 1));
                    fy[i][j][k] = min(fy[i][j][k],min(fy[i - dx[k]][j - dy[k]][k], fy[i - dx[k]][j - dy[k]][k ^ 1] + 1));
                }
    if (max(fx[n][m][0], fx[n][m][1]) == fx[0][0][0]) return 0 & puts("-1");
    printf("%d %d\n", max(fx[n][m][0], fx[n][m][1]) - 1, min(fy[n][m][0], fy[n][m][1]));
    return 0;
}

luoguP5003 跳舞的线-乱转弯

标签:spl   end   char   can   read   har   dig   etc   turn   

原文地址:https://www.cnblogs.com/lylyl/p/11668964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!