ue 抗鋸齒 渲染序列失靈_最大的鋸齒形序列

ue 抗鋸齒 渲染序列失靈

Problem statement:

問題陳述:

Given a square matrix of size n x n, find the sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence cannot belong to the same column.

給定大小為nxn的方陣, 找到Zigzag序列的總和最大之字形序列從頂部開始,在底部結束。 序列的兩個連續元素不能屬于同一列。

    Input:
First line contains an integer N denoting the size of the matrix. 
Then in the next line are N*N space separated values of the matrix.
Output:
Print the required max sum.
Constraints:
1<=N<=100
1<=M[][]<=1000

Example:

例:

    Input:
Input matrix
3 8 2
4 8 5
6 9 7
Output: 22
Maximum Zigzag sequence is: 
8->5->9
Other sequences can be: 
3->8->6 etc.
Input:
Input matrix
3  2  1
10 8  5 
6  6  4
Output: 18
In the above also, the maximum zigzag sequence will be:
2->10->6

The second example clearly shows that the greedy solution wouldn't work. Let's say we opt for greedy technique and as a measure, what we do is to extract local maximum, i.e., to extract maximum out of this row and the go-ahead to the next row and find out maximum skipping the column where we found our last maximum.

第二個例子清楚地表明,貪婪的解決方案是行不通的。 假設我們選擇貪婪技術,并且作為一種度量,我們要做的是提取局部最大值,即從該行中提取最大值,然后繼續到下一行,并找出最大值,跳過找到我們的列最后的最大值。

So if we follow a similar approach, let's check what we can lead to.

因此,如果我們采用類似的方法,那么讓我們檢查一下會導致什么。

So, firstly, we will pick 3 out of the first row (0th row)

因此,首先,我們將從第一行( 0行)中選擇3

From the next row, we will pick 8 as we can't peek 10 due to out constraint.

在下一行中,我們將選擇8 ,因為由于出局限制我們無法窺視10

And from the next row, we will pick 6?(of 0th column)

然后從下一行中,選擇6 ( 0列)

So it sums up to 3+8+6 which is 17. But it's wrong we know output would be 18. So finding local best doesn't lead to global best. Hence, greedy will not work here.

因此,總和為3 + 8 + 6 ,即17 。 但是我們知道輸出為18是錯誤的。 因此,找到本地最佳并不能帶來全球最佳。 因此,貪婪在這里不起作用。

We need dynamic programing or recursion to solve.

我們需要動態編程或遞歸來解決。

Solution Approach:

解決方法:

So, let's see what can be a recursive solution. Then, we will check for overlapping sub-problems and will gradually optimize the recursion by memorization.

因此,讓我們看看什么是遞歸解決方案。 然后,我們將檢查重疊的子問題,并通過記憶逐步優化遞歸。

Let the recursive function be, recurZigzag(matrix, currow, curcolulm, n)

設遞歸函數為recurZigzag(matrix,cur row ,cur colulm ,n)

    Function recurZigzag(matrix,currow,curcolulm):
If (currow reaches n)
Return 0;
for i=0 to n except curcolumn
Return  matrix[currow][curcolumn] + max(recurZigzag(matrix, currow + 1, i))
End Function
// In the main function
for column=0 to n-1
result=max?(result,recurZigzag(matrix,0,column)

So what we did?

那我們做了什么?

We are starting from each column on the top row.

我們從第一行的每一列開始。

Then we are recursively checking for the next row skipping the current column. So, this checks all combinations possible.

然后,我們遞歸地檢查跳過當前列的下一行。 因此,這將檢查所有可能的組合。

I would recommend to create the recursion tree for example 1 and check whether you can find overlapping sub-problems. There will be a lot of overlapping problems even for smaller inputs.

我建議為示例1創建遞歸樹,并檢查是否可以找到重疊的子問題。 即使對于較小的輸入,也會有很多重疊的問題。

So, we need to pass that overhead and we can solve that by memorization technique where we store the value of solved sub-problem in DP[][]

因此,我們需要傳遞該開銷,并且可以通過存儲將已解決子問題的值存儲在DP [] []中的記憶技術來解決該問題。

So, we first lookup in DP[][] whether it's already solved or not. If it's already solved then we will have some value for DP[i][j] (for subproblem, f(i,j)), Else DP[i][j] will contain the initialized value only.

因此,我們首先在DP [] []中查找它是否已解決。 如果已經解決,則DP [i] [j]會有一些值(對于子問題f(i,j) ),其他DP [i] [j]僅包含初始化值。

This is the trick here.

這是這里的把戲。

Below is the full CPP implementation for understanding memorization.

以下是用于理解記憶的完整CPP實施。

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int findmax(vector<int> arr, int in, int n, int* lastindex)
{
int max = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > max && i != in) {
max = arr[i];
*lastindex = i;
}
}
return max;
}
int recurZigZag(vector<vector<int> > arr, int row, int col, int n, int** dp)
{
//memoization part
if (dp[row][col] != -1) //if already solved, no need to compute again
return dp[row][col];
if (row == n - 1) {
dp[row][col] = arr[row][col];
return arr[row][col];
}
int max = INT_MIN;
for (int k = 0; k < n; k++) {
if (k != col) {
int t = recurZigZag(arr, row + 1, k, n, dp);
if (max < t)
max = t;
}
}
dp[row][col] = std::max(dp[row][col], arr[row][col] + max); //store solution
return dp[row][col];
}
int main()
{
int t, n, item;
cout << "Enter test case:\n";
cin >> t;
for (int i = 0; i < t; i++) {
cout << "Input size of square matrix\n";
cin >> n;
vector<vector<int> > arr;
cout << "Input the square matrix\n";
for (int i = 0; i < n; i++) {
vector<int> inn;
for (int j = 0; j < n; j++) {
cin >> item;
inn.push_back(item);
}
arr.push_back(inn);
}
int** dp = (int**)(malloc(sizeof(int*) * n));
for (int i = 0; i < n; i++) {
dp[i] = (int*)(malloc(sizeof(int) * n));
for (int j = 0; j < n; j++)
dp[i][j] = -1;
}
int mymax = INT_MIN;
for (int i = 0; i < n; i++) {
int p = recurZigZag(arr, 0, i, n, dp);
if (p > mymax)
mymax = p;
}
cout << "Maximum zigzag sum: " << mymax << endl;
}
return 0;
}

Output

輸出量

Enter test case:
2
Input size of square matrix 
3
Input the square matrix  
3 8 2  
4 8 5  
6 9 7  
Maximum zigzag sum: 22
Input size of square matrix 
3
Input the square matrix  
3 2 1  
10 8 5 
6 6 4  
Maximum zigzag sum: 18

翻譯自: https://www.includehelp.com/icp/largest-zigzag-sequence.aspx

ue 抗鋸齒 渲染序列失靈

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540653.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540653.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540653.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

團隊-團隊編程項目作業名稱-成員簡介及分工

成員&#xff1a;祁昊 分工&#xff1a;ui設計&#xff0c;美工&#xff0c;詳細設計。轉載于:https://www.cnblogs.com/qihao10086/p/7496101.html

python身份運算符_Python身份運算符

python身份運算符Identity operators are used to perform the comparison operation on the objects i.e. these operators check whether both operands refer to the same objects (with the same memory location) or not. 身份運算符用于對對象執行比較操作&#xff0c;即…

Oracle-Decode()函數和CASE語句的不同

Oracle-Decode()函數和CASE語句的區別&#xff1a; 具體示例如下&#xff1a; 1.CASE語句&#xff1a; SELECT CASE SIGN(5 - 5) WHEN 1 THEN Is Positive WHEN -1 THEN Is Negative ELSE Is Zero END FROM DUAL; 后臺實現&#xff1a; if (SIGN(5 – 5) 1) { Is Positive; } …

ai智能模式_AI的完整形式是什么?

ai智能模式AI&#xff1a;人工智能 (AI: Artificial Intelligence) AI is an abbreviation of "artificial intelligence", which occasionally called machine intelligence in the field of computer science. It is intelligence made understandable by machines…

centos6.5安裝python3.6

1、下載Python安裝包 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 2、解壓安裝包&#xff1a;tar -xzvf Python-3.6.0.tgz 3、進入安裝包路徑&#xff1a;cd Python-3.6.04、編譯安裝包 注意&#xff1a;prefix參數用于指定將Python安裝在新目錄&#xff…

BE的完整形式是什么?

工學學士 (BE: Bachelor of Engineering) BE is an abbreviation of Bachelor of Engineering. It is a bachelors degree program for under graduation in engineering and the duration of this course is 4 years. It is provided in many countries like India, Canada, S…

史上最詳細Windows版本搭建安裝React Native環境配置

說在前面的話: 感謝同事金曉冰傾情奉獻本環境搭建教程 之前我們已經講解了React Native的OS X系統的環境搭建以及配置&#xff0c;鑒于各大群里有很多人反應在Windows環境搭建出現各種問題&#xff0c;今天就特意更新一貼來說明。關于os x環境搭建以及react native入門學習資料…

程序代碼錯誤檢測_錯誤檢測代碼

程序代碼錯誤檢測錯誤檢測代碼 (Error Detecting Codes) A group of bits is known as words, and these words move as an entity from one block to another in the digital system. While moving from one part to another within the system via transmission media, the b…

Web瀏覽器端通過https 使用mqtt通訊

做的產品簡介 這次需要做一個web端的上課平臺&#xff0c;有音視頻通訊&#xff0c;有白板(畫板)功能&#xff0c;有文字通訊等。技術點 音視頻通訊需要走Webrtc需要跟ios, android, windows, mac 客戶端互聯互通一般通訊通過mqtt協議MQTT簡介 MQTT&#xff08;Message Queuing…

vga顯示模式_VGA的完整形式是什么?

vga顯示模式VGA&#xff1a;視頻圖形陣列 (VGA: Video Graphics Array) VGA is an abbreviation of "Video Graphics Array". VGA是“視頻圖形陣列”的縮寫 。 It is a three-row 15-pin DE-15 connector display hardware developed by IBM in 1987. It was first …

【iCore4 雙核心板_FPGA】例程十一:FSMC總線通信實驗——獨立地址模式

實驗原理&#xff1a; STM32F767上自帶FMC控制器&#xff0c;本實驗將通過FMC總線的地址獨立模式實現STM32與FPGA 之間通信&#xff0c;FPGA內部建立RAM塊,FPGA橋接STM32和RAM塊&#xff0c;本實驗通過FSMC總線從STM32向 RAM塊中寫入數據&#xff0c;然后讀取RAM出來的數據進行…

世界糧農組織五大健康食品_糧農組織的完整形式是什么?

世界糧農組織五大健康食品糧農組織&#xff1a;請注意 (FAO: For the Attention Of) FAO is an abbreviation of "For the Attention Of". FAO是“ For the Attention Of”的縮寫 。 It is an expression, which is commonly used in the Gmail platform. When a ma…

http 412 precondition failed

2019獨角獸企業重金招聘Python工程師標準>>> 今天在谷歌瀏覽器上刷新頁面的時候&#xff0c;出現了 如下失敗信息&#xff1a; HTTP 412 (Precondition Failed) 想想當時的動作是在發送ajax請求失敗之后&#xff0c;再刷新&#xff0c;就會出現上面的失敗問題。百度…

Python | Pyplot標簽

There are the following types of labels, 標簽有以下幾種&#xff0c; 1)X軸貼標 (1) X-axis labelling) plt.xlabel(Number Line)# Default labellingplt.xlabel(Number Line, colorgreen)#Font colour Changedplt.xlabel(Number Line, colorGreen, fontsize15)#Font size …

LTNS的完整形式是什么?

LTNS&#xff1a;很久沒看到 (LTNS: Long Time No See) LTNS is an abbreviation of "Long time, no see". LTNS是“長時間&#xff0c;看不見”的縮寫 。 It is an English phrase used when people meet and greet each other after a while when in between they…

MySQL Index Condition Pushdown

2019獨角獸企業重金招聘Python工程師標準>>> 一、Index Condition Pushdown簡介 ICP&#xff08;index condition pushdown&#xff09;是mysql利用索引&#xff08;二級索引&#xff09;元組和篩字段在索引中的where條件從表中提取數據記錄的一種優化操作。ICP的思…

ADBB的完整形式是什么?

ADBB&#xff1a;所有完成的再見 (ADBB: All Done Bye Bye) ADBB is an abbreviation to All Done Bye Bye. ADBB是All Done Bye Bye的縮寫。 Whenever a person wants to convey his message to another person, they use some sort of short-form in the text messages. ADB…

c 環境

系統ubuntu sudo apt-get install vim g openssh-server libgl1-mesa-dev檢查下安裝的版本gcc -v g -v make -v gdb -v 轉載于:https://blog.51cto.com/skinglzw/1964449

java.util (Collection接口和Map接口)

1&#xff1a;Collection和Map接口的幾個主要繼承和實現類 1.1 Collection接口 Collection是最基本的集合接口&#xff0c;一個Collection代表一組Object&#xff0c;即Collection的元素&#xff08;Elements&#xff09;。一些Collection允許相同的元素而另一些不行。一些能排…

scala 拆分字符串翻轉_Scala程序分割字符串

scala 拆分字符串翻轉A string is a collection that stores multiple characters, it is an immutable sequence which cannot be changed. 字符串是存儲多個字符的集合&#xff0c;它是不可更改的不可更改的序列。 分割字符串 (Splitting a string) In Scala, using the spl…