hdu5024-Wang Xifeng's Little Plot

此题一开始用暴力做,后来发现斜着走的时候其实暴力不太好写,于是改用搜索写了

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <memory.h>
  4 using namespace std;
  5
  6 char a[110][110]= {0};
  7 int down[110][110]= {0};
  8 int up[110][110]= {0};
  9
 10 int cnt[110][110][4]= {0};
 11 int n;
 12 int dx[4]= {-1,1,-1, 1};
 13 int dy[4]= {-1,1, 1,-1};
 14 int dfs(int curx,int cury,int num)
 15 {
 16     if(curx<0 || cury<0 || curx>=n || cury>=n)
 17         return 0;
 18     int &ans=cnt[curx][cury][num];
 19     if(a[curx][cury]==‘#‘)
 20         return ans=0;
 21     return ans=1+dfs(curx+dx[num],cury+dy[num],num);
 22 }
 23
 24
 25 int getMax()
 26 {
 27     int ans=0;
 28     for(int i=0; i<n; i++)
 29         for(int j=0; j<n; j++)
 30         {
 31             ans=max(ans,down[i][j]+up[i][j]-1);
 32         }
 33     return ans;
 34 }
 35
 36 int main()
 37 {
 38     freopen("in.txt","r",stdin);
 39
 40     while(scanf("%d",&n),n)
 41     {
 42         memset(down,0,sizeof down);
 43         memset(up,0,sizeof up);
 44         memset(cnt,0,sizeof cnt);
 45         for(int i=0; i<n; i++)
 46             scanf("%s",a[i]);
 47
 48         for(int i=0; i<n; i++)
 49             for(int j=0; j<n; j++)
 50             {
 51                 if(a[i][j]==‘#‘)
 52                 {
 53                     up[i][j]=down[i][j]=0;
 54
 55                 }
 56                 else
 57                 {
 58                     up[i][j]=down[i][j]=1;
 59                     if(i!=0)
 60                         up[i][j]=up[i-1][j]+1;
 61                     if(j!=0)
 62                         down[i][j]=down[i][j-1]+1;
 63                 }
 64             }
 65
 66         int ans=0;
 67         ans=max(ans,getMax());
 68
 69         memset(down,0,sizeof down);
 70         memset(up,0,sizeof up);
 71         for(int i=n-1; i>=0; i--)
 72             for(int j=n-1; j>=0; j--)
 73             {
 74                 if(a[i][j]==‘#‘)
 75                 {
 76                     up[i][j]=down[i][j]=0;
 77                 }
 78                 else
 79                 {
 80                     up[i][j]=down[i][j]=1;
 81                     if(i!=n-1)
 82                         up[i][j]=up[i+1][j]+1;
 83                     if(j!=n-1)
 84                         down[i][j]=down[i][j+1]+1;
 85                 }
 86             }
 87
 88         ans=max(ans,getMax());
 89
 90 //---------------------------
 91         memset(down,0,sizeof down);
 92         memset(up,0,sizeof up);
 93         for(int i=0; i<n; i++)
 94             for(int j=n-1; j>=0; j--)
 95             {
 96                 if(a[i][j]==‘#‘)
 97                 {
 98                     up[i][j]=down[i][j]=0;
 99                 }
100                 else
101                 {
102                     up[i][j]=down[i][j]=1;
103                     if(i!=0)
104                         up[i][j]=up[i-1][j]+1;
105                     if(j!=n-1)
106                         down[i][j]=down[i][j+1]+1;
107                 }
108             }
109
110         ans=max(ans,getMax());
111 //-------------------------------
112         memset(down,0,sizeof down);
113         memset(up,0,sizeof up);
114         for(int i=n-1; i>=0; i--)
115             for(int j=0; j<n; j++)
116             {
117                 if(a[i][j]==‘#‘)
118                 {
119                     up[i][j]=down[i][j]=0;
120                 }
121                 else
122                 {
123                     up[i][j]=down[i][j]=1;
124                     if(i!=n-1)
125                         up[i][j]=up[i+1][j]+1;
126                     if(j!=0)
127                         down[i][j]=down[i][j-1]+1;
128                 }
129             }
130
131         ans=max(ans,getMax());
132 //--------------------------------------------------
133
134         for(int i=0; i<n; i++)
135             for(int j=0; j<n; j++)
136             {
137                 for(int num=0; num<4; num++)
138                 {
139                     dfs(i,j,num);
140                 }
141             }
142
143         for(int i=0; i<n; i++)
144             for(int j=0; j<n; j++)
145             {
146                 ans=max(ans,cnt[i][j][0]+cnt[i][j][2]-1);
147                 ans=max(ans,cnt[i][j][0]+cnt[i][j][3]-1);
148                 ans=max(ans,cnt[i][j][1]+cnt[i][j][3]-1);
149                 ans=max(ans,cnt[i][j][1]+cnt[i][j][2]-1);
150             }
151
152         cout<<ans<<endl;
153     }
154     return 0;
155 }

hdu5024-Wang Xifeng's Little Plot

时间: 2024-12-29 23:39:24

hdu5024-Wang Xifeng's Little Plot的相关文章

HDU5024 Wang Xifeng&#39;s Little Plot 【记忆化搜索】

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 214 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu5024 Wang Xifeng&#39;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

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 5024 Wang Xifeng&#39;s Little Plot (搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 157    Accepted Submission(s): 105 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

2014 网选 5024 Wang Xifeng&#39;s Little Plot

题意:从任意一个任意一个可走的点开始找一个最长的路,这条路如果有转弯的话, 那么必须是 90度,或者没有转弯! 思路: 首先用dfs将所有可走点开始的 8 个方向上的线段的最长长度求出来 ! step[i][j][k] 表示的是(i,j)沿着k方向一直走到头或者转弯时的最长步数! 最后枚举每一个可走点转弯为90度的路径,找到最长的长度! step[i][j][k1] + step[i][j][k2] 就是 (i, j)这个点 k1 和 k2方向构成90度! 1 #include<iostream

HDU - 5024 Wang Xifeng&#39;s Little Plot

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.

HDU 5024 Wang Xifeng&#39;s Little Plot(2014广州网络赛1003)

写了1h的DFS,简直被自己的代码吓哭了..不过起码还是思路清晰,QUQ~ 说一下题意吧: 题意是求一条最长路,最多能经过一次转弯,并且其角度只能为90度. 拿第一个样例来说:(0,1)->(1,2)->[转弯](2,1) ,所以答案是3. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 代码如下: #include<iostream> #include<cstdio> #include<cstring>

HDU 5024 Wang Xifeng&#39;s Little Plot(暴力枚举+瞎搞)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 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. Thi