USACO 4.4 Shuttle Puzzle(dfs)
2013-03-29 14:29
399 查看
比较有意思的一个题,虽然不知道为什么这么做。。。看样例就可以发现一般是w_ _b wb_ _bw只能这样4种操作。。。没想到就这样过了。。注意了一下20个换行,2Y。。。我开始还在想bfs还想过,直接构造,构造不出来。。。
/* ID:cuizhe LANG: C++ TASK: shuttle */ #include <iostream> #include <cstdio> #include <map> #include <algorithm> #include <cstring> #include <string> using namespace std; map<string,int> mp; char str[32]; int que[100001]; int a[100001]; int ans = 10000000; int n; int judge() { int i; for(i = 0; i < n; i ++) if(str[i] != '2') break; if(i == n&&str == ' ') return 1; else return 0; } void dfs(int x,int step) { int i; if(ans <= step) return; if(judge()) { if(step-1 < ans) { ans = step-1; for(i = 1;i <= ans;i ++) a[i] = que[i]; } return ; } if(x-1 >= 0&&x-2 >= 0&&str[x-1] == '2'&&str[x-2] == '1') { que[step] = x-2; str[x-2] = ' '; str[x] = '1'; dfs(x-2,step+1); str[x-2] = '1'; str[x] = ' '; } if(x-1 >= 0&&str[x-1] == '1') { que[step] = x-1; str[x] = '1'; str[x-1] = ' '; dfs(x-1,step+1); str[x] = ' '; str[x-1] = '1'; } if(x+1 <= 2*n&&x+2 <= 2*n&&str[x+1] == '1'&&str[x+2] == '2') { que[step] = x+2; str[x+2] = ' '; str[x] = '2'; dfs(x+2,step+1); str[x+2] = '2'; str[x] = ' '; } if(x+1 <= 2*n&&str[x+1] == '2') { que[step] = x+1; str[x] = '2'; str[x+1] = ' '; dfs(x+1,step+1); str[x] = ' '; str[x+1] = '2'; } return ; } int main() { int i; freopen("shuttle.in","r",stdin); freopen("shuttle.out","w",stdout); scanf("%d",&n); for(i = 0; i < n; i ++) { str[i] = '1'; str[n+i+1] = '2'; } str = ' '; dfs(n,1); for(i = 1;i <= ans;i ++) { if(i%20 == 0||i == ans) printf("%d\n",a[i]+1); else printf("%d ",a[i]+1); } return 0; }
相关文章推荐
- USACO Section 4.4 Shuttle Puzzle - Hash都不用的DFS水题..
- USACO-Section 4.4 Shuttle Puzzle (BFS)
- USACO 4.4 shuttle puzzle
- USACO 4.4 Shuttle Puzzle
- usaco 4.4 Shuttle Puzzle 2010.8.8
- usaco 4.4 Shuttle Puzzle(字符hash+搜索)
- usaco 4.4 Shuttle Puzzle 找规律
- [2016/7/11][usaco 2.1][Flood Fill][dfs] The Castle
- USACO4.4.1 Shuttle Puzzle (shuttle)
- usaco4.4.1 Shuttle Puzzle
- hdu 4277 USACO ORZ DFS
- USACO section 1.5 Checker Challenge(dfs深搜)
- 求割边最少的最小割(P1344 [USACO4.4]追查坏牛奶Pollutant Control)
- usaco 4.4 Frame Up(拓扑排序)
- 【USACO】Subset Sums(双向搜索 dfs)
- USACO 2.1 健康的好斯坦奶牛 (DFS)
- USACO The Castle(floodfill(DFS))
- 洛谷P1344 [USACO4.4]追查坏牛奶Pollutant Control(网络流, 最大流最小割)
- 【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)
- USACO Section 1.4 - Mother's Milk(DFS + 记忆化)