hdu 5024 DFS 2014亚洲区域赛广州网赛

来源:互联网 发布:剑网三任务指引软件 编辑:程序博客网 时间:2024/05/02 01:39

http://acm.hdu.edu.cn/showproblem.php?pid=5024

DFS写的好像质量不太高,昨晚T了,然后睡的时候重新理下思路,今天重写下,750MSAC,太慢了

//#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>1;const double EPS = 1e-8;const double pi = acos(-1.0);const int INF = 100000000;const int MAXN = 100+10;char mat[MAXN][MAXN];bool vis[MAXN][MAXN];int n,ans;int dir[8][2]={{-1,-1},{0,-1},{1,-1},                {-1,0},{1,0},                {-1,1},{0,1},{1,1}              };inline bool legal(int x, int y){    if(x>=0 && x<n && y>=0 && y<n && !vis[x][y] && mat[x][y]!='#')return 1;    return 0;}void dfs(int x,int y,int cnt,int dirx,int diry,int is){    if(is>1)return;    vis[x][y]=1;ans=max(ans,cnt);    if(is == 1)    {        int xx=x+dirx;        int yy=y+diry;        int tt=0;        while(legal(xx,yy))        {            tt++;            //vis[xx][yy]=1;            xx=xx+dirx;            yy=yy+diry;        }        ans=max(ans,cnt+tt);        return;    }    //is==0  可以不转弯也可以转弯;    int ff=0;    for(int i=0;i<8;i++)    {        int xx=x+dir[i][0];        int yy=y+dir[i][1];        if(legal(xx,yy))        {            //ff++;            vis[xx][yy]=1;            if(dir[i][0]==dirx && dir[i][1]==diry)                ff++,dfs(xx,yy,cnt+1,dirx,diry,is);  //不转弯            else            {                if(dir[i][0]*dirx + dir[i][1]*diry == 0)                    ff++,dfs(xx,yy,cnt+1,dir[i][0],dir[i][1],is+1);//转弯            }            vis[xx][yy]=0;        }    }    if(!ff){ans=max(ans,cnt);return;}}int main(){    //IN("hdu5024.txt");    while(~scanf("%d",&n) && n)    {        ans=0;        for(int i=0;i<n;i++)scanf("%s",mat[i]);        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)            {                if(mat[i][j] == '#')continue;                CL(vis,0);                for(int k=0;k<8;k++)                {                    int xx=i+dir[k][0];                    int yy=j+dir[k][1];                    if(legal(xx,yy))                    {                        vis[i][j]=1;                        dfs(xx,yy,1,dir[k][0],dir[k][1],0);                    }                }            }        printf("%d\n",ans+1);    }    return 0;}
网上搜了个代码 15msAC,哎,,,

http://blog.csdn.net/u012150279/article/details/39434835

/************************************************************************> File Name: 1003.cpp> Author: Bslin> Mail: Baoshenglin1994@gmail.com> Created Time: 2014年09月20日 星期六 12时43分13秒 ************************************************************************/#include <stdio.h>char map[110][110];int dir[8][2] = {{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}};int n;bool inmap(int x, int y) {if(x >= 0 && x < n && y >= 0 && y < n)return true;return false;}int togo(int x, int y, int d1, int d2) {int nowx, nowy, res;res = 1;nowx = x + dir[d1][0];nowy = y + dir[d1][1];while(inmap(nowx, nowy) && map[nowx][nowy] == '.') {nowx = nowx + dir[d1][0];nowy = nowy + dir[d1][1];res ++;}nowx = x + dir[d2][0];nowy = y + dir[d2][1];while(inmap(nowx, nowy) && map[nowx][nowy] == '.') {nowx = nowx + dir[d2][0];nowy = nowy + dir[d2][1];res ++;}return res;}int dfs(int x, int y) {int res, tmp;res = 0;// 0: l  1: lu  2: u  3: ru  4: r  5: rd  6: d  7: ldtmp = togo(x, y, 0, 2);if(tmp > res) res = tmp;tmp = togo(x, y, 0, 6);if(tmp > res) res = tmp;tmp = togo(x, y, 4, 2);if(tmp > res) res = tmp;tmp = togo(x, y, 4, 6);if(tmp > res) res = tmp;tmp = togo(x, y, 1, 3);if(tmp > res) res = tmp;tmp = togo(x, y, 1, 7);if(tmp > res) res = tmp;tmp = togo(x, y, 5, 3);if(tmp > res) res = tmp;tmp = togo(x, y, 5, 7);if(tmp > res) res = tmp;return res;}int main(int argc, char *argv[]) {#ifndef ONLINE_JUDGEfreopen("in", "r", stdin);#endifint i, j;int ans, tmp;while(~scanf("%d", &n), n) {for (i = 0; i < n; ++i) {scanf("%s", map[i]);}ans = 0;for (i = 0; i < n; ++i) {for (j = 0; j < n; ++j) {if(map[i][j] == '.') {tmp = dfs(i, j);if(tmp > ans) ans = tmp;}}}printf("%d\n", ans);}return 0;}



1 0
原创粉丝点击