AOJ 1316 The Sorcerer's Donut

全部列舉出來

然後暴力的放進去 hash 搞定

Copy
/\*
\* GCA : "Computer is artificial subject absolutely,Math is God"
\*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include
#include <queue>
#include <cctype>
#include <utility>
#include <ctime>
using namespace std;
#ifdef DEBUG
#define VAR(a,b) \_\_typeof(b) a=(b)
#define debug(...) printf("DEBUG: "),printf(\_\_VA\_ARGS\_\_)
#else
#define VAR(a,b) \_\_typeof(b) a=(b)
#define debug(...)
#endif
typedef unsigned int uint;
typedef long long int Int;
typedef unsigned long long int UInt;
#define Set(a,s) memset(a,s,sizeof(a))
#define Pln() printf("\\n")
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define M 25
#define PB push\_back
#define oo INT\_MAX
#define FOR(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define eps 1e-9
#define X first
#define Y second
inline bool xdy(double x,double y){return x>y+eps;}
inline bool xddy(double x,double y){return x>y-eps;}
inline bool xcy(double x,double y){return x<y-eps;}
inline bool xcdy(double x,double y){return x<y+eps;}
const Int mod=1000000007;
int nx,ny;
char mz\[M\]\[M\];
set<string\> sid;
int dx\[8\]={0,1,1,1,0,-1,-1,-1};
int dy\[8\]={1,1,0,-1,-1,-1,0,1};
char store\[M\*M\];
int ox,oy;
string ans;
bool vis\[M\]\[M\];
void dfs(int x,int y,int d,int dep){

store\[dep\]=mz\[y\]\[x\];
int nex=(x+dx\[d\]+nx)%nx;
int ney=(y+dy\[d\]+ny)%ny;
store\[dep+1\]=0;
string ss(store);
if(sid.count(ss)&&ss.length()>=2){
if(ss.length()>ans.length())ans=ss;
else if(ss.length()==ans.length()&&ss<ans){
ans=ss;
}
}
sid.insert(ss);
vis\[x\]\[y\]=1;
if(!vis\[nex\]\[ney\])dfs(nex,ney,d,dep+1);
vis\[x\]\[y\]=0;

}
void solve(){
Set(vis,0);
for(int i=0;i<ny;i++){
for(int j=0;j<nx;j++){
for(int k=0;k<8;k++){
vis\[j\]\[i\]=1;
dfs(j,i,k,0);
vis\[j\]\[i\]=0;
}
}
}
if(ans.size())
puts(ans.c\_str());
else puts("0");
}
int main() {
ios\_base::sync\_with\_stdio(0);
while(~scanf("%d%d",&ny,&nx)&&ny&&nx){
sid.clear();
ans="";
for(int i=0;i<ny;i++)scanf("%s",mz\[i\]);
solve();

}

}