天天看點

把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.