GDUT_ winter training solution to a problem report _ the topic I_B, C, D title individual solution to a problem report

GDUT_ winter training solution to a problem report _ the topic I_B, C, D title individual solution to a problem report


Problem B: dfs ( I prefer to call it a function of infection, because before writing mine clearance when teaching others, would not say dfs say unhesitatingly say the infection function. )

Due to the recent rain, water in various fields where farmers John pooled with a N × M (1 <= N <= 100; 1 <= M <= 100) represented by squares. Each square containing water ( "W") or dry ( "."). Farmer John wanted to find out how much his field pond. Pond water is a group of connected square, which is considered a square adjacent to its eight neighbors.

Farmer John's field to a map, to determine how much of his pond.

Entry

* Line 1: two spaces separated integers: N and M

* Each row 2 ... N + 1: M characters, on behalf of his party Farmer John's field. Each character is a "W" or. "." There are no spaces between characters.

Export

* Line 1: The number of fields in the pond Farmer John.

样例输入
10 12
W . . . . . . . . WW .
. WWW . . . . . WWW
. . . . WW . . . WW .
. . . . . . . . . WW .
. . . . . . . . . W . .
. . W . . . . . . W . .
. W . W . . . . . WW .
W . W . W . . . . . W .
. W . W . . . . . . W .
. . W . . . . . . . W .

Sample output:
3

This have nothing to say, ordinary type of burning bridges dfs, and then be able to find a w Unicom gave him infected, but he still became '' to non-repeatable way.

On the code:

using namespace std;
int n, m;
void dfs(int,int);
char all[110][110];
int main()
{
	scanf ( "%d %d", &n, &m );
	for ( int time = 0; time < n; time++ )
	{
		for ( int time1 = 0; time1 < m; time1++ )
		{
			scanf ( " %c", &all[time][time1] );
		}
	}
	int sum=0;
	for(int time=0;time<n;time++)
	{
		for(int time1=0;time1<m;time1++)
		{
			if(all[time][time1]=='W'){dfs(time,time1);sum++;}
		}
	}
//	for ( int time = 0; time < n; time++
//	{
//		for ( int time1 = 0; time1 < m; time1++ )
//		{
//			printf ( "%c", all[time][time1] );
//		}
//		printf("\n");
//	}
	printf("%d\n",sum);

	return 0;
}
inline bool ilegal(int a,int b){
	return a<0||b<0||a>=n||b>=m;
}
void dfs(int a,int b)
{
	all[a][b]='.';

	for(int time=-1;time<2;time++)
	{
		for(int time1=-1;time1<2;time1++)
		{
			//if((time!=0)&&(time1!=0))
			{
				if(!ilegal(a+time,b+time1)&&(all[a+time][b+time1]=='W'))
				dfs(a+time,b+time1);
			}
		}
	}
	return ;
}

The accumulation of small usage: ilegal function determining the legality of a thing, to simplify the steps, very common




Problem C: dfs

There is a rectangular room, covered with tiles above. Each tile is red or black. A man standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can not move on red tiles, can only move on the black tiles.

Write a program to calculate the number of black tiles he can reach by repeating the above action.

Entry

A plurality of data input sets composition. Data set to include two positive integers beginning of the line W and H; W and H are the number of tiles in the x and y directions. W and H does not exceed 20.

There dataset H lines each containing W characters. Each tile represents a character color, as shown below.
'.'- a black tile
"35;" - Tile Red
"!" - a person standing on a black tile (concentrated only in the primary data)
input line end is a representation of two zeros.

Export

For each data set, your program should output a row containing the tile he can count initial tile (including itself) arriving from.

Sample input

6 9
. . . . # .
. . . . . #
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.

11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0

Sample output:

45
59
6
13

This question and similar questions B, will not go directly to the code:

using namespace std;
int line,arr;
char all[21][21];
void dfs(int x,int y);
int sum=0;
int check(int a,int b);

int main()
{
	while(~scanf("%d %d",&line,&arr))
	{
		if(line==0||arr==0)return 0;
		int start_x,start_y;
		sum=0;
		getchar();
		for(int time=0;time<arr;time++)
		{
			for(int time1=0;time1<line;time1++)
			{
				all[time][time1]=getchar();
				if(all[time][time1]=='@'){start_x=time;start_y=time1;}
			}
			getchar();
		}
		//input finished
		dfs(start_x,start_y);
		printf("%d\n",sum);
	}

	return 0;
}

void dfs(int x,int y)
{
	all[x][y]='#';
	sum++;
	if(check(x-1,y)&&all[x-1][y]=='.')dfs(x-1,y);
	if(check(x+1,y)&&all[x+1][y]=='.')dfs(x+1,y);
	if(check(x,y-1)&&all[x][y-1]=='.')dfs(x,y-1);
	if(check(x,y+1)&&all[x][y+1]=='.')dfs(x,y+1);

}
int check(int a,int b)
{
	if(a<arr&&a>-1&&b<line&&b>-1)return 1;
	else return 0;
}



D title: dfs (template title)

The more tricks than BC title,

I have N jewels, which intends to use the K satellites make a necklace to my mother, but she would not accept too heavy necklace. Taking into account the value and weight of each jewel, please help me to find the most valuable necklace my mother would have accepted.

Entry

The first line of input is the number of cases.
In each case, the first line contains two integers N (N <= 20), i.e., the total number of stone, K (K <= N) , i.e., the exact number necklace made of stone.

Followed by N rows, each row comprising two integers: a (a <= 1000), showing a value of each gemstone; b (b <= 1000), represents the weight of the gemstone.

The last line of each line contains an integer w, my mother received the maximum weight, w = 1000.

Export

In each case, the maximum possible value of the output of the necklace.

Sample input
1
2 1
1 1
1 1
. 3
Sample Output
1

BC title this subject than the pattern, beginning with the N items, take the C, which makes the C items weighing no more than the maximum accepted her mother's neck weight, but also the most expensive, is a look at knapsack problem is the explosion of data in search

code show as below:

using namespace std;

int N;
int K;
int V[21];
int W[21];
void dfs(int value,int weight,int i,int num);
int accepted_weight;
int _Max;
int main()
{
	int t;
	scanf("%d",&t);
	for(int time=0;time<t;time++)
	{
		_Max=INT_MIN;

		scanf("%d %d",&N,&K);

		for(int time1=0;time1<N;time1++)
		{
			scanf("%d %d",&V[time1],&W[time1]);
		}
		scanf("%d",&accepted_weight);
		dfs(0,0,0,0);

		printf("%d\n",_Max);
	}
	return 0;
}

void dfs(int value,int weight,int i,int num)
{
	if(i==N){if(_Max<value)_Max=value;}
	else
	{
		if(weight+W[i]<accepted_weight&&num<K)dfs(value+V[i],weight+W[i],i+1,num+1);
		dfs(value,weight,i+1,num);
	}

	return ;
}

Dfs handle the parameters of significance, it is relatively easy to A's

Released eight original articles · won praise 0 · Views 117

Guess you like

Origin blog.csdn.net/DevourPower/article/details/103952282