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
- hdu 5024 DFS 2014亚洲区域赛广州网赛
- hdu 5127/2014亚洲(广州)现场赛A
- 第一次参加亚洲区域赛(广州赛区):倒数第一
- 2014亚洲区域赛网络赛广州赛区A Corrupt Mayor's Performance Art(hdoj5023)
- HDU5024 2014 ACM-ICPC亚洲区域赛广州赛区网络赛C题 Wang Xifeng's Little Plot
- hdu 5131 Song Jiang's rank list 2014ACM/ICPC亚洲区广州站-重现赛
- hdu 5135 Little Zu Chongzhi's Triangles 2014ACM/ICPC亚洲区广州站-重现赛
- hdu 5131 Song Jiang's rank list 【2014ACM/ICPC亚洲区广州站-重现赛】
- HDU 5128 The E-pang Palace(2014亚洲区广州站现场赛)
- HDU 5131 Song Jiang's rank list(2014亚洲区广州站现场赛)
- HDU 5135 Little Zu Chongzhi's Triangles(2014亚洲区广州站现场赛)
- 2014广州亚洲现场赛/hdu 5128 The E-pang Palace(计算几何:暴力枚举)
- 2014广州亚洲现场赛/HDU 5131 Song Jiang's rank list(水题)
- 第39届大学生程序设计大赛亚洲区域赛广州站比赛总结
- 2014 亚洲区域赛 牡丹江+鞍山 总结
- 2014亚洲区域赛西安站总结
- 2014亚洲区域赛西安站总结
- HDU 5875 Function 2016亚洲区域赛大连网络赛
- SpringAop日志管理
- Benefits of Having SURE! as Your Billing and CRM Solution Partner
- iOS 使用ARC的工程怎么同时再引用MRC的第三方类库
- Java解压文件
- 10进制颜色代码
- hdu 5024 DFS 2014亚洲区域赛广州网赛
- 用WebCollector制作一个爬取《知乎》并进行问题精准抽取的爬虫(JAVA)
- Python下VTK 编程
- 三大框架面试总结
- .Net 简写
- 数据库中varchar和char的比较
- 字符串函数的自我实现
- UCS2 UTF8 编解码
- 下载win7默认字库