hdu5024 Wang Xifeng's Little Plot (水

时间:2022-03-20 08:14:14

http://acm.hdu.edu.cn/showproblem.php?pid=5024

网络赛

Wang Xifeng's Little Plot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 239    Accepted Submission(s): 156

Problem Description
《Dream of the Red Chamber》(also 《The Story of the Stone》) is one of
the Four Great Classical Novels of Chinese literature, and it is
commonly regarded as the best one. This novel was created in Qing
Dynasty, by Cao Xueqin. But the last 40 chapters of the original version
is missing, and that part of current version was written by Gao E.
There is a heart breaking story saying that after Cao Xueqin died, Cao's
wife burned the last 40 chapter manuscript for heating because she was
desperately poor. This story was proved a rumor a couple of days ago
because someone found several pages of the original last 40 chapters
written by Cao.

In the novel, Wang Xifeng was in charge of Da
Guan Yuan, where people of Jia family lived. It was mentioned in the
newly recovered pages that Wang Xifeng used to arrange rooms for Jia
Baoyu, Lin Daiyu, Xue Baochai and other teenagers. Because Jia Baoyu was
the most important inheritor of Jia family, and Xue Baochai was
beautiful and very capable , Wang Xifeng didn't want Jia Baoyu to marry
Xue Baochai, in case that Xue Baochai might take her place. So, Wang
Xifeng wanted Baoyu's room and Baochai's room to be located at two ends
of a road, and this road should be as long as possible. But Baoyu was
very bad at directions, and he demanded that there could be at most one
turn along the road from his room to Baochai's room, and if there was a
turn, that turn must be ninety degree. There is a map of Da Guan Yuan in
the novel, and redists (In China English, one whose job is studying
《Dream of the Red Chamber》is call a "redist") are always arguing about
the location of Baoyu's room and Baochai's room. Now you can solve this
big problem and then become a great redist.

Input
The map of Da Guan Yuan is represented by a matrix of characters '.'
and '#'. A '.' stands for a part of road, and a '#' stands for other
things which one cannot step onto. When standing on a '.', one can go
to adjacent '.'s through 8 directions: north, north-west, west,
south-west, south, south-east,east and north-east.

There are several test cases.

For each case, the first line is an integer N(0<N<=100) ,meaning the map is a N × N matrix.

Then the N × N matrix follows.

The input ends with N = 0.

Output
For each test case, print the maximum length of the road which Wang
Xifeng could find to locate Baoyu and Baochai's rooms. A road's length
is the number of '.'s it includes. It's guaranteed that for any test
case, the maximum length is at least 2.
Sample Input
3
#.#
##.
..#
3
...
##.
..#
3
...
###
..#
3
...
##.
...
0
Sample Output
3
4
3
5
Source
Recommend
hujie

题意:

给出矩阵地图,.能走#不能走,八个方向都可以走,求某个点开始走一波直线然后转个90度的弯再走一波直线的最长的路能走多长。

题解:

预处理出每个点向各个方向能走多长,然后枚举拐弯处。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const double eps=1e-;
const double pi=acos(-1.0); const int gx[]= {,,-,};
const int gy[]= {,,,}; const int maxn=;
int n;
char a[maxn][maxn];
int b[maxn][maxn][];///0- 1| 2/ 3"\" b[x][y][dr],在dr方向,以x,y为端点的最长线段的长度,包括x,y inline bool in(const int &x,const int &y) {
return(x>= && x<=n && y>= && y<=n);
} void gank(int X,int Y,int dr) {
int x=X,y=Y;
bool flag=;
int fx,fy;
int step=;
while(in(x,y)) {
//printf("a[%d][%d]=%c\n",x,y,a[x][y]);
if(!flag && a[x][y]=='.') {
flag=;
fx=x;
fy=y;
step=;
}
if(flag && a[x][y]=='#') {
flag=;
int nStep=;
//printf("%d,%d,%d,%d,%d\n",x,y,fx,fy,step);
while(in(fx,fy) && !(fx==x && fy==y)) {
b[fx][fy][dr]=max(nStep,step-nStep+);
fx+=gx[dr];
fy+=gy[dr];
nStep++;
}
}
x+=gx[dr];
y+=gy[dr];
step++;
} if(flag) {
flag=;
int nStep=;
//printf("%d,%d,%d,%d,%d\n",x,y,fx,fy,step);
while(in(fx,fy) && !(fx==x && fy==y)) {
b[fx][fy][dr]=max(nStep,step-nStep+);
fx+=gx[dr];
fy+=gy[dr];
nStep++;
}
} } int farm() {
int i,j;
mz(b); ///-
FOR(i,,n) {
gank(i,,);
} ///|
FOR(i,,n) {
gank(,i,);
} ///"/"
FOR(i,,n) {
gank(i,,);
//printf("start at %d,%d:\n",n,i);
gank(n,i,);
} ///"\"
FOR(i,,n) {
gank(i,,);
gank(,i,);
} int ans=;
FOR(i,,n) {
FOR(j,,n) {
//printf("%d,%d %d %d %d %d\n",i,j,b[i][j][0],b[i][j][1],b[i][j][2],b[i][j][3]);
ans=max(ans,b[i][j][]+b[i][j][]-);
ans=max(ans,b[i][j][]+b[i][j][]-);
}
}
return ans;
} int main() {
int i,j;
while(scanf("%d",&n)!=EOF) {
if(n==)break;
FOR(i,,n) {
FOR(j,,n)scanf(" %c",&a[i][j]);
} // printf("\n%d\n",n);
// FOR(i,1,n){
// FOR(j,1,n)printf("%c",a[i][j]);
// puts("");
// }
printf("%d\n",farm());
}
return ;
}