您的位置:首页 > 其它

CF 2014 ACM-ICPC Vietnam National Second Round I. Space Tour

2015-03-16 16:53 507 查看
I. Space Tour

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Alpha Centauri-M (ACM) is a planet with marvelous scenes for visitors from the Earth. Luckily you win a ticket to participate in a space tour to Alpha Centauri-M.

The map of the planet ACM is divided into a grid of M rows and
N columns. The columns are indexed from 1 to
N (from left to right) and the rows are indexed from 1 to
M (from top to bottom). There are several cells on the map that are not safe for visitors to explore because of high mountains and volcanoes.

Upon arriving at ACM, the spaceship will land on any safe cell on the grid and passengers can visit the planet via a special system of space cars. From the landing cell
(r0, c0) on the map, a space car can go to either one of the four connected adjacent cells, namely
(r0–1, c0), (r0 + 1, c0), (r0, c0–1), (r0, c0 + 1).
Subsequently, the space car will continue moving the following fixed navigation pattern:

Turn right and go forward one cell
Turn left and go forward one cell
Go back to step 1.

A space car can only visit safe cells, therefore it will stop if the next cell is not safe or is beyond the map boundaries. The following figure illustrates a map consisting of
M = 6 rows and N = 7 columns. From the landing cell
(4, 3), you may visit 16 cells (including the landing cell).



For each landing cell on the map, you can determine the number of cells that you can visit (including the landing cell). Your task is to choose the landing cell from which you can visit the maximum number of cells on Alpha Centauri-M.

Input
The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 20. The following lines describe the datasets.

The first line of a dataset contains 2 space-separated positive integers
M and N
(1 ≤ M, N ≤ 1000). The ith line in the next
M lines of the dataset contains
N binary digits to represent the states of all cells in the
ith row of the map (1: safe cell, 0: unsafe cell).

Output
For each dataset, write in one line the maximum number of cells that you can visit on Alpha Centauri-M (including the landing cell).

Sample test(s)

Input
2
3 3
011
111
111
6 7
1101011
0111111
1111101
1111111
1111110
0111111


Output
8
20


解析

官方题解

考试的时候居然觉得是搜索= =b

DP,对每个方向上进行4次DP,然后O(N^2)的找答案。

然后着陆点会被重复地算4次,输出的时候要-3。不过对于没有着陆点的情况要特判输0。

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

using namespace std;

int Grid[1010][1010],N,M,
up[1010][1010],down[1010][1010],left[1010][1010],right[1010][1010];

void readdata()
{
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
{
getchar();
for(int j=1;j<=M;j++)
{
char c;scanf("%c",&c);
if(c=='1') Grid[i][j]=1; else Grid[i][j]=0;
}
}
}

void dynamic_planning()
{
memset(up,0,sizeof(up));
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if(Grid[i][j])
{
up[i][j]=1;
if(Grid[i-1][j]) up[i][j]=up[i-1][j+1]+2;
}

memset(down,0,sizeof(down));
for(int i=N;i>0;i--)
for(int j=1;j<=M;j++)
if(Grid[i][j])
{
down[i][j]=1;
if(Grid[i+1][j]) down[i][j]=down[i+1][j-1]+2;
}

memset(left,0,sizeof(left));
for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++)
if(Grid[j][i])
{
left[j][i]=1;
if(Grid[j][i-1]) left[j][i]=left[j-1][i-1]+2;
}

memset(right,0,sizeof(right));
for(int i=M;i>0;i--)
for(int j=1;j<=N;j++)
if(Grid[j][i])
{
right[j][i]=1;
if(Grid[j][i+1]) right[j][i]=right[j+1][i+1]+2;
}

int ans=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if(Grid[i][j])
//ans=max(up[i-1][j]+left[i][j-1]+right[i][j+1]+down[i+1][j],ans);
ans=max(up[i][j]+left[i][j]+right[i][j]+down[i][j],ans);
printf("%d\n",max(ans-3,0));
}
int main()
{
int T; scanf("%d",&T);
for(int i=1;i<=T;i++)
{
readdata();
dynamic_planning();
}
//while(1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: