2014 网选 5024 Wang Xifeng'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>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define  N  105
 6 using namespace std;
 7
 8 int step[N][N][8];
 9 int dir[8][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1} };
10 int index[8][2] = { {0, 1}, {1, 3}, {2, 3}, {0, 2}, {5, 6}, {5, 7}, {4, 7}, {4, 6}};//每一个节点所对应的转弯的枚举
11 char mp[N][N], vis[N][N];
12 int n;
13
14 bool judge(int x, int y){
15     if(x < 1 || y < 1 || x > n || y > n)
16         return false;
17     if( mp[x][y] == ‘#‘)  return false;
18
19     return true;
20 }
21
22 void dfs(int x, int y){
23     for(int i = 0; i < 8; ++i){
24         int xx = x + dir[i][1];
25         int yy = y + dir[i][0];
26         if(!judge(xx, yy))
27             step[x][y][i] = 1;
28         else{
29             if( !step[xx][yy][i] )//记忆话的赶脚
30                 dfs(xx, yy);
31             step[x][y][i] = 1 + step[xx][yy][i];
32         }
33     }
34 }
35
36 int main(){
37     while(scanf("%d", &n) && n){
38         memset(step, 0, sizeof(step));
39         memset(vis, 0, sizeof(vis));
40         for(int i = 1; i <= n; ++i)
41             scanf("%s", mp[i]+1);
42
43         for(int i = 1; i <= n; ++i)
44             for(int j = 1; j <= n; ++j)
45                 if(mp[i][j] == ‘.‘)
46                            dfs(i, j);
47
48         int maxN = -1;
49         for(int i=1; i <= n; ++i)
50             for(int j = 1; j <= n; ++j){
51                 if(mp[i][j] == ‘.‘)
52                     for(int k = 0; k < 8; ++k)
53                         maxN = max(maxN, step[i][j][index[k][0]] + step[i][j][index[k][1]] );
54             }
55         printf("%d\n", maxN - 1);//因为多加了一次拐点!
56     }
57     return 0;
58 } 

2014 网选 5024 Wang Xifeng's Little Plot

时间: 2024-10-12 00:12:42

2014 网选 5024 Wang Xifeng's Little Plot的相关文章

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 (搜索)

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......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

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

[ACM] HDU 5024 Wang Xifeng&#39;s Little Plot (构造,枚举)

Wang Xifeng'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 Qin

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

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 (枚举 + DFS记忆化搜索)

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

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

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.