首页 > 代码库 > hdu5024 Wang Xifeng's Little Plot (水

hdu5024 Wang Xifeng's Little Plot (水

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
3435
 

Source
2014 ACM/ICPC Asia Regional Guangzhou Online
 

Recommend
hujie

题意:

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

题解:

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

 

代码:

  1 //#pragma comment(linker, "/STACK:102400000,102400000")  2 #include<cstdio>  3 #include<cmath>  4 #include<iostream>  5 #include<cstring>  6 #include<algorithm>  7 #include<cmath>  8 #include<map>  9 #include<set> 10 #include<stack> 11 #include<queue> 12 using namespace std; 13 #define ll long long 14 #define usll unsigned ll 15 #define mz(array) memset(array, 0, sizeof(array)) 16 #define mf1(array) memset(array, -1, sizeof(array)) 17 #define minf(array) memset(array, 0x3f, sizeof(array)) 18 #define REP(i,n) for(i=0;i<(n);i++) 19 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 20 #define RD(x) scanf("%d",&x) 21 #define RD2(x,y) scanf("%d%d",&x,&y) 22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 23 #define WN(x) printf("%d\n",x); 24 #define RE  freopen("D.in","r",stdin) 25 #define WE  freopen("1biao.out","w",stdout) 26 #define mp make_pair 27 #define pb push_back 28 const double eps=1e-10; 29 const double pi=acos(-1.0); 30  31 const int gx[4]= {0,1,-1,1}; 32 const int gy[4]= {1,0,1,1}; 33  34 const int maxn=111; 35 int n; 36 char a[maxn][maxn]; 37 int b[maxn][maxn][4];///0- 1| 2/ 3"\"  b[x][y][dr],在dr方向,以x,y为端点的最长线段的长度,包括x,y 38  39 inline bool in(const int &x,const int &y) { 40     return(x>=1 && x<=n && y>=1 && y<=n); 41 } 42  43 void gank(int X,int Y,int dr) { 44     int x=X,y=Y; 45     bool flag=0; 46     int fx,fy; 47     int step=0; 48     while(in(x,y)) { 49         //printf("a[%d][%d]=%c\n",x,y,a[x][y]); 50         if(!flag && a[x][y]==.) { 51             flag=1; 52             fx=x; 53             fy=y; 54             step=0; 55         } 56         if(flag && a[x][y]==#) { 57             flag=0; 58             int nStep=1; 59             //printf("%d,%d,%d,%d,%d\n",x,y,fx,fy,step); 60             while(in(fx,fy) && !(fx==x && fy==y)) { 61                 b[fx][fy][dr]=max(nStep,step-nStep+1); 62                 fx+=gx[dr]; 63                 fy+=gy[dr]; 64                 nStep++; 65             } 66         } 67         x+=gx[dr]; 68         y+=gy[dr]; 69         step++; 70     } 71  72     if(flag) { 73         flag=0; 74         int nStep=1; 75         //printf("%d,%d,%d,%d,%d\n",x,y,fx,fy,step); 76         while(in(fx,fy) && !(fx==x && fy==y)) { 77             b[fx][fy][dr]=max(nStep,step-nStep+1); 78             fx+=gx[dr]; 79             fy+=gy[dr]; 80             nStep++; 81         } 82     } 83  84  85 } 86  87 int farm() { 88     int i,j; 89     mz(b); 90  91     ///- 92     FOR(i,1,n) { 93         gank(i,1,0); 94     } 95  96     ///| 97     FOR(i,1,n) { 98         gank(1,i,1); 99     }100 101     ///"/"102     FOR(i,1,n) {103         gank(i,1,2);104         //printf("start at %d,%d:\n",n,i);105         gank(n,i,2);106     }107 108     ///"\"109     FOR(i,1,n) {110         gank(i,1,3);111         gank(1,i,3);112     }113 114 115     int ans=0;116     FOR(i,1,n) {117         FOR(j,1,n) {118             //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]);119             ans=max(ans,b[i][j][0]+b[i][j][1]-1);120             ans=max(ans,b[i][j][2]+b[i][j][3]-1);121         }122     }123     return ans;124 }125 126 int main() {127     int i,j;128     while(scanf("%d",&n)!=EOF) {129         if(n==0)break;130         FOR(i,1,n) {131             FOR(j,1,n)scanf(" %c",&a[i][j]);132         }133 134 //        printf("\n%d\n",n);135 //        FOR(i,1,n){136 //            FOR(j,1,n)printf("%c",a[i][j]);137 //            puts("");138 //        }139         printf("%d\n",farm());140     }141     return 0;142 }
View Code

 

hdu5024 Wang Xifeng's Little Plot (水