Codeforces Round #224 (Div. 2) D. Ksenia and Pawns

D. Ksenia and Pawns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ksenia has a chessboard of size n × m. Each cell of the chessboard contains one of the characters: "<", ">", "^", "v", "#". The cells that contain character "#" are blocked. We know that all chessboard cells that touch the border are blocked.

Ksenia is playing with two pawns on this chessboard. Initially, she puts the pawns on the chessboard. One cell of the chessboard can contain two pawns if and only if the cell is blocked. In other cases two pawns can not stand in one cell. The game begins when Ksenia put pawns on the board. In one move, Ksenia moves each pawn to a side adjacent cell in the direction of arrows painted on the cell on which the corresponding pawn sits (if the pawn sits on "#", it does not move). Assume that Ksenia moves pawns simultaneously (see the second test case).

Of course, Ksenia plays for points. How can one calculate the points per game? Very simply! Let's count how many movements the first pawn made and how many movements the second pawn made, sum these two numbers — it will be the resulting score of the game.

Ksenia wonders: what is the maximum number of points she can earn (for that, she should place the pawns optimally well early in the game). Help her and find that number.

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 2000) — the sizes of the board. Each of the following n lines contains mcharacters – the board's description. Each character is one of the characters: "<", ">", "^", "v", "#".

It is guaranteed that the border cells of the table are blocked cells (with character "#").

Output

If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get.

Sample test(s)
input
1 1
#
output
0
input
3 4
####
#>^#
####
output
3
input
3 4
####
#><#
####
output
-1
input
7 5
#####
##v##
##v##
#####
##^##
##^##
#####
output
4
input
7 5
#####
##v##
##v##
##<##
##^##
##^##
#####
output
5



题意:

在棋盘上摆两个兵,按照棋盘指示走,两个兵只有在'#'格才能位于同一格,问怎样摆放使得两个兵走的步数之和最大。


思路:

找出最长的一条路(长度为best),如果有两条不相同的,则为2*best ,若两条路有交集,则为 2*best-1 (一前一后放),由于路径固定,可将一条路径标记,看其他的是否与其有不为‘#’的交集就够了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 2005
#define MAXN 400005
#define mod 1000000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,k,ans,cnt,tot,flag;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dp[maxn][maxn];
int nx[maxn*maxn],ny[maxn*maxn];

int dfs(int x,int y)
{
    if(dp[x][y]!=-1) return dp[x][y];
    int i,j,t,best=0;
    if(mp[x][y]=='^')
    {
        if(vis[x-1][y]) return INF;
        vis[x-1][y]=1;
        t=dfs(x-1,y);
        vis[x-1][y]=0;
    }
    else if(mp[x][y]=='v')
    {
        if(vis[x+1][y]) return INF;
        vis[x+1][y]=1;
        t=dfs(x+1,y);
        vis[x+1][y]=0;
    }
    else if(mp[x][y]=='<')
    {
        if(vis[x][y-1]) return INF;
        vis[x][y-1]=1;
        t=dfs(x,y-1);
        vis[x][y-1]=0;
    }
    else if(mp[x][y]=='>')
    {
        if(vis[x][y+1]) return INF;
        vis[x][y+1]=1;
        t=dfs(x,y+1);
        vis[x][y+1]=0;
    }
    else t=-1;
    dp[x][y]=t+1;
    return t+1;
}
void dfs1(int x,int y)
{
 //   printf("x:%d y:%d\n",x,y);
    if(mp[x][y]=='#')
    {
        if(k) flag=1;
        return ;
    }
    vis[x][y]=1;
    if(mp[x][y]=='^')
    {
        if(!vis[x-1][y]) dfs1(x-1,y);
    }
    else if(mp[x][y]=='v')
    {
        if(!vis[x+1][y]) dfs1(x+1,y);
    }
    else if(mp[x][y]=='<')
    {
        if(!vis[x][y-1]) dfs1(x,y-1);
    }
    else if(mp[x][y]=='>')
    {
        if(!vis[x][y+1]) dfs1(x,y+1);
    }
}
void solve()
{
    int i,j,t,best=-1;
    cnt=0;
    memset(dp,-1,sizeof(dp));
    memset(vis,0,sizeof(vis));
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            if(mp[i][j]!='#')
            {
                vis[i][j]=1;
                t=dfs(i,j);
                vis[i][j]=0;
                if(t>best)
                {
                    best=t;
                    cnt=1;
                    nx[1]=i,ny[1]=j;
                }
                else if(t==best)
                {
                    cnt++;
                    nx[cnt]=i,ny[cnt]=j;
                }
                if(best>=INF)
                {
                    ans=-1;
                    return ;
                }
            }
        }
    }
    if(best==-1)
    {
        ans=0;
        return ;
    }
    memset(vis,0,sizeof(vis));
    k=0;
    dfs1(nx[1],ny[1]);
    flag=0;
    k=1;
    for(i=2;i<=cnt;i++)
    {
        dfs1(nx[i],ny[i]);
        if(flag) break ;
    }
    ans=2*best-1+flag;
}
int main()
{
    int i,j,t;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1; i<=n; i++)
        {
            scanf("%s",mp[i]+1);
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值