Usaco 17.Jan Copper Set T3

Source: Internet
Author: User

In the morning in the Usaco month of the copper, t1t2 is used to kill, but T3 card a morning, the following to the topic:

Test instructions is probably the input of a n*n matrix, the matrix element only 0 and 12 states, each operation with the top left corner of a matrix in the upper left vertex of a matrix, the matrix of all elements of the state change (that is, 0 to 1, 1 to 0), the matrix of the elements all become 0 the minimum number of times.

The first time to see the sample when thought is a Dfs or BFS search questions, and then decisively wrote the DFS, it is normal to WA. But in fact this problem needs to use greedy ...

As mentioned in the topic, a matrix is changed two times after the original state, that is, if the lower right corner of the point left to the last processing, there is a great possibility to repeat a certain operation, so the bottom right corner of each operation should be from the lower right corner of the search, since this, each operation of the matrix should be how to choose?

Because the final thing is to change all the elements to 0, the optimal solution is to ensure that the bottom-right vertex of each operation is 1, and that the matrix contains the highest number of 1.

Then the strategy is already there. The key code is as follows:

Read the data in advance and ask for prefixes and

for (;;)
{
BOOL Flag=false;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
if (a[i][j]== ' 1 ') {flag=true;break;}
if (flag) break;
}
if (!flag) break;//The judgment matrix is all 0
ans++;
int maxx=0,x=0,y=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (sum[i][j]>maxx&&a[i][j]== ' 1 ')//Find the lower right point of the matrix that has a state of 1 and contains the highest number of elements with a state of 1
{
MAXX=SUM[I][J];
X=i;y=j;
}
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
{
if (a[i][j]== ' 1 ') a[i][j]= ' 0 ';
elsea[i][j]= ' 1 ';//change the state of all elements in the selected matrix
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j]-' 0 ';//data changes after the matrix prefix and
}

Output answer

Time complexity O (n^4) with data range from 1 to 10, so no timeouts

Usaco 17.Jan Copper Set T3

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.