Blue Bridge Cup the previous questions take questions underground palace treasure

Blue Bridge Cup the previous questions take questions underground palace treasure

Problem Description
  X has a king underground palace treasure. Is nxm lattice matrix. Each grid put a baby. Each baby close to the value of the label.

Underground palace entrance at the top left corner, exit at the bottom right corner.

Bob was taken to the inlet of the underground palace, the king asked him to walk to the right or down only.

Walked by a grid, the grid value if the baby is larger than the value of the hands of any baby Xiao Ming, Xiao Ming can pick it up (of course, you can not take).

When Bob went to the exit, if he happens to be in the hands of baby k pieces, then the baby will be given to Bob.

Please help Xiao Ming told, in a given situation, he has a number of different courses of action to get this k treasures.
Input format
  input line of three integers, separated by spaces: nmk (1 <= n, m <= 50, 1 <= k <= 12)

Then there are n rows, each row having m integers Ci (0 <= Ci <= 12) representative of treasures of the squares of the value of
the output format
  required output an integer representing the number of action programs just taking the k baby. This number may be large, its output modulo 1000000007.
  
Sample input
2 2 2
. 1 2
2. 1

Sample Output
2

Sample input
2. 3 2
. 1 2. 3
2. 5. 1

Sample output
14

The subject first sight knows how deep search, so the first with a deep exploratory search about, may time out.
This question one more constraint, the value of picking off than before picking up goods, where the value of the items are large and can only pick k pieces of
this topic in three aspects.

  1. If the value of the goods in the grid than their maximum value items, then select.

  2. If the value of the goods in the grid than their maximum value items, then do not choose

  3. If the value of goods grid is smaller than the maximum value of the items themselves, then do not choose

    Since 2.3 is not selected for, so it can be grouped in a category, i.e., the article does not pick up the lattice, it becomes two.


  1. For items worth grid is larger than the maximum value of the goods themselves, then select
  2. Not select items lattice

Next, let me talk about the conditions of his suspension

  1. Bounds (i.e. ordinate or abscissa = m = n)
  2. To reach the destination (i.e., i.e. abscissa = m - 1 or ordinate = n - 1)

So for us to reach the end, there are two states

  1. We have picked up k items can no longer pick up, this is set up, plus a number of programs
  2. We picked up a k - 1 items, and the value of the goods at the end (ie the end position of the lower right corner) is greater than the maximum value of the pick. Then we can carry out the seizure of the article location, the program also set up, plus a number of programs.

code show as below

#include <iostream>
using namespace std;
#include <algorithm>
int n, m, k, a[55][55];
long long sum = 0;
#define MAX_SIZE 1000000007
void dfs(int x, int y, int max, int t)//x->横坐标 y->纵坐标 max->捡到的物品最大值, t->捡到的物品的数量
{
	if (x == n || y == m)
	{
		return ;//中止条件1
	}
	if (x == n - 1 && y == m - 1) //中止条件2
	{
		if (t == k)//终点状态条件1
		{
			sum++;
			sum %= MAX_SIZE;
		}
		if (t == k - 1 && a[x][y] > max)//终点状态条件2
		{
			sum++;
			sum %= MAX_SIZE;
		}
	}
	if (a[x][y] > max)
	{
		dfs(x + 1, y, a[x][y], t + 1);//深搜能够进行的条件1
		dfs(x, y + 1, a[x][y], t + 1);
	}
		dfs(x + 1, y, max, t);//深搜能够进行的条件2
		dfs(x, y + 1, max, t);
}
int main()
{
	int i, j;
	cin >> n >> m >> k;
	for (i = 0 ; i < n ; i++)
	{
		for (j = 0 ; j < m ; j++)
		{
			cin >> a[i][j];
		}
	}
	dfs(0, 0, -1, 0);//因为输入的价值可能等于0,所以传入-1
	cout << sum % MAX_SIZE;;
}

Sure, a deep search timeout ·····
then use an array of memory, i.e. memory [x] [y] [ max] [t], for the memory.
So how in the above procedures to improve it?

  1. The void type to be used so long long memory array may operate directly on the original number of known methods.
  2. Will return; return value of the place changed
  3. For already be stored value calculated
  4. To have some value, can be directly applied
  5. The previous global variable sum canceled

Talk about this place details of the conversion process

  1. Some examples might answer or no solution, so to all the memory is initialized to -1, to avoid confusion is to initialize the results obtained or the
  2. Because we passed above max = -1, so this time we reached the array to go inside, it is possible array index is -1, on overflow, but we have to be compared by -1, this time we can skillfully processing, i.e. max plus one in the array, so that better handled.
#include <iostream>
using namespace std;
#include <cstring>
#include <algorithm>
int n, m, k, a[55][55];
#define MAX_SIZE 1000000007
long long memory[55][55][15][15];
long long dfs(int x, int y, int max, int t)//x->横坐标 y->纵坐标 max->捡到的物品最大值, t->捡到的物品的数量
{
	long long sum = 0;
	if (memory[x][y][max + 1][t] != -1)
	{
		return memory[x][y][max + 1][t];
	}
	if (x == n || y == m)
	{
		return 0;
	}
	if (x == n - 1 && y == m - 1)
	{
		if (t == k)
		{
			sum++;
		}
		if (t == k - 1 && a[x][y] > max)
		{
			sum++;
		}
		sum %= MAX_SIZE;
		return sum;
	}
	if (a[x][y] > max)
	{
		sum += dfs(x + 1, y, a[x][y], t + 1);
		sum += dfs(x, y + 1, a[x][y], t + 1);
	}
		sum += dfs(x + 1, y, max, t);
		sum += dfs(x, y + 1, max, t);
		memory[x][y][max + 1][t] = sum % MAX_SIZE;
		return sum % MAX_SIZE;
}
int main()
{
	int i, j;
	cin >> n >> m >> k;
	for (i = 0 ; i < n ; i++)
	{
		for (j = 0 ; j < m ; j++)
		{
			cin >> a[i][j];
		}
	}
	memset(memory, -1, sizeof(memory));
	printf("%lld", dfs(0, 0, -1, 0));
}

If wrong, point out trouble! Thanks for watching!

Published 13 original articles · won praise 5 · Views 489

Guess you like

Origin blog.csdn.net/qq_44410340/article/details/104971706