zoj 2954 Hanoi Tower

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1953

 

这道题挺简单的,但是有陷阱啊.刚开始掉进去了,怎么也爬不出来.后来看了别人的解题报告才发现.哎..被骗了.以后注意这种题型.

 

题目中说:

 (1) If there is an invalid move before all disks being on peg 3 and the invalid move is the p-th move of this case (start from 1) , output the integer -p please and the moves after this move(if any) are ignored.
(2) If after the p-th move all disks are on peg 3 without any invalid move, output the integer p please and the moves after this move (if any) are ignored.

结果那两个ignore被我ignore了..T_T.

我写了一个for循环,在里面输入数据.然后判断是否要停止程序然后打印结果..后来才发现如果停止了..后面输入的数据就输入到下一个测试数据组了..

然后只好将数据先放到数组里.然后判断..

上代码:

 

/* * 2954.cpp * * Created on: Apr 5, 2010 * Author: wyy * */ #include<iostream> #include<vector> using namespace std; int main() { //freopen("input.txt", "r", stdin); const int SIZE = 12001; int T, n, m, index; int a[SIZE], b[SIZE]; cin >> T; while(T--) { vector< vector<int> > vec(3); cin >> n >> m; for(int i = 0; i != n; ++i) vec[0].push_back(n - i); for( int i = 0; i != m; ++i) cin >> a[i] >> b[i]; for(index = 0; index != m; ++index) { if(vec[a[index] - 1].empty() || (!vec[b[index] - 1].empty() && *(vec[a[index] - 1].end() - 1) > *(vec[b[index] - 1].end() - 1))) { cout << -(index + 1) << endl; break; } else { vec[b[index] - 1].push_back(*(vec[a[index] - 1].end() - 1)); vec[a[index] - 1].erase(vec[a[index] - 1].end() - 1); } if(vec[2].end() - vec[2].begin() == n) { cout << index + 1 << endl; break; } } if(index == m) cout << "0" << endl; } //fclose(stdin); return 0; }

 

你可能感兴趣的:(vector,测试,Integer,output,2010)