Codeforces 611C New Year and Domino (dynamic planning, DP)

Source: Internet
Author: User

Test instructions: Given a h*w grid, there are only. and #, which means empty, #表示禁止的, and then there are Q queries that give you two coordinates, one for the top left and the lower right, to find out how many (vertical and horizontal) two adjacent points are in the middle of each.

Analysis: A see this topic, must be DP, a think, will not do, think for a long time will not, this how to analyze it, horizontal and vertical obviously is mixed, how to think about it, thought for a long time did not think out. Later secretly asked others, oh, the original is divided into consideration.

Separate consideration is the line of thought row, the column of the consideration of the column, and finally added to the good, we use dr[i][j] to represent the line I to the J position of the number of the same column Dc[i][j] column I to the number of J position.

Because the rows and columns are similar, we analyze the rows, if J-1 is., then the total is one more, otherwise the total is the same as the j-1, then consider the boundary, namely:if(G[i][j] = = '. ') && G[i][j-1] = = '. '  ) Dr[I][j] = Dr[i][j-1] + 1;
Else Dr[I][j] = Dr[i][j-1];

The same goes for the same list,

if (G[i][j) == '. ' && g[i-1 ][j == '. ' )     Dc[i][j" = dc[i-1 ][j] + 1

< Span class= "Sh-symbol" > < Span class= "Sh-symbol" >else     dc[i][j< Span class= "Sh-symbol" > = dc[i-1][j /span>

< Span class= "Sh-symbol" > < Span class= "Sh-symbol" > The code is as follows:

#include <iostream>#include<cstdio>#include<vector>#include<Set>#include<queue>#include<iomanip>#include<cstring>#include<sstream>#include<algorithm>#include<map>#include<list>using namespaceStd;typedefLong LongLL;Const intMAXN = -+Ten;CharG[MAXN][MAXN];intDR[MAXN][MAXN], DC[MAXN][MAXN];intMain () {intR, C; CIN>> R >>C;  for(inti =1; I <= R; ++i) scanf ("%s", g[i]+1); memset (DR,0,sizeof(DR)); memset (DC,0,sizeof(DC));  for(inti =1; I <= R; ++i) {         for(intj =1; J <= C; ++j) {            if(G[i][j] = ='.'&& g[i][j-1] =='.') Dr[i][j] = dr[i][j-1] +1; ElseDR[I][J] = dr[i][j-1]; if(G[i][j] = ='.'&& g[i-1][J] = ='.') Dc[i][j] = dc[i-1][J] +1; ElseDC[I][J] = dc[i-1][j]; }    }    intQ CIN >>Q;  while(q--){        intx1, y1, x2, y2; scanf ("%d %d%d%d", &x1, &y1, &AMP;X2, &y2); intCNT =0;  for(inti = x1; I <= x2; ++i) {CNT+ = Dr[i][y2]-Dr[i][y1]; }         for(inti = y1; I <= y2; ++i) {CNT+ = Dc[x2][i]-Dc[x1][i]; } printf ("%d\n", CNT); }    return 0;}

Codeforces 611C New Year and Domino (dynamic planning, DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.