南邮 OJ 1966 MOVE

MOVE

时间限制(普通/Java) :  2000 MS/ 10000 MS          运行内存限制 : 81920 KByte
总提交 : 90            测试通过 : 16 

比赛描述

On a infinite plane,you are on the (0,0) at the beginning.
You have to move to (x,y).But you can only move by the give K ways.
I want you to give me the least steps you need to move to the destination.

For example
 If you have to go to (3,1) and you have 3 ways to move: (-1,1) (1,0)(3,1).you only need one (3,1) move to get to the destination.

There are more than one case in a single input.
I promise that you can get to the destination.



输入

In the first there is a T,which means T cases;
In the first line there are 3 integers x y K   -100<x,y<100  k<=10
In the following K line ,each line has 2 integers xi,yi   -100<xi,yi<100;

输出

One integer to show the minimum steps.

样例输入

1
1 1 1
1 1

样例输出

1

题目来源

2E






/* Time Limit Exceed at Test 1
#include<iostream>
#include<queue>
using namespace std;

#define MAX_K 10
int x[MAX_K],y[MAX_K];

struct point{
	int x,y,step;
};

int main(){
	int t,target_x,target_y,k,i;
	point p,p1;
	queue<point> qp;
	scanf("%d%",&t);
	while(t--){
		scanf("%d%d%d",&k,&target_x,&target_y);
		for(i=0;i<k;i++){
			scanf("%d%d",x+i,y+i);
		}
		p.x = p.y = p.step = 0;
		while(!qp.empty()){
			qp.pop();
		}
		qp.push(p);
		while(!qp.empty()){
			p = qp.front();
			qp.pop();
			if(p.x==target_x && p.y==target_y){
				printf("%d\n",p.step);
				break;
			}
			p1.step = p.step+1;
			for(i=0;i<k;i++){
				p1.x = p.x+x[i];
				p1.y = p.y+y[i];
			}
			qp.push(p1);
		}
	}
}
*/



/* Wrong Answer at Test 1
#include<iostream>
#include<queue>
using namespace std;

#define MAX_K 10
int x[MAX_K],y[MAX_K];
#define MAX_X 101
#define MAX_Y 101
bool vst[MAX_X][MAX_X];

struct point{
	int x,y,step;
};

int main(){
	int t,target_x,target_y,k,i;
	point p,p1;
	queue<point> qp;
	scanf("%d%",&t);
	while(t--){
		memset(vst,0,sizeof(vst));
		scanf("%d%d%d",&k,&target_x,&target_y);
		for(i=0;i<k;i++){
			scanf("%d%d",x+i,y+i);
		}
		p.x = p.y = p.step = 0;
		while(!qp.empty()){
			qp.pop();
		}
		qp.push(p);
		vst[p.x][p.y] = 1;
		while(!qp.empty()){
			p = qp.front();
			qp.pop();
			if(p.x==target_x && p.y==target_y){
				printf("%d\n",p.step);
				break;
			}
			p1.step = p.step+1;
			for(i=0;i<k;i++){
				p1.x = p.x+x[i];
				p1.y = p.y+y[i];
				if(0<=p1.x && p1.x<MAX_X && 0<=p1.y && p1.y<MAX_Y && !vst[p1.x][p1.y]){
					vst[p1.x][p1.y] = 1;
					qp.push(p1);
				}
			}
		}
	}
}
*/

/* Wrong Answer at Test 1
#include<iostream>
#include<queue>
#include<set>
using namespace std;

#define MAX_K 10
int x[MAX_K],y[MAX_K];

struct point{
	int x,y,step;
};

bool operator<(const point p1, const point p2){
	if(p1.x == p2.x){
		return p1.y < p2.y;
	}
	return p1.x < p2.x;
}

int main(){
//	freopen("test.txt","r",stdin);
	int t,target_x,target_y,k,i;
	point p,p1;
	queue<point> qp;
	set<point> sp;
	scanf("%d%",&t);
	while(t--){
		scanf("%d%d%d",&k,&target_x,&target_y);
		for(i=0;i<k;i++){
			scanf("%d%d",x+i,y+i);
		}
		while(!qp.empty()){
			qp.pop();
		}
		sp.clear();
		p.x = p.y = p.step = 0;
		qp.push(p);
		sp.insert(p);
		while(!qp.empty()){
			p = qp.front();
			qp.pop();
			if(p.x==target_x && p.y==target_y){
				printf("%d\n",p.step);
				break;
			}
			p1.step = p.step+1;
			for(i=0;i<k;i++){
				p1.x = p.x+x[i];
				p1.y = p.y+y[i];
				if(!sp.count(p1)){
					sp.insert(p1);
					qp.push(p1);
				}
			}
		}
	}
}
*/





//1906MS
#include<iostream>
#include<queue>
#include<set>
using namespace std;

#define MAX_K 10
int x[MAX_K],y[MAX_K];

struct point{
	int x,y,step;
};

bool operator<(const point p1, const point p2){
	if(p1.x == p2.x){
		return p1.y < p2.y;
	}
	return p1.x < p2.x;
}

int main(){
//	freopen("test.txt","r",stdin);
	int t,target_x,target_y,k,i;
	point p,p1;
	queue<point> qp;
	set<point> sp;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&target_x,&target_y,&k);
		for(i=0;i<k;i++){
			scanf("%d%d",x+i,y+i);
		}
		while(!qp.empty()){
			qp.pop();
		}
		sp.clear();
		p.x = p.y = p.step = 0;
		qp.push(p);
		sp.insert(p);
		while(!qp.empty()){
			p = qp.front();
			qp.pop();
			if(p.x==target_x && p.y==target_y){
				printf("%d\n",p.step);
				break;
			}
			p1.step = p.step+1;
			for(i=0;i<k;i++){
				p1.x = p.x+x[i];
				p1.y = p.y+y[i];
				if(!sp.count(p1)){
					sp.insert(p1);
					qp.push(p1);
				}
			}
		}
	}
}


你可能感兴趣的:(ACM,move,南邮OJ)