USACO 5.2 Snail Trails(DFS)
2013-05-20 10:50
411 查看
DFS,1Y啊。。
/* ID: cuizhe LANG: C++ TASK: snail */ #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> #include <cmath> #include <algorithm> using namespace std; int a[4] = {0,1,-1,0}; int b[4] = {1,0,0,-1}; int o[201][201]; int n,ans; void dfs(int x,int y,int pos,int step) { int i; ans = max(step,ans); if(x+a[pos] <= n&&x+a[pos] >= 1&&y+b[pos] >= 1&&y+b[pos] <= n&&!o[x+a[pos]][y+b[pos]]) { o[x+a[pos]][y+b[pos]] = 2; dfs(x+a[pos],y+b[pos],pos,step+1); o[x+a[pos]][y+b[pos]] = 0; } else if(x+a[pos] <= n&&x+a[pos] >= 1&&y+b[pos] >= 1&&y+b[pos] <= n&&o[x+a[pos]][y+b[pos]] == 2) { return ; } else { for(i = 0; i < 4; i ++) { if(pos == 0||pos == 3) { if(i == 1||i == 2) { if(x+a[i] <= n&&x+a[i] >= 1&&y+b[i] >= 1&&y+b[i] <= n&&!o[x+a[i]][y+b[i]]) { o[x+a[i]][y+b[i]] = 2; dfs(x+a[i],y+b[i],i,step+1); o[x+a[i]][y+b[i]] = 0; } } } else { if(i == 0||i == 3) { if(x+a[i] <= n&&x+a[i] >= 1&&y+b[i] >= 1&&y+b[i] <= n&&!o[x+a[i]][y+b[i]]) { o[x+a[i]][y+b[i]] = 2; dfs(x+a[i],y+b[i],i,step+1); o[x+a[i]][y+b[i]] = 0; } } } } } return ; } int main() { int m,i,j,len,sv,ev; char ch[101]; freopen("snail.in","r",stdin); freopen("snail.out","w",stdout); scanf("%d%d",&n,&m); for(i = 0; i < m; i ++) { scanf("%s",ch); len = strlen(ch); sv = ch[0]-'A' + 1; ev = 0; for(j = 1; j < len; j ++) { ev = ev*10 + ch[j]- '0'; } o[sv][ev] = 1; } o[1][1] = 2; dfs(1,1,0,1); dfs(1,1,1,1); printf("%d\n",ans); return 0; }
相关文章推荐
- usaco 5.2 Snail Trails(DFS)
- USACO Section 5.2 Wisconsin Squares - 按要求DFS就行了..
- [USACO5.2]Snail Trails
- USACO 5.2 Snail Trails
- usaco 5.2 Wisconsin Squares(DFS)
- Snail Trail_usaco 5.2_dfs
- 【题目】[USACO5.2]蜗牛的旅行Snail Trails
- [USACO5.2]Snail Trails
- USACO 5.2 Wisconsin Squares(DFS)
- Zero Sum_usaco2.3.3_dfs
- [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
- USACO section 2.4 Overfencing(dfs+bfs)
- 洛谷 1118 [USACO] 数字三角形(不是DP的那道) dfs+乱搞
- USACO milk3 DFS
- USACO 2.3 和为零 (DFS)
- [BZOJ1616][Usaco2008 Mar]Cow Travelling游荡的奶牛(dfs||dp)
- USACO The Castle(floodfill(DFS))
- 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名
- USACO Prime Palindromes(dfs)
- usaco street race(dfs)