Uva-101-The Blocks Problem
2013-03-23 12:17
344 查看
这个题属于模拟题吧, 它的数据比POJ更为严格,我用POJ AC了的代码交却得到WA,无奈重写。
最后发现问题在于如果当前所需要移动的积木与移动到的积木在同一个位置,则不需要进行任何操作。
代码:
最后发现问题在于如果当前所需要移动的积木与移动到的积木在同一个位置,则不需要进行任何操作。
代码:
#include<cstdio> #include<iostream> #include<vector> using namespace std; const int maxn=26; vector<int> s[maxn]; int n,pos[maxn]; void Move(int m,int k) { int p=pos[m]; for(int i=s[m].size()-1;s[m][i]!=k;i--) { s[s[m][i]].push_back(s[m][i]); s[m].pop_back(); pos[s[m][i]]=s[m][i]; } } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { s[i].clear(); s[i].push_back(i); pos[i]=i; } char op[10],opb[10]; int a,b; while(1) { scanf("%s",op); if(op[0]=='q') break; scanf("%d%s%d",&a,opb,&b); if(pos[a]==pos[b]) continue; if(op[0]=='m') { if(opb[1]=='n') { Move(pos[a],a); Move(pos[b],b); s[pos[a]].pop_back(); s[pos[b]].push_back(a); pos[a]=pos[b]; } else { Move(pos[a],a); s[pos[a]].pop_back(); s[pos[b]].push_back(a); pos[a]=pos[b]; } } else { if(opb[1]=='n') { Move(pos[b],b); int len=s[pos[a]].size(),p=0; for(int i=0;i<len;i++) if(s[pos[a]][i]==a) { p=i; break; } int last=pos[a]; for(int i=p;i<len;i++) { s[pos[b]].push_back(s[last][i]); pos[s[last][i]]=pos[b]; } for(int i=p;i<len;i++) s[last].pop_back(); } else { int len=s[pos[a]].size(),p=0; for(int i=0;i<len;i++) if(s[pos[a]][i]==a) { p=i; break; } int last=pos[a]; for(int i=p;i<len;i++) { s[pos[b]].push_back(s[last][i]); pos[s[last][i]]=pos[b]; } for(int i=p;i<len;i++) s[last].pop_back(); } } } for(int i=0;i<n;i++) { printf("%d:",i); for(int j=0;j<s[i].size();j++) printf(" %d",s[i][j]); printf("\n"); } } return 0; }
相关文章推荐
- UVA 101(p110)----The Blocks Problem
- UVA-101 The Blocks Problem 栈模拟
- UVa 101 - The Blocks Problem
- UVa 101 The Blocks Problem
- UVa 101 - The Blocks Problem
- UVA 101 - The Blocks Problem
- UVA101-The Blocks Problem
- uva 101 The Blocks Problem (模拟)
- UVA 101 The Blocks Problem
- POJ 1208 The Blocks Problem (UVA 101)
- uva 101 - The Blocks Problem
- UVa 101 The Blocks Problem
- UVa 101 - The Blocks Problem
- UVa Problem 101 - The Blocks Problem
- UVa 101 - The Blocks Problem
- 【例题5-2 UVA - 101】The Blocks Problem
- uva 101 The Blocks Problem
- UVa 101 - The Blocks Problem
- uva 101 The Blocks Problem
- UVa 101 The Blocks Problem