2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng's Little Plot HDU5024, icpcxifeng
A good enumeration + simulated question. Change thinking perspective
I did this question, N <= 100 in size, and thought it was a normal DFS search, so I wrote it dumbly. Various conditions limit the simulation process
However, a careful analysis shows that each vertex is traversed in all eight directions 100X100X100 ^ 8. X points, select from 8 when each point goes, TLE
So we changed it to another angle:
Take the compliant point as the turning point and walk in two vertical directions to find the farthest distance. In this way, you only need to know the length of each vertex and combine the corresponding values.
This avoids deep search for each vertex.
PS: When searching, x and y are reversed, leading to a problem in the diagram. Later, [dy] [dx] is used to indicate the position, and the front row changes. It is best to write the search time mark at the beginning and end of the function to adapt to complex situations.
#include <stdio.h>#include <string.h>#include<iostream>using namespace std;#define mem(a) memset(a,0,sizeof(a))#define max(a,b) (a)>(b)?(a):(b)typedef __int64 LL;const LL maxn=10+100;int maps[maxn][maxn];int vis [maxn][maxn];int N,M,T;int dt[][2]={{0,-1}, {1,-1}, {1,0} , {1,1}, {0,1}, {-1,1}, {-1,0}, {-1,-1}};int step[8];int IsInBord(int x,int y){ if(x<0 || x>=N || y<0 || y>=N) return 0; return 1;}int main(){#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin);#endif int i,j,k; char str[100]; while(scanf("%d",&N),N) { getchar(); mem(maps); for(i=0;i<N;i++) { gets(str); int len=strlen(str); for(j=0;j<len;j++) { if(str[j]=='.') maps[i][j]=1; } } int ans=0; for(i=0;i<N;i++) for(j=0;j<N;j++) { if(maps[i][j]==0 ) continue; int ai, aj; for(k = 0; k < 8;k++) { step[k] = 0; ai = i + dt[k][1]; aj = j + dt[k][0]; //cout<<"k"<<k<<","<<dt[k][1]<<","<<dt[k][0]<<endl; while(IsInBord(ai, aj) && maps[ai][aj] == 1) { step[k]++; ai += dt[k][1]; aj += dt[k][0]; //cout<<ai<<","<<aj<<","<<maps[ai][aj]<<endl; } } int res = 0; for(k = 0; k < 8; k++) { if(step[k] + step[(k + 2) % 8] + 1 > res) { res = step[k] + step[(k + 2) % 8] + 1; } } ans = max(res, ans); } printf("%d\n",ans); } return 0;}
What is ACM/ICPC Asia Regional?
International. The finals are world-class. Asia belongs to the intercontinental and international level!