把PKU2907“Collecting Beepers”AC了,其实昨天比赛的时候就可以AC,太紧张了!

做完网页后,静下心来做题,做昨天我WA的题,程序编得很快,30分钟就编完了,理由是我觉得我昨天的程序核心算法是对的。采用回朔法。不过提交的时候没有考虑到mins=99999的情况,应该是mins=0;所以wa了!

我的程序代码:

Collecting Beepers
Time Limit:1000MS  Memory Limit:65536K
Total Submit:488 Accepted:228

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Source
NCPC 2002

  • Source

     

    #include "iostream"
    #include "cmath"
    using namespace std;
    
    
    int mins,flag[21][21],length,map[21][21];
    
    void func(int i,int x1,int y1,int ix,int iy,int xsize,int ysize,int t,int * x,int *y)
    {
    	int k;
    	if(i==t+1)
    	{
    		length+=(abs(x1-ix)+abs(y1-iy));
    		func(i+1,ix,iy,ix,iy,xsize,ysize,t,x,y);
    		length-=(abs(x1-ix)+abs(y1-iy));
    		return;
    	}
    	if(i==t+2)
    	{
    		if(mins>length)
    			mins=length;
    		return;
    	}
    	for(k=0;k<t;k++)
    	{
    		if(!flag[x[k]][y[k]])
    		{
    			length+=(abs(x1-x[k])+abs(y1-y[k]));
    			flag[x[k]][y[k]]=1;
    			func(i+1,x[k],y[k],ix,iy,xsize,ysize,t,x,y);
    			length-=(abs(x1-x[k])+abs(y1-y[k]));
    			flag[x[k]][y[k]]=0;
    		}
    	}
    	return;
    }
    int main()
    {
    	int n,i;
    	int xsize,ysize,ix,iy,x[10],y[10],t;
    	cin>>n;
    	while(n)
    	{
    		cin>>xsize>>ysize;
    		cin>>ix>>iy;
    		cin>>t;
    		memset(map,0,sizeof(map));
    		memset(flag,0,sizeof(flag));
    		flag[ix][iy]=1;
    		length=0;
    		mins=99999;
    		for(i=0;i<t;i++)
    		{
    			cin>>x[i]>>y[i];
    			map[x[i]][y[i]]=1;
    		}
    		func(1,ix,iy,ix,iy,xsize,ysize,t,x,y);
    		if(mins==99999)
    			mins=0;
    		cout<<"The shortest path has length "<<mins<<endl;
    		n--;
    	}
    	return 0;
    }
  •  

    The shortest path has length 24

    1
    10 10
    1 1
    4
    2 3
    5 5
    9 4
    6 5

    The output will be one line per scenario, giving the minimum distance that Karel has to move to get from her starting position to each of the beepers and back again to the starting position.

    First there will be a line containing the number of scenarios you are asked to help Karel in. For each scenario there will first be a line containing the size of the world. This will be given as two integers (x-size and y-size). Next there will be one line containing two numbers giving the starting position of Karel. On the next line there will be one number giving the number of beepers. For each beeper there will be a line containing two numbers giving the coordinates of each beeper.

    Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position.

    Karel can only move along the x and y axis, never diagonally. Moving from one position (i, j) to an adjacent position (i, j + 1), (i, j − 1), (i − 1, j), or (i + 1, j) has a cost of one.

    You can assume that Karel’s world is never larger than 20 × 20 squares and that there will never be more than 10 beepers to pick up. Each coordinate will be given as a pair (x, y) where each value will be in the range 1 through the size of that particular direction of the coordinate system.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值